Skip to content

Commit

Permalink
Pass options to fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
fabon-f committed Dec 10, 2023
1 parent da457db commit f8742ad
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ Fetch target site and scrape metadata. This function send multiple requests to d

`suppressAdditionalRequest` option suppress this behavior. With `suppressAdditionalRequest`option, this function fetches only the specified URL and reduces processing time, but provides only information which can be extracted from the specified page.

You can also pass the options for `fetch` function.

```ts
type Options = {
suppressAdditionalRequest?: boolean
}
} & RequestInit

type ImageInfo = {
src: string
Expand All @@ -65,3 +67,8 @@ type Metadata = {
image: ImageInfo | undefined
}
```
```ts
// example using fetch options
fetchSiteMetadata('https://example.test', { headers: { 'User-Agent': 'bot' } })
```
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,33 @@ const fetchImageInfo = async (info: ImageInfo) => {
}
}

export type Options = {
type IntrinsicOptions = {
suppressAdditionalRequest?: boolean
}

export type Options = IntrinsicOptions & Parameters<typeof fetch>[1]

export type {
Metadata,
ImageInfo
}

function fetchOptions(options: Options): RequestInit {
const res = {} as Record<string, any>
const optionKeys = ['suppressAdditionalRequest']
for (const [k, v] of Object.entries(options)) {
if (!optionKeys.includes(k)) {
res[k] = v
}
}
return res
}

export default async function fetchSiteMetadata(url: string | URL, options: Options = {}): Promise<Metadata> {
const urlString = typeof url === 'string' ? url : url.toString()
const controller = new AbortController()
const response = await fetch(urlString, {
...fetchOptions(options),
signal: controller.signal
})
if (!response.body) { throw new Error('response.body') }
Expand Down
15 changes: 15 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const testServer = createServer(async (req, res) => {
res.end('</div></html>')
return
}
if (req.url === '/header-test') {
if (req.headers['test'] === 'true') {
res.end('<title>title</title>')
} else {
res.end('<title>error</title>')
}
return
}
try {
const file = await readFile(join('./test/fixtures/', req.url))
if (req.url.endsWith('.html')) {
Expand Down Expand Up @@ -141,6 +149,13 @@ test('Deprecated icon rel', async t => {
t.is(res.icon, new URL('/favicon2.ico', url).toString());
})

test('Passing fetch options', async t => {
const res = await fetchSiteMetadata(new URL('/header-test', url), {
headers: { 'test': 'true' }
})
t.is(res.title, 'title')
})

test.after(() => {
testServer.close()
testServerWithoutFavicon.close()
Expand Down

0 comments on commit f8742ad

Please sign in to comment.