From 3a213a7706ab4baf942c2ce1f1f49019677aef9a Mon Sep 17 00:00:00 2001 From: Remco Lakens Date: Thu, 20 Feb 2025 22:41:13 +0100 Subject: [PATCH 1/2] feat: override credentials --- .gitignore | 1 + src/client.test.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++- src/client.ts | 10 ++++++-- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 3bdd52e..626c31d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ dist/ .DS_Store +test-reports \ No newline at end of file diff --git a/src/client.test.ts b/src/client.test.ts index a1983d4..5c6ed7f 100644 --- a/src/client.test.ts +++ b/src/client.test.ts @@ -1,3 +1,4 @@ +import { createSha256 } from "helpers"; import { afterAll, beforeAll, @@ -10,7 +11,6 @@ import { import createFetchMock from "vitest-fetch-mock"; import { initClientFetcher } from "./client"; import { TypedDocumentString } from "./testing"; -import { createSha256 } from "helpers"; const query = new TypedDocumentString(/* GraphQL */ ` query myQuery { @@ -98,6 +98,7 @@ describe("gqlClientFetch", () => { }, ); }); + it("should perform a mutation", async () => { const mockedFetch = fetchMock.mockResponse(responseString); const gqlResponse = await fetcher(mutation, { @@ -196,6 +197,64 @@ describe("gqlClientFetch", () => { expect(fetchMock).toHaveBeenCalledTimes(1); }); + it("should use 'omit' credentials when provided", async () => { + const fetcher = initClientFetcher("https://localhost/graphql", { + defaultCredentials: "omit", + }); + fetchMock.mockResponse(responseString); + + const gqlResponse = await fetcher(query, { + myVar: "baz", + }); + + expect(gqlResponse).toEqual(response); + + expect(fetchMock).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + credentials: "omit", + }), + ); + }); + + it("should use 'same-origin' credentials when provided", async () => { + const fetcher = initClientFetcher("https://localhost/graphql", { + defaultCredentials: "same-origin", + }); + fetchMock.mockResponse(responseString); + + const gqlResponse = await fetcher(query, { + myVar: "baz", + }); + + expect(gqlResponse).toEqual(response); + + expect(fetchMock).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + credentials: "same-origin", + }), + ); + }); + + it("should use 'include' credentials when provided", async () => { + const fetcher = initClientFetcher("https://localhost/graphql"); + fetchMock.mockResponse(responseString); + + const gqlResponse = await fetcher(query, { + myVar: "baz", + }); + + expect(gqlResponse).toEqual(response); + + expect(fetchMock).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + credentials: "include", + }), + ); + }); + it("should use the provided signal", async () => { const fetcher = initClientFetcher("https://localhost/graphql"); fetchMock.mockResponse(responseString); diff --git a/src/client.ts b/src/client.ts index a8b0a11..2ebf744 100644 --- a/src/client.ts +++ b/src/client.ts @@ -45,6 +45,11 @@ type Options = { */ includeQuery?: boolean; + /** + * Default credentials to be sent with each request + */ + defaultCredentials?: RequestCredentials; + /** * Function to customize creating the documentId from a query * @@ -75,6 +80,7 @@ export const initClientFetcher = defaultTimeout = 30000, defaultHeaders = {}, includeQuery = false, + defaultCredentials = "include", createDocumentId = getDocumentId, }: Options = {}, ): ClientFetcher => @@ -119,7 +125,7 @@ export const initClientFetcher = fetch(url.toString(), { headers: headers, method: "GET", - credentials: "include", + credentials: defaultCredentials, signal: options.signal, }), ); @@ -140,7 +146,7 @@ export const initClientFetcher = headers: headers, method: "POST", body: createRequestBody(request), - credentials: "include", + credentials: defaultCredentials, signal: options.signal, }), ); From ae80ddf2d067e6795840019b46a919bd9b062a8f Mon Sep 17 00:00:00 2001 From: Remco Lakens Date: Thu, 20 Feb 2025 22:45:19 +0100 Subject: [PATCH 2/2] chore: changeset --- .changeset/forty-paws-refuse.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/forty-paws-refuse.md diff --git a/.changeset/forty-paws-refuse.md b/.changeset/forty-paws-refuse.md new file mode 100644 index 0000000..f89acdd --- /dev/null +++ b/.changeset/forty-paws-refuse.md @@ -0,0 +1,5 @@ +--- +"@labdigital/graphql-fetcher": minor +--- + +override credentials option