Skip to content

Commit

Permalink
Merge pull request #113 from tepiton/pb-add-opt-params
Browse files Browse the repository at this point in the history
Allow params besides maxheight & maxwidth
  • Loading branch information
ndaidong committed Dec 19, 2021
2 parents 06fc50d + 3ba1d38 commit 8a3b0e1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ describe('test if extract() with some popular providers', () => {
maxheight = 0
} = params

const result = await extract(url, provider, { maxwidth, maxheight })
// `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)
expect(result.provider_name).toEqual(expected.provider_name)
Expand Down
22 changes: 12 additions & 10 deletions src/utils/fetchEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,25 @@ const fetchEmbed = async (url, provider, params = {}) => {
`url=${encodeURIComponent(url)}`
]

const {
maxwidth = 0,
maxheight = 0
} = params

if (maxwidth > 0) {
queries.push(`maxwidth=${maxwidth}`)
// remove these if they're set to zero
if (params.maxwidth <= 0) {
delete params.maxwidth
}
if (maxheight > 0) {
queries.push(`maxheight=${maxheight}`)
if (params.maxheight <= 0) {
delete params.maxheight
}

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

const query = queries.join('&')
const queryParams = new URLSearchParams(params).toString()

let query = queries.join('&')

if (queryParams) {
query = query + '&' + queryParams
}

const link = getRegularUrl(query, provider.fetchEndpoint)
const body = retrieve(link)
Expand Down
10 changes: 10 additions & 0 deletions src/utils/fetchEmbed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ describe('test if fetchEmbed() works correctly', () => {
type: 'rich'
}
},
{
input: {
url: 'https://twitter.com/ndaidong/status/1173592062878314497?theme=dark',
file: './test-data/twitter-dark.json'
},
expected: {
provider_name: 'Twitter',
type: 'rich'
}
},
{
input: {
url: 'https://www.facebook.com/facebook/videos/10153231379946729/',
Expand Down
13 changes: 13 additions & 0 deletions test-data/twitter-dark.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"url": "https://twitter.com/ndaidong/status/1173592062878314497",
"author_name": "Dong Nguyen",
"author_url": "https://twitter.com/ndaidong",
"html": "<blockquote class=\"twitter-tweet\" data-theme=\"dark\"><p lang=\"en\" dir=\"ltr\">Live event: Release Lotus - New social network in Vietnam<a href=\"https://t.co/BWGejBZRWh\">https://t.co/BWGejBZRWh</a><a href=\"https://twitter.com/hashtag/lotus?src=hash&amp;ref_src=twsrc%5Etfw\">#lotus</a> <a href=\"https://twitter.com/hashtag/SocialNetwork?src=hash&amp;ref_src=twsrc%5Etfw\">#SocialNetwork</a></p>&mdash; Dong Nguyen (@ndaidong) <a href=\"https://twitter.com/ndaidong/status/1173592062878314497?ref_src=twsrc%5Etfw\">September 16, 2019</a></blockquote>\n<script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script>\n",
"width": 550,
"height": null,
"type": "rich",
"cache_age": "3153600000",
"provider_name": "Twitter",
"provider_url": "https://twitter.com",
"version": "1.0"
}

0 comments on commit 8a3b0e1

Please sign in to comment.