Skip to content

Commit

Permalink
v2.0.1
Browse files Browse the repository at this point in the history
- Merge pr#113 by @pborenstein
  - Simplify params/queries merging
- Update sync scripts
  - Sync to the latest provider list
- Update README
  • Loading branch information
ndaidong committed Dec 19, 2021
1 parent 8a3b0e1 commit 563d93e
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 600 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,25 @@ const getOembed = async (url) => {
return oembed
} catch (err) {
console.trace(err)
return null
return null
}
}

const data = getOembed('your url')
console.log(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.
Optional argument `params` can be useful when you want to specify some additional customizations.

Here are several popular params:

- `maxwidth`: max width of embed size
- `maxheight`: max height of embed size
- `theme`: e.g, `dark` or `light`
- `lang`: e.g, 'en', 'fr', 'cn', 'vi', etc

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)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.0.0",
"version": "2.0.1",
"name": "oembed-parser",
"description": "Get oEmbed data from given URL.",
"homepage": "https://www.npmjs.com/package/oembed-parser",
Expand Down
2 changes: 0 additions & 2 deletions src/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ describe('test if extract() with some popular providers', () => {
maxheight = 0
} = params

// `extract()` takes 2 args: url & params but not provider
// const result = await extract(url, provider, { maxwidth, maxheight })
const result = await extract(url, { maxwidth, maxheight })
expect(result).toBeTruthy()
expect(checkFn(result)).toBe(true)
Expand Down
34 changes: 14 additions & 20 deletions src/utils/fetchEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,34 @@ const getFacebookGraphToken = () => {
const appId = env.FACEBOOK_APP_ID || '845078789498971'
const clientToken = env.FACEBOOK_CLIENT_TOKEN || '8ff3ab4ddd45b8f018b35c4fb7edac62'

return `access_token=${appId}|${clientToken}`
return `${appId}|${clientToken}`
}

const getRegularUrl = (query, basseUrl) => {
return basseUrl.replace(/\{format\}/g, 'json') + '?' + query
}

const fetchEmbed = async (url, provider, params = {}) => {
const queries = [
'format=json',
`url=${encodeURIComponent(url)}`
]

// remove these if they're set to zero
if (params.maxwidth <= 0) {
delete params.maxwidth
const query = {
url,
format: 'json',
...params
}
if (params.maxheight <= 0) {
delete params.maxheight

if (query.maxwidth <= 0) {
delete query.maxwidth
}
if (query.maxheight <= 0) {
delete query.maxheight
}

if (isFacebookGraphDependent(provider.providerUrl)) {
queries.push(getFacebookGraphToken())
query.access_token = getFacebookGraphToken()
}

const queryParams = new URLSearchParams(params).toString()

let query = queries.join('&')

if (queryParams) {
query = query + '&' + queryParams
}
const queryParams = new URLSearchParams(query).toString()

const link = getRegularUrl(query, provider.fetchEndpoint)
const link = getRegularUrl(queryParams, provider.fetchEndpoint)
const body = retrieve(link)
return body
}
Expand Down
Loading

0 comments on commit 563d93e

Please sign in to comment.