From 8b0f9d531299fe22feb29a3c2d3cf7a76d606223 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 11 May 2023 23:32:43 -0500 Subject: [PATCH] Respect custom fetch implementation --- src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 173e200..1ed703e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { URL } from "url"; import { Parser } from "htmlparser2"; -import fetch from "node-fetch"; +import nodeFetch from "node-fetch"; import UnexpectedError from "./unexpectedError"; import { schema, keys } from "./schema"; import { Metadata, Opts } from "./types"; @@ -41,14 +41,14 @@ function unfurl(url: string, opts?: Opts): Promise { return getPage(url, opts) .then(getMetadata(url, opts)) - .then(getRemoteMetadata(url)) + .then(getRemoteMetadata(url, opts)) .then(parse(url)); } async function getPage(url: string, opts: Opts) { const res = await (opts.fetch ? opts.fetch(url) - : fetch(new URL(url), { + : nodeFetch(new URL(url), { headers: opts.headers, size: opts.size, follow: opts.follow, @@ -122,7 +122,7 @@ async function getPage(url: string, opts: Opts) { return buf.toString(); } -function getRemoteMetadata(url: string) { +function getRemoteMetadata(url: string, { fetch = nodeFetch }: Opts) { return async function ({ oembed, metadata }) { if (!oembed) { return metadata;