Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect custom fetch implementation #108

Merged
merged 1 commit into from
May 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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