Skip to content

Commit

Permalink
fix: throw if an invalid fetch function is given
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Jun 23, 2021
1 parent f255a27 commit ec01c59
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,13 @@ export class Client {
this.queryContentFromRef(options.ref);
}

if (options.fetch) {
if (typeof options.fetch === "function") {
this.fetchFn = options.fetch;
} else if (typeof globalThis.fetch === "function") {
this.fetchFn = globalThis.fetch;
} else {
throw new Error(
"A fetch implementation was not provided. In environments where fetch is not available (including Node.js), a fetch implementation must be provided via a polyfill or the `fetch` option."
"A valid fetch implementation was not provided. In environments where fetch is not available (including Node.js), a fetch implementation must be provided via a polyfill or the `fetch` option."
);
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ test("constructor throws if fetch is unavailable", t => {
});
});

test("constructor throws if provided fetch is not a function", t => {
const endpoint = prismic.getEndpoint("qwerty");
const fetch = "not a function";

t.throws(
() =>
prismic.createClient(endpoint, {
// We wouldn't normally test for input types since TypeScript handles
// that for us at build time, but we want to provide a nicer DX for
// non-TypeScript users.
// @ts-expect-error - We are purposly providing an invalid type to test if it throws.
fetch
}),
{
message: /fetch implementation was not provided/
}
);
});

test("uses globalThis.fetch if available", t => {
const endpoint = prismic.getEndpoint("qwerty");

Expand Down

0 comments on commit ec01c59

Please sign in to comment.