Skip to content

Commit

Permalink
Merge a7a7a3c into 66d3aad
Browse files Browse the repository at this point in the history
  • Loading branch information
ndaidong committed Jul 28, 2022
2 parents 66d3aad + a7a7a3c commit 02c74b9
Show file tree
Hide file tree
Showing 19 changed files with 781 additions and 364 deletions.
33 changes: 9 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,9 @@ extract(url).then((oembed) => {
})
```

##### Note:

> Since Node.js v14, ECMAScript modules [have became the official standard format](https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_modules_ecmascript_modules).
> Just ensure that you are [using module system](https://nodejs.org/api/packages.html#determining-module-system) and enjoy with ES6 import/export syntax.

## APIs

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

Load and extract oembed data.

Expand Down Expand Up @@ -90,7 +84,7 @@ Here are several popular params:
Note that some params are supported by these providers but not by the others.
Please see the provider's oEmbed API docs carefully for exact information.

#### .hasProvider(String URL)
### `.hasProvider(String URL)`

Check if a URL matches with any provider in the list.

Expand All @@ -103,7 +97,7 @@ hasProvider('https://www.youtube.com/watch?v=ciS8aCrX-9s') // return true
hasProvider('https://trello.com/b/BO3bg7yn/notes') // return false
```

#### .findProvider(String URL)
### `.findProvider(String URL)`

Get the provider which is relevant to given URL.

Expand All @@ -125,7 +119,7 @@ Result looks like below:
}
```

#### .setProviderList(Array providers)
### `.setProviderList(Array providers)`

Apply a list of providers to use, overriding the [default](https://raw.githubusercontent.com/ndaidong/oembed-parser/master/src/utils/providers.json).

Expand Down Expand Up @@ -159,26 +153,17 @@ const providers = [
setProviderList(providers)
```

#### .setRequestOptions(Object requestOptions)
### `.setRequestOptions(Object requestOptions)`

Define options to call oembed HTTP request.

`oembed-parser` is using [axios](https://github.com/axios/axios) to send HTTP requests. Please refer [axios' request config](https://axios-http.com/docs/req_config) for more info.

Default option:
### `.getRequestOptions()`

```js
{
headers: {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0',
accept: 'application/json; charset=utf-8'
},
responseType: 'json',
responseEncoding: 'utf8',
timeout: 6e4,
maxRedirects: 3
}
```
Return current request options.

Default values can be found [here](https://github.com/ndaidong/oembed-parser/blob/main/src/config.js#L5).

## Facebook and Instagram

Expand Down
913 changes: 699 additions & 214 deletions dist/cjs/oembed-parser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cjs/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "oembed-parser-cjs",
"version": "3.0.7",
"version": "3.0.8",
"main": "./oembed-parser.js"
}
2 changes: 1 addition & 1 deletion dist/cjs/providers.latest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// provider data, synchronized at 2022-07-24T14:03:37.993Z
// provider data, synchronized at 2022-07-28T14:44:04.385Z

/* eslint-disable */

Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.0.7",
"version": "3.0.8",
"name": "oembed-parser",
"description": "Get oEmbed data from given URL.",
"homepage": "https://www.npmjs.com/package/oembed-parser",
Expand All @@ -17,9 +17,9 @@
"scripts": {
"lint": "standard .",
"pretest": "npm run lint",
"test": " NODE_OPTIONS=--experimental-vm-modules jest --verbose --coverage=true --unhandled-rejections=strict --detectOpenHandles",
"eval": "DEBUG=oembed-parser:* node eval",
"eval:cjs": "DEBUG=oembed-parser:* node cjs-eval",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --verbose --coverage=true",
"eval": "node eval",
"eval:cjs": "node cjs-eval",
"sync": "node sync",
"tsc": "tsc --init",
"build-ts": "tsc",
Expand All @@ -29,15 +29,15 @@
"dependencies": {
"axios": "^0.27.2",
"bellajs": "^11.0.3",
"debug": "^4.3.4"
"tldts": "^5.7.84"
},
"standard": {
"ignore": [
"/dist"
]
},
"devDependencies": {
"esbuild": "^0.14.49",
"esbuild": "^0.14.50",
"jest": "^28.1.3",
"nock": "^13.2.9"
},
Expand Down
5 changes: 4 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export const extract = async (url, params = {}) => {
return data
}

export { setRequestOptions } from './config.js'
export {
setRequestOptions,
getRequestOptions
} from './config.js'

export {
find as findProvider,
Expand Down
3 changes: 2 additions & 1 deletion src/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('test if extract() with some popular providers', () => {

cases.forEach(({ input, expected, checkFn }) => {
const { url, file, params = {} } = input
test(` check fetchEmbed("${url}")`, async () => {
test(`check fetchEmbed("${url}")`, async () => {
const provider = findProvider(url)
const { baseUrl, path } = parseUrl(provider.fetchEndpoint)

Expand Down Expand Up @@ -228,6 +228,7 @@ describe('test if extract() with some popular providers', () => {
if (maxheight > 0) {
expect(result.height).toBeLessThanOrEqual(expected.maxheight)
}
nock.cleanAll()
})
})
})
Expand Down
4 changes: 0 additions & 4 deletions src/utils/fetchEmbed.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// utils -> fetchEmbed

import retrieve from './retrieve.js'
import { warning } from './logger.js'

const isFacebookGraphDependent = (url) => {
return url.includes('facebook.com') || url.includes('instagram.com')
Expand All @@ -11,9 +10,6 @@ const getFacebookGraphToken = () => {
const env = process.env || {}
const appId = env.FACEBOOK_APP_ID
const clientToken = env.FACEBOOK_CLIENT_TOKEN
if (!appId || !clientToken) {
warning('Working with Facebook Graph requires `FACEBOOK_APP_ID` and `FACEBOOK_CLIENT_TOKEN`')
}
return `${appId}|${clientToken}`
}

Expand Down
3 changes: 2 additions & 1 deletion src/utils/fetchEmbed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('test if fetchEmbed() works correctly', () => {

cases.forEach(({ input, expected }) => {
const { url, file, params = {} } = input
test(` check fetchEmbed("${url}")`, async () => {
test(`check fetchEmbed("${url}")`, async () => {
const provider = findProvider(url)
const { baseUrl, path } = parseUrl(provider.fetchEndpoint)

Expand Down Expand Up @@ -122,6 +122,7 @@ describe('test if fetchEmbed() works correctly', () => {
if (maxheight > 0) {
expect(result.height).toBeLessThanOrEqual(expected.maxheight)
}
nock.cleanAll()
})
})
})
10 changes: 0 additions & 10 deletions src/utils/getDomain.js

This file was deleted.

43 changes: 0 additions & 43 deletions src/utils/getDomain.test.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/isValidURL.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('test isValidURL()', () => {
}
]
cases.forEach(({ url, expected }) => {
test(` isValidURL("${url}") must return "${expected}"`, () => {
test(`isValidURL("${url}") must return "${expected}"`, () => {
const result = isValidURL(url)
expect(result).toEqual(expected)
})
Expand Down
15 changes: 0 additions & 15 deletions src/utils/logger.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/utils/provider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// utils / provider

import { getDomain } from 'tldts'

import isValidURL from './isValidURL.js'
import getDomain from './getDomain.js'

import { providers as defaultProviderList } from './providers.latest.js'

Expand Down
12 changes: 6 additions & 6 deletions src/utils/provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ describe('test if provider.find() works correctly', () => {
]

cases.forEach(({ url, fetchEndpoint }) => {
test(` provider.find("${url}") must return "${fetchEndpoint}"`, () => {
test(`provider.find("${url}") must return "${fetchEndpoint}"`, () => {
const foundedProvider = provider.find(url)
expect(foundedProvider.fetchEndpoint).toEqual(fetchEndpoint)
})
})

test(' provider.find("{}") must return null', () => {
test('provider.find("{}") must return null', () => {
expect(provider.find({})).toEqual(null)
})

test(' provider.find("abcdef") must return null', () => {
test('provider.find("abcdef") must return null', () => {
expect(provider.find('abcdef')).toEqual(null)
})

test(' provider.find("https://somethingdoesnotexist.com") must return null', () => {
test('provider.find("https://somethingdoesnotexist.com") must return null', () => {
expect(provider.find('https://somethingdoesnotexist.com')).toEqual(null)
})
})
Expand All @@ -68,10 +68,10 @@ describe('test if provider set/get works correctly', () => {
endpoints: []
}
]
test(' provider.set()', () => {
test('provider.set()', () => {
expect(provider.set(providerList)).toEqual(providerList.length)
})
test(' provider.get()', () => {
test('provider.get()', () => {
expect(provider.get()).toEqual(providerList)
})
})
2 changes: 1 addition & 1 deletion src/utils/providers.latest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// provider data, synchronized at 2022-07-24T14:03:37.993Z
// provider data, synchronized at 2022-07-28T14:44:04.385Z

/* eslint-disable */

Expand Down
11 changes: 8 additions & 3 deletions src/utils/providers.prev.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// provider data, synchronized at 2022-07-04T04:38:20.547Z
// provider data, synchronized at 2022-07-24T14:03:37.993Z

/* eslint-disable */

Expand Down Expand Up @@ -227,9 +227,13 @@ export const providers = [
"schemes": [
"https://audioboom.com/channels/*",
"https://audioboom.com/channel/*",
"https://audioboom.com/posts/*"
"https://audioboom.com/playlists/*",
"https://audioboom.com/podcasts/*",
"https://audioboom.com/podcast/*",
"https://audioboom.com/posts/*",
"https://audioboom.com/episodes/*"
],
"url": "https://audioboom.com/publishing/oembed/v4.{format}",
"url": "https://audioboom.com/publishing/oembed.{format}",
"formats": [
"json",
"xml"
Expand Down Expand Up @@ -3259,6 +3263,7 @@ export const providers = [
"schemes": [
"http://soundcloud.com/*",
"https://soundcloud.com/*",
"https://on.soundcloud.com/*",
"https://soundcloud.app.goog.gl/*"
],
"url": "https://soundcloud.com/oembed"
Expand Down
8 changes: 3 additions & 5 deletions src/utils/retrieve.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import axios from 'axios'

import { getRequestOptions } from '../config.js'
import { error } from './logger.js'

const isValidContentType = (ctype) => {
return [
'application/json',
'application/javascript',
'text/javascript'
].some((item) => {
return ctype.includes(item)
Expand All @@ -19,12 +19,10 @@ export default async (url) => {
const res = await axios.get(url, getRequestOptions())
const contentType = res.headers['content-type'] || ''
if (!isValidContentType(contentType)) {
error(`Invalid content type! (${contentType})`)
return null
throw new Error(`Invalid content type: "${contentType}"`)
}
return res.data
} catch (err) {
error(err.message)
return null
throw new Error(`${err.name}: ${err.message}`)
}
}
Loading

0 comments on commit 02c74b9

Please sign in to comment.