Skip to content

Commit 8615904

Browse files
authored
Merge pull request #111 from ndaidong/v2.0.0
v2.0.0
2 parents 83c03fb + ee369c3 commit 8615904

File tree

9 files changed

+154
-56
lines changed

9 files changed

+154
-56
lines changed

README.md

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# oembed-parser
23

34
Extract eEmbed content from given URL.
@@ -15,19 +16,28 @@ Extract eEmbed content from given URL.
1516
- [Example FaaS](https://us-central1-technews-251304.cloudfunctions.net/oembed-parser?url=https://www.youtube.com/watch?v=8jPQjjsBbIc)
1617

1718

18-
### Installation
19+
## Installation
1920

2021
```bash
21-
npm install oembed-parser
22+
$ npm install oembed-parser
23+
24+
# or pnpm
25+
$ pnpm install oembed-parser
26+
27+
# or yarn
28+
$ yarn add oembed-parser
2229
```
2330

24-
### Usage
31+
32+
## Usage
2533

2634
```js
27-
import {
28-
extract
29-
} from 'oembed-parser'
35+
const { extract } = require('oembed-parser')
3036

37+
// es6 module syntax
38+
import { extract } from 'oembed-parser'
39+
40+
// test
3141
const url = 'https://www.youtube.com/watch?v=8jPQjjsBbIc'
3242

3343
extract(url).then((oembed) => {
@@ -37,96 +47,139 @@ extract(url).then((oembed) => {
3747
})
3848
```
3949

40-
### APIs
50+
## APIs
4151

4252
#### .extract(String url [, Object params])
4353

44-
Extract oEmbed data from specified url.
45-
Return: a Promise
46-
47-
Optional argument `params` is an object with it we can set `maxwidth` and/or `maxheight` those are used to scale embed size to fit your container size. Please refer [oEmbed/Full Spec/Consumer Request](https://oembed.com/#section2) for more info.
54+
Load and extract oembed data.
4855

49-
Here is how we can use `oembed-parser` in async/await style:
56+
Example:
5057

5158
```js
52-
import {
53-
extract
54-
} from 'oembed-parser'
59+
const { extract } = require('oembed-parser')
5560

5661
const getOembed = async (url) => {
5762
try {
5863
const oembed = await extract(url)
5964
return oembed
6065
} catch (err) {
6166
console.trace(err)
67+
return null
6268
}
6369
}
6470

6571
const data = getOembed('your url')
6672
console.log(data)
6773
```
6874

75+
Optional argument `params` is an object with it we can set `maxwidth` and/or `maxheight` those are used to scale embed size to fit your container size. Please refer [oEmbed/Full Spec/Consumer Request](https://oembed.com/#section2) for more info.
6976

7077
#### .hasProvider(String URL)
7178

72-
Return boolean. True if the URL matches with any provider in the list.
79+
Check if a URL matches with any provider in the list.
80+
81+
Examples:
82+
83+
```js
84+
const { hasProvider } = require('oembed-parser')
85+
86+
hasProvider('https://www.youtube.com/watch?v=ciS8aCrX-9s') // return true
87+
hasProvider('https://trello.com/b/BO3bg7yn/notes') // return false
88+
```
7389

7490
#### .findProvider(String URL)
7591

76-
Return provider which is relevant to given URL.
92+
Get the provider which is relevant to given URL.
7793

7894
For example:
7995

8096
```js
81-
import {
82-
findProvider
83-
} from 'oembed-parser'
97+
const { findProvider } = require('oembed-parser')
8498

8599
findProvider('https://www.facebook.com/video.php?v=999999999')
100+
```
86101

87-
// get something like below:
102+
Result looks like below:
88103

89-
// {
90-
// fetchEndpoint: 'https://graph.facebook.com/v10.0/oembed_video',
91-
// providerName: 'Facebook',
92-
// providerUrl: 'https://www.facebook.com/'
93-
// }
104+
```json
105+
{
106+
fetchEndpoint: 'https://graph.facebook.com/v10.0/oembed_video',
107+
providerName: 'Facebook',
108+
providerUrl: 'https://www.facebook.com/'
109+
}
94110
```
95111

112+
#### .setProviderList(Array providers)
96113

97-
#### .setProviderList(Array of provider definitions)
98-
99-
Sets the list of providers to use, overriding the defaults.
114+
Apply a list of providers to use, overriding the [default](https://raw.githubusercontent.com/ndaidong/oembed-parser/master/src/utils/providers.json).
100115

101116
This can be useful for whitelisting only certain providers, or for adding
102117
custom providers.
103118

104-
For the expected format, see the
105-
[default list](https://raw.githubusercontent.com/ndaidong/oembed-parser/master/src/utils/providers.json).
119+
Default list of resource providers is synchronized from [oembed.com](http://oembed.com/providers.json).
106120

121+
For example:
122+
123+
```js
124+
const { setProviderList } = require('oembed-parser')
125+
126+
const providers = [
127+
{
128+
provider_name: 'Alpha',
129+
provider_url: 'https://alpha.com',
130+
endpoints: [
131+
// endpoint definition here
132+
]
133+
},
134+
{
135+
provider_name: 'Beta',
136+
provider_url: 'https://beta.com',
137+
endpoints: [
138+
// endpoint definition here
139+
]
140+
}
141+
]
107142

108-
### Provider list
143+
setProviderList(providers)
144+
```
145+
146+
#### .setRequestOptions(Object requestOptions)
147+
Define options to call oembed HTTP request.
109148

110-
List of resource providers is a clone of [oembed.com](http://oembed.com/providers.json) and available [here](https://raw.githubusercontent.com/ndaidong/oembed-parser/master/src/utils/providers.json).
149+
`oembed-parser` is using [axios]() to send HTTP requests. Please refer [axios' request config](https://axios-http.com/docs/req_config) for more info.
150+
151+
Default option:
152+
153+
```js
154+
{
155+
headers: {
156+
'user-agent': 'Mozilla/5.0 (X11; Linux i686; rv:94.0) Gecko/20100101 Firefox/94.0',
157+
accept: 'application/json; charset=utf-8'
158+
},
159+
responseType: 'json',
160+
responseEncoding: 'utf8',
161+
timeout: 6e4,
162+
maxRedirects: 3
163+
}
164+
```
111165

112166

113167
## Facebook and Instagram
114168

115169
Since October 24 2020, Facebook have deprecated their legacy urls and applied a new Facebook oEmbed endpoints.
116-
Please update your `oembed-parser` version to v1.4.2 or later to be able to extract oembed data from Instagram and Facebook.
117-
118-
Technically, now we have to use Facebook Graph API, with the access token from a valid and live Facebook app.
119170

171+
Technically, now we have to use Facebook Graph API, with the access token from a valid and live Facebook app. `oembed-parser` will try to get these values from environment variables, so please define them, for example:
120172

121173
```bash
122174
export FACEBOOK_APP_ID=your_app_id
123175
export FACEBOOK_CLIENT_TOKEN=your_client_token
124176
```
125177

126-
For more info, please refer:
178+
References:
127179

180+
- [oEmbed Read](https://developers.facebook.com/docs/features-reference/oembed-read)
128181
- [Facebook oEmbed](https://developers.facebook.com/docs/plugins/oembed)
129-
182+
- [Facebook Graph API](https://developers.facebook.com/docs/graph-api/overview)
130183

131184
## Test
132185

@@ -140,6 +193,7 @@ npm test
140193
npm run eval {URL_TO_PARSE_OEMBED}
141194
```
142195

143-
# License
144-
196+
## License
145197
The MIT License (MIT)
198+
199+
---

index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77

88
export function extract(url: string, params?: any): Promise<OembedData>;
99

10-
export function hasProvider(url: string): boolean;
10+
export function hasProvider(url: string): boolean
1111

1212
export function setProviderList(providers: Provider[]): void
1313

14+
export function setRequestOptions(options: object): void
15+
16+
export function getRequestOptions(): object
17+
1418
export interface Endpoint {
1519
schemes?: string[];
1620
url: string;

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "2.0.0rc2",
2+
"version": "2.0.0",
33
"name": "oembed-parser",
44
"description": "Get oEmbed data from given URL.",
55
"homepage": "https://www.npmjs.com/package/oembed-parser",
@@ -24,10 +24,11 @@
2424
"reset": "node reset"
2525
},
2626
"dependencies": {
27-
"got": "^11.8.3"
27+
"axios": "^0.24.0",
28+
"bellajs": "^10.0.2"
2829
},
2930
"devDependencies": {
30-
"jest": "^27.4.3",
31+
"jest": "^27.4.5",
3132
"nock": "^13.2.1"
3233
},
3334
"keywords": [

reset.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const dirs = [
1818

1919
const files = [
2020
'yarn.lock',
21+
'pnpm-lock.yaml',
2122
'package-lock.json',
2223
'coverage.lcov',
2324
'coverage.lcov'

src/config.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
// config
22

3-
const fetchOptions = {
3+
const { clone, copies } = require('bellajs')
4+
5+
const requestOptions = {
46
headers: {
57
'user-agent': 'Mozilla/5.0 (X11; Linux i686; rv:94.0) Gecko/20100101 Firefox/94.0',
68
accept: 'application/json; charset=utf-8'
79
},
810
responseType: 'json',
9-
timeout: 30 * 1e3,
10-
redirect: 'follow'
11+
responseEncoding: 'utf8',
12+
timeout: 6e4, // 1 minute
13+
maxRedirects: 3
1114
}
1215

1316
module.exports = {
14-
fetchOptions
17+
getRequestOptions: () => {
18+
return clone(requestOptions)
19+
},
20+
setRequestOptions: (opts) => {
21+
copies(opts, requestOptions)
22+
}
1523
}

src/main.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const fetchEmbed = require('./utils/fetchEmbed')
88

99
const provider = require('./utils/provider')
1010

11+
const {
12+
setRequestOptions
13+
} = require('./config')
14+
1115
const extract = async (url, params = {}) => {
1216
if (!isValidURL(url)) {
1317
throw new Error('Invalid input URL')
@@ -24,5 +28,6 @@ module.exports = {
2428
extract,
2529
hasProvider: provider.has,
2630
findProvider: provider.find,
27-
setProviderList: provider.set
31+
setProviderList: provider.set,
32+
setRequestOptions
2833
}

src/main.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ const {
77
extract,
88
hasProvider,
99
findProvider,
10-
setProviderList
10+
setProviderList,
11+
setRequestOptions
1112
} = require('./main')
1213

14+
const {
15+
getRequestOptions
16+
} = require('./config')
17+
1318
const required = [
1419
'type',
1520
'version'
@@ -251,3 +256,23 @@ test('test .setProviderList() method', () => {
251256
expect(hasProvider('http://www.example.org/media/abcdef')).toBe(true)
252257
expect(hasProvider('https://www.youtube.com/watch?v=ciS8aCrX-9s')).toBe(false)
253258
})
259+
260+
test('Testing setRequestOptions()', () => {
261+
setRequestOptions({
262+
headers: {
263+
authorization: 'bearer <token>'
264+
},
265+
timeout: 20,
266+
somethingElse: 1000
267+
})
268+
269+
const actual = getRequestOptions()
270+
const expectedHeader = {
271+
authorization: 'bearer <token>',
272+
'user-agent': 'Mozilla/5.0 (X11; Linux i686; rv:94.0) Gecko/20100101 Firefox/94.0',
273+
accept: 'application/json; charset=utf-8'
274+
}
275+
276+
expect(actual.headers).toEqual(expectedHeader)
277+
expect(actual.timeout).toEqual(20)
278+
})

src/utils/retrieve.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// utils -> retrieve
22

3-
const got = require('got')
3+
const axios = require('axios')
44

5-
const { fetchOptions } = require('../config')
5+
const { getRequestOptions } = require('../config')
66

77
module.exports = async (url) => {
88
try {
9-
const { headers, body } = await got(url, fetchOptions)
9+
const res = await axios.get(url, getRequestOptions())
1010

11-
const contentType = headers['content-type'] || ''
11+
const contentType = res.headers['content-type'] || ''
1212
if (!contentType || !contentType.includes('application/json')) {
1313
return null
1414
}
1515

16-
return body
16+
return res.data
1717
} catch (err) {
1818
return null
1919
}

src/utils/retrieve.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test('test retrieve() from bad source', async () => {
2828
const url = 'https://some.where/good/page'
2929
const { baseUrl, path } = parseUrl(url)
3030
const scope = nock(baseUrl)
31-
scope.get(path).reply(500, { data: { name: 'oembed-parser' } }, {
31+
scope.get(path).reply(500, '', {
3232
'Content-Type': 'application/json'
3333
})
3434
const result = await retrieve(url)

0 commit comments

Comments
 (0)