diff --git a/README.md b/README.md index 7cc46df..86d8cd0 100644 --- a/README.md +++ b/README.md @@ -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 @@ -65,3 +67,8 @@ type Metadata = { image: ImageInfo | undefined } ``` + +```ts +// example using fetch options +fetchSiteMetadata('https://example.test', { headers: { 'User-Agent': 'bot' } }) +``` diff --git a/src/index.ts b/src/index.ts index 8f01c13..51329cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -61,19 +61,33 @@ const fetchImageInfo = async (info: ImageInfo) => { } } -export type Options = { +type IntrinsicOptions = { suppressAdditionalRequest?: boolean } +export type Options = IntrinsicOptions & Parameters[1] + export type { Metadata, ImageInfo } +function fetchOptions(options: Options): RequestInit { + const res = {} as Record + 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 { 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') } diff --git a/test/index.ts b/test/index.ts index 87e34b4..cb0e369 100644 --- a/test/index.ts +++ b/test/index.ts @@ -13,6 +13,14 @@ const testServer = createServer(async (req, res) => { res.end('') return } + if (req.url === '/header-test') { + if (req.headers['test'] === 'true') { + res.end('title') + } else { + res.end('error') + } + return + } try { const file = await readFile(join('./test/fixtures/', req.url)) if (req.url.endsWith('.html')) { @@ -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()