From ec01c598c8a79049f843e9d47a3abd83043dd469 Mon Sep 17 00:00:00 2001 From: Angelo Ashmore Date: Tue, 22 Jun 2021 14:26:00 -1000 Subject: [PATCH] fix: throw if an invalid fetch function is given --- src/client.ts | 4 ++-- test/client.test.ts | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index 7070b763..8fe4f852 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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." ); } } diff --git a/test/client.test.ts b/test/client.test.ts index 259f9c5b..fe6ae245 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -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");