Skip to content

Commit

Permalink
feat!: remove isomorphic-unfetch fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Dec 4, 2024
1 parent c02475b commit e889af1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 110 deletions.
59 changes: 0 additions & 59 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
},
"dependencies": {
"@prismicio/client": "^7.12.0",
"isomorphic-unfetch": "^3.1.0",
"vue-router": "^4.5.0"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions src/PrismicTest.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script setup lang="ts">
import { ref } from "vue"
const hello = ref("hello world")
</script>

<template>
<div>{{ hello }}</div>
</template>
15 changes: 1 addition & 14 deletions src/createPrismic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {
Client,
FetchLike,
HTMLRichTextFunctionSerializer,
HTMLRichTextMapSerializer,
LinkResolverFunction,
Expand Down Expand Up @@ -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 = {
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import PrismicTest from "./PrismicTest.vue"

export { PrismicTest }

export { createPrismic } from "./createPrismic"
export { usePrismic } from "./usePrismic"

Expand Down
62 changes: 26 additions & 36 deletions test/createPrismic-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" })

Expand Down Expand Up @@ -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()

Expand All @@ -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
})

0 comments on commit e889af1

Please sign in to comment.