diff --git a/package-lock.json b/package-lock.json index 8b6c886..ed7494d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,6 @@ "license": "Apache-2.0", "dependencies": { "@prismicio/client": "^7.12.0", - "isomorphic-unfetch": "^3.1.0", "vue-router": "^4.5.0" }, "devDependencies": { @@ -5073,16 +5072,6 @@ "dev": true, "license": "ISC" }, - "node_modules/isomorphic-unfetch": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/isomorphic-unfetch/-/isomorphic-unfetch-3.1.0.tgz", - "integrity": "sha512-geDJjpoZ8N0kWexiwkX8F9NkTsXhetLPVbZFQ+JTW239QNOwvB0gniuR1Wc6f0AMTn7/mFGyXvHTifrCp/GH8Q==", - "license": "MIT", - "dependencies": { - "node-fetch": "^2.6.1", - "unfetch": "^4.2.0" - } - }, "node_modules/istanbul-lib-coverage": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.2.tgz", @@ -6479,48 +6468,6 @@ "dev": true, "license": "MIT" }, - "node_modules/node-fetch": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", - "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", - "license": "MIT", - "dependencies": { - "whatwg-url": "^5.0.0" - }, - "engines": { - "node": "4.x || >=6.0.0" - }, - "peerDependencies": { - "encoding": "^0.1.0" - }, - "peerDependenciesMeta": { - "encoding": { - "optional": true - } - } - }, - "node_modules/node-fetch/node_modules/tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", - "license": "MIT" - }, - "node_modules/node-fetch/node_modules/webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", - "license": "BSD-2-Clause" - }, - "node_modules/node-fetch/node_modules/whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", - "license": "MIT", - "dependencies": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } - }, "node_modules/nopt": { "version": "7.2.1", "resolved": "https://registry.npmjs.org/nopt/-/nopt-7.2.1.tgz", @@ -8179,12 +8126,6 @@ "dev": true, "license": "MIT" }, - "node_modules/unfetch": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/unfetch/-/unfetch-4.2.0.tgz", - "integrity": "sha512-F9p7yYCn6cIW9El1zi0HI6vqpeIvBsr3dSuRO6Xuppb1u5rXpCPmMvLSyECLhybr9isec8Ohl0hPekMVrEinDA==", - "license": "MIT" - }, "node_modules/unist-util-stringify-position": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz", diff --git a/package.json b/package.json index 6b4ac30..5f2ed18 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,6 @@ }, "dependencies": { "@prismicio/client": "^7.12.0", - "isomorphic-unfetch": "^3.1.0", "vue-router": "^4.5.0" }, "devDependencies": { diff --git a/src/PrismicTest.vue b/src/PrismicTest.vue new file mode 100644 index 0000000..aeded83 --- /dev/null +++ b/src/PrismicTest.vue @@ -0,0 +1,9 @@ + + + diff --git a/src/createPrismic.ts b/src/createPrismic.ts index 140418f..cb10691 100644 --- a/src/createPrismic.ts +++ b/src/createPrismic.ts @@ -1,6 +1,5 @@ import type { Client, - FetchLike, HTMLRichTextFunctionSerializer, HTMLRichTextMapSerializer, LinkResolverFunction, @@ -55,19 +54,7 @@ export const createPrismic = (options: PrismicPluginOptions): PrismicPlugin => { if (options.client) { client = options.client } else { - client = createClient(options.endpoint, { - fetch: async (endpoint, options) => { - let fetchFunction: FetchLike - if (typeof globalThis.fetch === "function") { - fetchFunction = globalThis.fetch - } else { - fetchFunction = (await import("isomorphic-unfetch")).default - } - - return await fetchFunction(endpoint, options) - }, - ...options.clientConfig, - }) + client = createClient(options.endpoint, options.clientConfig) } const prismicClient: PrismicPluginClient = { diff --git a/src/index.ts b/src/index.ts index d1583c3..aa49d43 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,7 @@ +import PrismicTest from "./PrismicTest.vue" + +export { PrismicTest } + export { createPrismic } from "./createPrismic" export { usePrismic } from "./usePrismic" diff --git a/test/createPrismic-client.test.ts b/test/createPrismic-client.test.ts index 461d78c..c82838f 100644 --- a/test/createPrismic-client.test.ts +++ b/test/createPrismic-client.test.ts @@ -2,18 +2,11 @@ import { expect, it, vi } from "vitest" import { createClient, getRepositoryEndpoint } from "@prismicio/client" import { mount } from "@vue/test-utils" -import unfetch from "isomorphic-unfetch" import { WrapperComponent } from "./__fixtures__/WrapperComponent" import { createPrismic } from "../src" -vi.mock("isomorphic-unfetch", () => { - return { - default: vi.fn(), - } -}) - it("creates client from repository name", () => { const prismic = createPrismic({ endpoint: "test" }) @@ -67,6 +60,25 @@ it("uses provided client", () => { expect(wrapper.vm.$prismic.client.endpoint).toBe(client.endpoint) }) +it("uses `globalThis` fetch function by default", () => { + const initialFetch = globalThis.fetch + globalThis.fetch = vi.fn() + + const prismic = createPrismic({ endpoint: "test" }) + + const wrapper = mount(WrapperComponent, { + global: { + plugins: [prismic], + }, + }) + + expect(globalThis.fetch).not.toHaveBeenCalled() + wrapper.vm.$prismic.client.fetchFn("foo", {}) + expect(globalThis.fetch).toHaveBeenCalledOnce() + + globalThis.fetch = initialFetch +}) + it("uses provided fetch function", () => { const spiedFetch = vi.fn() @@ -89,36 +101,14 @@ it("uses provided fetch function", () => { expect(spiedFetch).toHaveBeenCalledOnce() }) -it("uses `globalThis` fetch function when available", () => { - // `globalThis.fetch` does not exists in our Node.js context - const fetchStub = (globalThis.fetch = vi.fn()) - - const prismic = createPrismic({ endpoint: "test" }) - - const wrapper = mount(WrapperComponent, { - global: { - plugins: [prismic], - }, - }) - - expect(fetchStub).not.toHaveBeenCalled() - wrapper.vm.$prismic.client.fetchFn("foo", {}) - expect(fetchStub).toHaveBeenCalledOnce() - - // @ts-expect-error `globalThis.fetch` does not exists in our Node.js context +it.only("throws when `globalThis` fetch function is not available and no fetch function is provided", async () => { + const initialFetch = globalThis.fetch + // @ts-expect-error - We're deleting the global fetch function for testing purposes delete globalThis.fetch -}) -it("uses `isomorphic-unfetch` when `globalThis` fetch function is not available", async () => { - const prismic = createPrismic({ endpoint: "test" }) - - const wrapper = mount(WrapperComponent, { - global: { - plugins: [prismic], - }, - }) + expect(() => createPrismic({ endpoint: "test" })).toThrowError( + /a valid fetch implementation was not provided/i, + ) - expect(unfetch).not.toHaveBeenCalled() - await wrapper.vm.$prismic.client.fetchFn("foo", {}) - expect(unfetch).toHaveBeenCalledOnce() + globalThis.fetch = initialFetch })