Skip to content

Commit

Permalink
fix: remove make-fetch-happen
Browse files Browse the repository at this point in the history
BREAKING CHANGE: make-fetch-happen would respect cache headers by saving responses in the file system. We no longer use make-fetch-happen and instead use the built-in fetch machanism on your system which could potentially result in no caching depending on your global fetch.

Closes #2
  • Loading branch information
kentcdodds committed May 22, 2024
1 parent 6bbad29 commit f5a6843
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
48 changes: 24 additions & 24 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,39 @@ const unquoteSerializer = {
expect.addSnapshotSerializer(unquoteSerializer)

const server = setupServer(
http.get('http://oembed.com/providers.json', () =>
http.get('https://oembed.com/providers.json', () =>
HttpResponse.json([
{
provider_name: 'Beautiful.AI',
provider_url: 'http://www.beautiful.ai',
provider_url: 'https://www.beautiful.ai',
endpoints: [
{
url: 'http://www.beautiful.ai/api/oembed',
url: 'https://www.beautiful.ai/api/oembed',
// no scheme 😱
discovery: true,
},
],
},
{
provider_name: 'Twitter',
provider_url: 'http://www.twitter.com',
provider_url: 'https://www.twitter.com',
endpoints: [
{
schemes: [
'http://twitter.com/*/status/*',
'http://*.twitter.com/*/status/*',
'http://twitter.com/*/moments/*',
'http://*.twitter.com/*/moments/*',
'https://twitter.com/*/status/*',
'https://*.twitter.com/*/status/*',
'https://twitter.com/*/moments/*',
'https://*.twitter.com/*/moments/*',
],
url: 'http://publish.twitter.com/oembed',
url: 'https://publish.twitter.com/oembed',
},
],
},
]),
),
http.get('http://publish.twitter.com/oembed', () =>
http.get('https://publish.twitter.com/oembed', () =>
HttpResponse.json({
html: '<blockquote class="twitter-tweet" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">I spent a few minutes working on this, just for you all. I promise, it wont disappoint. Though it may surprise 🎉<br><br>🙏 <a href="http://t.co/wgTJYYHOzD">http://t.co/wgTJYYHOzD</a></p>— Kent C. Dodds (@kentcdodds) <a href="http://twitter.com/kentcdodds/status/783161196945944580?ref_src=twsrc%5Etfw">October 4, 2016</a></blockquote>',
html: '<blockquote class="twitter-tweet" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">I spent a few minutes working on this, just for you all. I promise, it wont disappoint. Though it may surprise 🎉<br><br>🙏 <a href="https://t.co/wgTJYYHOzD">https://t.co/wgTJYYHOzD</a></p>— Kent C. Dodds (@kentcdodds) <a href="https://twitter.com/kentcdodds/status/783161196945944580?ref_src=twsrc%5Etfw">October 4, 2016</a></blockquote>',
}),
),
)
Expand All @@ -74,19 +74,19 @@ test('smoke test', async () => {
`
Here's a great tweet:
http://twitter.com/kentcdodds/status/783161196945944580
https://twitter.com/kentcdodds/status/783161196945944580
And here's an example of no provider:
http://example.com
https://example.com
`.trim(),
)

expect(result.toString()).toMatchInlineSnapshot(`
<p>Here's a great tweet:</p>
<blockquote class="twitter-tweet" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">I spent a few minutes working on this, just for you all. I promise, it wont disappoint. Though it may surprise 🎉<br><br>🙏 <a href="http://t.co/wgTJYYHOzD">http://t.co/wgTJYYHOzD</a></p>— Kent C. Dodds (@kentcdodds) <a href="http://twitter.com/kentcdodds/status/783161196945944580?ref_src=twsrc%5Etfw">October 4, 2016</a></blockquote>
<blockquote class="twitter-tweet" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">I spent a few minutes working on this, just for you all. I promise, it wont disappoint. Though it may surprise 🎉<br><br>🙏 <a href="https://t.co/wgTJYYHOzD">https://t.co/wgTJYYHOzD</a></p>— Kent C. Dodds (@kentcdodds) <a href="https://twitter.com/kentcdodds/status/783161196945944580?ref_src=twsrc%5Etfw">October 4, 2016</a></blockquote>
<p>And here's an example of no provider:</p>
<p>http://example.com</p>
<p>https://example.com</p>
`)
})

Expand All @@ -96,10 +96,10 @@ test('no config required', async () => {
transformers: [transformer],
})
.use(remarkHTML, {sanitize: false})
.process(`http://twitter.com/kentcdodds/status/783161196945944580`)
.process(`https://twitter.com/kentcdodds/status/783161196945944580`)

expect(result.toString()).toMatchInlineSnapshot(
`<blockquote class="twitter-tweet" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">I spent a few minutes working on this, just for you all. I promise, it wont disappoint. Though it may surprise 🎉<br><br>🙏 <a href="http://t.co/wgTJYYHOzD">http://t.co/wgTJYYHOzD</a></p>— Kent C. Dodds (@kentcdodds) <a href="http://twitter.com/kentcdodds/status/783161196945944580?ref_src=twsrc%5Etfw">October 4, 2016</a></blockquote>`,
`<blockquote class="twitter-tweet" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">I spent a few minutes working on this, just for you all. I promise, it wont disappoint. Though it may surprise 🎉<br><br>🙏 <a href="https://t.co/wgTJYYHOzD">https://t.co/wgTJYYHOzD</a></p>— Kent C. Dodds (@kentcdodds) <a href="https://twitter.com/kentcdodds/status/783161196945944580?ref_src=twsrc%5Etfw">October 4, 2016</a></blockquote>`,
)
})

Expand All @@ -112,10 +112,10 @@ test('config can be a function', async () => {
transformers: [[transformer, config]],
})
.use(remarkHTML, {sanitize: false})
.process(`http://twitter.com/kentcdodds/status/783161196945944580`)
.process(`https://twitter.com/kentcdodds/status/783161196945944580`)

expect(result.toString()).toMatchInlineSnapshot(
`<blockquote class="twitter-tweet" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">I spent a few minutes working on this, just for you all. I promise, it wont disappoint. Though it may surprise 🎉<br><br>🙏 <a href="http://t.co/wgTJYYHOzD">http://t.co/wgTJYYHOzD</a></p>— Kent C. Dodds (@kentcdodds) <a href="http://twitter.com/kentcdodds/status/783161196945944580?ref_src=twsrc%5Etfw">October 4, 2016</a></blockquote>`,
`<blockquote class="twitter-tweet" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">I spent a few minutes working on this, just for you all. I promise, it wont disappoint. Though it may surprise 🎉<br><br>🙏 <a href="https://t.co/wgTJYYHOzD">https://t.co/wgTJYYHOzD</a></p>— Kent C. Dodds (@kentcdodds) <a href="https://twitter.com/kentcdodds/status/783161196945944580?ref_src=twsrc%5Etfw">October 4, 2016</a></blockquote>`,
)
})

Expand All @@ -126,18 +126,18 @@ test('config function does not need to return anything', async () => {
transformers: [[transformer, config]],
})
.use(remarkHTML, {sanitize: false})
.process(`http://twitter.com/kentcdodds/status/783161196945944580`)
.process(`https://twitter.com/kentcdodds/status/783161196945944580`)

expect(result.toString()).toMatchInlineSnapshot(
`<blockquote class="twitter-tweet" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">I spent a few minutes working on this, just for you all. I promise, it wont disappoint. Though it may surprise 🎉<br><br>🙏 <a href="http://t.co/wgTJYYHOzD">http://t.co/wgTJYYHOzD</a></p>— Kent C. Dodds (@kentcdodds) <a href="http://twitter.com/kentcdodds/status/783161196945944580?ref_src=twsrc%5Etfw">October 4, 2016</a></blockquote>`,
`<blockquote class="twitter-tweet" data-dnt="true" data-theme="dark"><p lang="en" dir="ltr">I spent a few minutes working on this, just for you all. I promise, it wont disappoint. Though it may surprise 🎉<br><br>🙏 <a href="https://t.co/wgTJYYHOzD">https://t.co/wgTJYYHOzD</a></p>— Kent C. Dodds (@kentcdodds) <a href="https://twitter.com/kentcdodds/status/783161196945944580?ref_src=twsrc%5Etfw">October 4, 2016</a></blockquote>`,
)
})

test('sends the correct search params', async () => {
let request: Request

server.use(
http.get('http://publish.twitter.com/oembed', ({request: req}) => {
http.get('https://publish.twitter.com/oembed', ({request: req}) => {
request = req
return HttpResponse.json({
html: 'whatever',
Expand All @@ -155,10 +155,10 @@ test('sends the correct search params', async () => {
],
})
.use(remarkHTML, {sanitize: false})
.process(`http://twitter.com/kentcdodds/status/783161196945944580`)
.process(`https://twitter.com/kentcdodds/status/783161196945944580`)

// @ts-expect-error it doesn't think request will be assigned by now... But it will!
expect(request.url.toString()).toMatchInlineSnapshot(
`http://publish.twitter.com/oembed?url=http%3A%2F%2Ftwitter.com%2Fkentcdodds%2Fstatus%2F783161196945944580&dnt=true&omit_script=true&theme=dark&format=json`,
`https://publish.twitter.com/oembed?url=https%3A%2F%2Ftwitter.com%2Fkentcdodds%2Fstatus%2F783161196945944580&dnt=true&omit_script=true&theme=dark&format=json`,
)
})
11 changes: 1 addition & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import path from 'path'
import {URL} from 'url'
import {type Transformer} from '@remark-embedder/core'
import fetch from 'make-fetch-happen'

fetch.defaults({
cachePath: path.join(
process.cwd(),
'node_modules/.cache/@remark-embedder/transformer-oembed/fetch',
),
})

type Provider = {
provider_name: string
Expand All @@ -28,7 +19,7 @@ declare namespace getProviders {

async function getProviders(): Promise<Providers> {
if (!getProviders.cache) {
const res = await fetch('http://oembed.com/providers.json')
const res = await fetch('https://oembed.com/providers.json')
getProviders.cache = (await res.json()) as Providers
}

Expand Down

0 comments on commit f5a6843

Please sign in to comment.