Skip to content

Commit

Permalink
fix: respect custom fetch implementation (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgleason committed May 12, 2023
1 parent 0b5197d commit 607f295
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions 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";
Expand Down Expand Up @@ -41,14 +41,14 @@ function unfurl(url: string, opts?: Opts): Promise<Metadata> {

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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 607f295

Please sign in to comment.