oembed-parser
Advanced tools
Comparing version 2.0.0-rc2 to 2.0.0
@@ -10,6 +10,10 @@ // Type definitions for oembed-parser 1.3.5 | ||
export function hasProvider(url: string): boolean; | ||
export function hasProvider(url: string): boolean | ||
export function setProviderList(providers: Provider[]): void | ||
export function setRequestOptions(options: object): void | ||
export function getRequestOptions(): object | ||
export interface Endpoint { | ||
@@ -16,0 +20,0 @@ schemes?: string[]; |
{ | ||
"version": "2.0.0rc2", | ||
"version": "2.0.0", | ||
"name": "oembed-parser", | ||
@@ -27,6 +27,7 @@ "description": "Get oEmbed data from given URL.", | ||
"dependencies": { | ||
"got": "^11.8.3" | ||
"axios": "^0.24.0", | ||
"bellajs": "^10.0.2" | ||
}, | ||
"devDependencies": { | ||
"jest": "^27.4.3", | ||
"jest": "^27.4.5", | ||
"nock": "^13.2.1" | ||
@@ -33,0 +34,0 @@ }, |
134
README.md
@@ -0,1 +1,2 @@ | ||
# oembed-parser | ||
@@ -18,15 +19,24 @@ | ||
### Installation | ||
## Installation | ||
```bash | ||
npm install oembed-parser | ||
$ npm install oembed-parser | ||
# or pnpm | ||
$ pnpm install oembed-parser | ||
# or yarn | ||
$ yarn add oembed-parser | ||
``` | ||
### Usage | ||
## Usage | ||
```js | ||
import { | ||
extract | ||
} from 'oembed-parser' | ||
const { extract } = require('oembed-parser') | ||
// es6 module syntax | ||
import { extract } from 'oembed-parser' | ||
// test | ||
const url = 'https://www.youtube.com/watch?v=8jPQjjsBbIc' | ||
@@ -41,17 +51,12 @@ | ||
### APIs | ||
## APIs | ||
#### .extract(String url [, Object params]) | ||
Extract oEmbed data from specified url. | ||
Return: a Promise | ||
Load and extract oembed data. | ||
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. | ||
Example: | ||
Here is how we can use `oembed-parser` in async/await style: | ||
```js | ||
import { | ||
extract | ||
} from 'oembed-parser' | ||
const { extract } = require('oembed-parser') | ||
@@ -64,2 +69,3 @@ const getOembed = async (url) => { | ||
console.trace(err) | ||
return null | ||
} | ||
@@ -72,10 +78,20 @@ } | ||
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. | ||
#### .hasProvider(String URL) | ||
Return boolean. True if the URL matches with any provider in the list. | ||
Check if a URL matches with any provider in the list. | ||
Examples: | ||
```js | ||
const { hasProvider } = require('oembed-parser') | ||
hasProvider('https://www.youtube.com/watch?v=ciS8aCrX-9s') // return true | ||
hasProvider('https://trello.com/b/BO3bg7yn/notes') // return false | ||
``` | ||
#### .findProvider(String URL) | ||
Return provider which is relevant to given URL. | ||
Get the provider which is relevant to given URL. | ||
@@ -85,42 +101,78 @@ For example: | ||
```js | ||
import { | ||
findProvider | ||
} from 'oembed-parser' | ||
const { findProvider } = require('oembed-parser') | ||
findProvider('https://www.facebook.com/video.php?v=999999999') | ||
``` | ||
// get something like below: | ||
Result looks like below: | ||
// { | ||
// fetchEndpoint: 'https://graph.facebook.com/v10.0/oembed_video', | ||
// providerName: 'Facebook', | ||
// providerUrl: 'https://www.facebook.com/' | ||
// } | ||
```json | ||
{ | ||
fetchEndpoint: 'https://graph.facebook.com/v10.0/oembed_video', | ||
providerName: 'Facebook', | ||
providerUrl: 'https://www.facebook.com/' | ||
} | ||
``` | ||
#### .setProviderList(Array providers) | ||
#### .setProviderList(Array of provider definitions) | ||
Apply a list of providers to use, overriding the [default](https://raw.githubusercontent.com/ndaidong/oembed-parser/master/src/utils/providers.json). | ||
Sets the list of providers to use, overriding the defaults. | ||
This can be useful for whitelisting only certain providers, or for adding | ||
custom providers. | ||
For the expected format, see the | ||
[default list](https://raw.githubusercontent.com/ndaidong/oembed-parser/master/src/utils/providers.json). | ||
Default list of resource providers is synchronized from [oembed.com](http://oembed.com/providers.json). | ||
For example: | ||
### Provider list | ||
```js | ||
const { setProviderList } = require('oembed-parser') | ||
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). | ||
const providers = [ | ||
{ | ||
provider_name: 'Alpha', | ||
provider_url: 'https://alpha.com', | ||
endpoints: [ | ||
// endpoint definition here | ||
] | ||
}, | ||
{ | ||
provider_name: 'Beta', | ||
provider_url: 'https://beta.com', | ||
endpoints: [ | ||
// endpoint definition here | ||
] | ||
} | ||
] | ||
setProviderList(providers) | ||
``` | ||
#### .setRequestOptions(Object requestOptions) | ||
Define options to call oembed HTTP request. | ||
`oembed-parser` is using [axios]() to send HTTP requests. Please refer [axios' request config](https://axios-http.com/docs/req_config) for more info. | ||
Default option: | ||
```js | ||
{ | ||
headers: { | ||
'user-agent': 'Mozilla/5.0 (X11; Linux i686; rv:94.0) Gecko/20100101 Firefox/94.0', | ||
accept: 'application/json; charset=utf-8' | ||
}, | ||
responseType: 'json', | ||
responseEncoding: 'utf8', | ||
timeout: 6e4, | ||
maxRedirects: 3 | ||
} | ||
``` | ||
## Facebook and Instagram | ||
Since October 24 2020, Facebook have deprecated their legacy urls and applied a new Facebook oEmbed endpoints. | ||
Please update your `oembed-parser` version to v1.4.2 or later to be able to extract oembed data from Instagram and Facebook. | ||
Technically, now we have to use Facebook Graph API, with the access token from a valid and live Facebook app. | ||
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: | ||
```bash | ||
@@ -131,7 +183,8 @@ export FACEBOOK_APP_ID=your_app_id | ||
For more info, please refer: | ||
References: | ||
- [oEmbed Read](https://developers.facebook.com/docs/features-reference/oembed-read) | ||
- [Facebook oEmbed](https://developers.facebook.com/docs/plugins/oembed) | ||
- [Facebook Graph API](https://developers.facebook.com/docs/graph-api/overview) | ||
## Test | ||
@@ -149,4 +202,5 @@ | ||
# License | ||
## License | ||
The MIT License (MIT) | ||
The MIT License (MIT) | ||
--- |
@@ -21,2 +21,3 @@ #!/usr/bin/env node | ||
'yarn.lock', | ||
'pnpm-lock.yaml', | ||
'package-lock.json', | ||
@@ -23,0 +24,0 @@ 'coverage.lcov', |
// config | ||
const fetchOptions = { | ||
const { clone, copies } = require('bellajs') | ||
const requestOptions = { | ||
headers: { | ||
@@ -9,8 +11,14 @@ 'user-agent': 'Mozilla/5.0 (X11; Linux i686; rv:94.0) Gecko/20100101 Firefox/94.0', | ||
responseType: 'json', | ||
timeout: 30 * 1e3, | ||
redirect: 'follow' | ||
responseEncoding: 'utf8', | ||
timeout: 6e4, // 1 minute | ||
maxRedirects: 3 | ||
} | ||
module.exports = { | ||
fetchOptions | ||
getRequestOptions: () => { | ||
return clone(requestOptions) | ||
}, | ||
setRequestOptions: (opts) => { | ||
copies(opts, requestOptions) | ||
} | ||
} |
@@ -11,2 +11,6 @@ /** | ||
const { | ||
setRequestOptions | ||
} = require('./config') | ||
const extract = async (url, params = {}) => { | ||
@@ -28,3 +32,4 @@ if (!isValidURL(url)) { | ||
findProvider: provider.find, | ||
setProviderList: provider.set | ||
setProviderList: provider.set, | ||
setRequestOptions | ||
} |
@@ -10,5 +10,10 @@ // main | ||
findProvider, | ||
setProviderList | ||
setProviderList, | ||
setRequestOptions | ||
} = require('./main') | ||
const { | ||
getRequestOptions | ||
} = require('./config') | ||
const required = [ | ||
@@ -255,1 +260,21 @@ 'type', | ||
}) | ||
test('Testing setRequestOptions()', () => { | ||
setRequestOptions({ | ||
headers: { | ||
authorization: 'bearer <token>' | ||
}, | ||
timeout: 20, | ||
somethingElse: 1000 | ||
}) | ||
const actual = getRequestOptions() | ||
const expectedHeader = { | ||
authorization: 'bearer <token>', | ||
'user-agent': 'Mozilla/5.0 (X11; Linux i686; rv:94.0) Gecko/20100101 Firefox/94.0', | ||
accept: 'application/json; charset=utf-8' | ||
} | ||
expect(actual.headers).toEqual(expectedHeader) | ||
expect(actual.timeout).toEqual(20) | ||
}) |
// utils -> retrieve | ||
const got = require('got') | ||
const axios = require('axios') | ||
const { fetchOptions } = require('../config') | ||
const { getRequestOptions } = require('../config') | ||
module.exports = async (url) => { | ||
try { | ||
const { headers, body } = await got(url, fetchOptions) | ||
const res = await axios.get(url, getRequestOptions()) | ||
const contentType = headers['content-type'] || '' | ||
const contentType = res.headers['content-type'] || '' | ||
if (!contentType || !contentType.includes('application/json')) { | ||
@@ -16,3 +16,3 @@ return null | ||
return body | ||
return res.data | ||
} catch (err) { | ||
@@ -19,0 +19,0 @@ return null |
@@ -31,3 +31,3 @@ // retrieve.test | ||
const scope = nock(baseUrl) | ||
scope.get(path).reply(500, { data: { name: 'oembed-parser' } }, { | ||
scope.get(path).reply(500, '', { | ||
'Content-Type': 'application/json' | ||
@@ -34,0 +34,0 @@ }) |
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
233679
9221
0
200
1
2
+ Addedaxios@^0.24.0
+ Addedbellajs@^10.0.2
+ Addedaxios@0.24.0(transitive)
+ Addedbellajs@10.0.2(transitive)
+ Addedfollow-redirects@1.15.9(transitive)
- Removedgot@^11.8.3
- Removed@sindresorhus/is@4.6.0(transitive)
- Removed@szmarczak/http-timer@4.0.6(transitive)
- Removed@types/cacheable-request@6.0.3(transitive)
- Removed@types/http-cache-semantics@4.0.4(transitive)
- Removed@types/keyv@3.1.4(transitive)
- Removed@types/node@22.13.1(transitive)
- Removed@types/responselike@1.0.3(transitive)
- Removedcacheable-lookup@5.0.4(transitive)
- Removedcacheable-request@7.0.4(transitive)
- Removedclone-response@1.0.3(transitive)
- Removeddecompress-response@6.0.0(transitive)
- Removeddefer-to-connect@2.0.1(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedget-stream@5.2.0(transitive)
- Removedgot@11.8.6(transitive)
- Removedhttp-cache-semantics@4.1.1(transitive)
- Removedhttp2-wrapper@1.0.3(transitive)
- Removedjson-buffer@3.0.1(transitive)
- Removedkeyv@4.5.4(transitive)
- Removedlowercase-keys@2.0.0(transitive)
- Removedmimic-response@1.0.13.1.0(transitive)
- Removednormalize-url@6.1.0(transitive)
- Removedonce@1.4.0(transitive)
- Removedp-cancelable@2.1.1(transitive)
- Removedpump@3.0.2(transitive)
- Removedquick-lru@5.1.1(transitive)
- Removedresolve-alpn@1.2.1(transitive)
- Removedresponselike@2.0.1(transitive)
- Removedundici-types@6.20.0(transitive)
- Removedwrappy@1.0.2(transitive)