Skip to content

Commit 3a213a7

Browse files
committed
feat: override credentials
1 parent e387d09 commit 3a213a7

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
dist/
33
.DS_Store
4+
test-reports

src/client.test.ts

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createSha256 } from "helpers";
12
import {
23
afterAll,
34
beforeAll,
@@ -10,7 +11,6 @@ import {
1011
import createFetchMock from "vitest-fetch-mock";
1112
import { initClientFetcher } from "./client";
1213
import { TypedDocumentString } from "./testing";
13-
import { createSha256 } from "helpers";
1414

1515
const query = new TypedDocumentString(/* GraphQL */ `
1616
query myQuery {
@@ -98,6 +98,7 @@ describe("gqlClientFetch", () => {
9898
},
9999
);
100100
});
101+
101102
it("should perform a mutation", async () => {
102103
const mockedFetch = fetchMock.mockResponse(responseString);
103104
const gqlResponse = await fetcher(mutation, {
@@ -196,6 +197,64 @@ describe("gqlClientFetch", () => {
196197
expect(fetchMock).toHaveBeenCalledTimes(1);
197198
});
198199

200+
it("should use 'omit' credentials when provided", async () => {
201+
const fetcher = initClientFetcher("https://localhost/graphql", {
202+
defaultCredentials: "omit",
203+
});
204+
fetchMock.mockResponse(responseString);
205+
206+
const gqlResponse = await fetcher(query, {
207+
myVar: "baz",
208+
});
209+
210+
expect(gqlResponse).toEqual(response);
211+
212+
expect(fetchMock).toHaveBeenCalledWith(
213+
expect.any(String),
214+
expect.objectContaining({
215+
credentials: "omit",
216+
}),
217+
);
218+
});
219+
220+
it("should use 'same-origin' credentials when provided", async () => {
221+
const fetcher = initClientFetcher("https://localhost/graphql", {
222+
defaultCredentials: "same-origin",
223+
});
224+
fetchMock.mockResponse(responseString);
225+
226+
const gqlResponse = await fetcher(query, {
227+
myVar: "baz",
228+
});
229+
230+
expect(gqlResponse).toEqual(response);
231+
232+
expect(fetchMock).toHaveBeenCalledWith(
233+
expect.any(String),
234+
expect.objectContaining({
235+
credentials: "same-origin",
236+
}),
237+
);
238+
});
239+
240+
it("should use 'include' credentials when provided", async () => {
241+
const fetcher = initClientFetcher("https://localhost/graphql");
242+
fetchMock.mockResponse(responseString);
243+
244+
const gqlResponse = await fetcher(query, {
245+
myVar: "baz",
246+
});
247+
248+
expect(gqlResponse).toEqual(response);
249+
250+
expect(fetchMock).toHaveBeenCalledWith(
251+
expect.any(String),
252+
expect.objectContaining({
253+
credentials: "include",
254+
}),
255+
);
256+
});
257+
199258
it("should use the provided signal", async () => {
200259
const fetcher = initClientFetcher("https://localhost/graphql");
201260
fetchMock.mockResponse(responseString);

src/client.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ type Options = {
4545
*/
4646
includeQuery?: boolean;
4747

48+
/**
49+
* Default credentials to be sent with each request
50+
*/
51+
defaultCredentials?: RequestCredentials;
52+
4853
/**
4954
* Function to customize creating the documentId from a query
5055
*
@@ -75,6 +80,7 @@ export const initClientFetcher =
7580
defaultTimeout = 30000,
7681
defaultHeaders = {},
7782
includeQuery = false,
83+
defaultCredentials = "include",
7884
createDocumentId = getDocumentId,
7985
}: Options = {},
8086
): ClientFetcher =>
@@ -119,7 +125,7 @@ export const initClientFetcher =
119125
fetch(url.toString(), {
120126
headers: headers,
121127
method: "GET",
122-
credentials: "include",
128+
credentials: defaultCredentials,
123129
signal: options.signal,
124130
}),
125131
);
@@ -140,7 +146,7 @@ export const initClientFetcher =
140146
headers: headers,
141147
method: "POST",
142148
body: createRequestBody(request),
143-
credentials: "include",
149+
credentials: defaultCredentials,
144150
signal: options.signal,
145151
}),
146152
);

0 commit comments

Comments
 (0)