diff --git a/.api-reports/api-report-link_http.api.md b/.api-reports/api-report-link_http.api.md index 11346eeb050..b5ff92b245a 100644 --- a/.api-reports/api-report-link_http.api.md +++ b/.api-reports/api-report-link_http.api.md @@ -89,9 +89,9 @@ export namespace HttpLink { preserveHeaderCase?: boolean; } export interface Options { - credentials?: string; + credentials?: RequestCredentials; fetch?: typeof fetch; - fetchOptions?: any; + fetchOptions?: RequestInit; headers?: Record; includeExtensions?: boolean; includeUnusedVariables?: boolean; diff --git a/.api-reports/api-report.api.md b/.api-reports/api-report.api.md index a4fecc6d549..d9564240b5f 100644 --- a/.api-reports/api-report.api.md +++ b/.api-reports/api-report.api.md @@ -1370,9 +1370,9 @@ export namespace HttpLink { preserveHeaderCase?: boolean; } export interface Options { - credentials?: string; + credentials?: RequestCredentials; fetch?: typeof fetch; - fetchOptions?: any; + fetchOptions?: RequestInit; headers?: Record; includeExtensions?: boolean; includeUnusedVariables?: boolean; diff --git a/.changeset/purple-eyes-divide.md b/.changeset/purple-eyes-divide.md new file mode 100644 index 00000000000..05ce71adb16 --- /dev/null +++ b/.changeset/purple-eyes-divide.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +The `fetchOptions` option provided to `HttpLink` and `BatchHttpLink` is now `RequestInit` instead of `any`. The `credentials` option is now a `RequestCredentials` type instead of a `string`. diff --git a/.size-limits.json b/.size-limits.json index 170e634388d..34fcaa8fbd5 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -1,6 +1,6 @@ { - "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 43862, - "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38777, - "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33487, - "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27626 + "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 43890, + "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38731, + "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33462, + "import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27595 } diff --git a/src/link/batch-http/__tests__/batchHttpLink.ts b/src/link/batch-http/__tests__/batchHttpLink.ts index 8f704a42231..2fa8011d60d 100644 --- a/src/link/batch-http/__tests__/batchHttpLink.ts +++ b/src/link/batch-http/__tests__/batchHttpLink.ts @@ -719,7 +719,7 @@ describe("SharedHttpTest", () => { const variables = { params: "stub" }; const link = new BatchHttpLink({ uri: "/data", - credentials: "same-team-yo", + credentials: "include", }); const stream = new ObservableStream( @@ -730,19 +730,19 @@ describe("SharedHttpTest", () => { await expect(stream).toComplete(); const creds = fetchMock.lastCall()![1]!.credentials; - expect(creds).toBe("same-team-yo"); + expect(creds).toBe("include"); }); it("prioritizes creds from the context over the setup", async () => { const variables = { params: "stub" }; const middleware = new ApolloLink((operation, forward) => { operation.setContext({ - credentials: "same-team-yo", + credentials: "omit", }); return forward(operation); }); const link = middleware.concat( - new BatchHttpLink({ uri: "/data", credentials: "error" }) + new BatchHttpLink({ uri: "/data", credentials: "include" }) ); const stream = new ObservableStream( @@ -753,7 +753,7 @@ describe("SharedHttpTest", () => { await expect(stream).toComplete(); const creds = fetchMock.lastCall()![1]!.credentials; - expect(creds).toBe("same-team-yo"); + expect(creds).toBe("omit"); }); it("adds uri to the request from the context", async () => { @@ -801,7 +801,7 @@ describe("SharedHttpTest", () => { return forward(operation); }); const link = middleware.concat( - new BatchHttpLink({ uri: "/data", credentials: "error" }) + new BatchHttpLink({ uri: "/data", credentials: "include" }) ); const stream = new ObservableStream( @@ -839,7 +839,7 @@ describe("SharedHttpTest", () => { const variables = { params: "stub" }; const link = new BatchHttpLink({ uri: "/data", - fetchOptions: { someOption: "foo", mode: "no-cors" }, + fetchOptions: { mode: "no-cors" }, }); const stream = new ObservableStream( @@ -849,8 +849,7 @@ describe("SharedHttpTest", () => { await expect(stream).toEmitTypedValue(data); await expect(stream).toComplete(); - const { someOption, mode, headers } = fetchMock.lastCall()![1]! as any; - expect(someOption).toBe("foo"); + const { mode, headers } = fetchMock.lastCall()![1]! as any; expect(mode).toBe("no-cors"); expect(headers["content-type"]).toBe("application/json"); }); @@ -902,13 +901,13 @@ describe("SharedHttpTest", () => { const middleware = new ApolloLink((operation, forward) => { operation.setContext({ fetchOptions: { - someOption: "foo", + mode: "cors", }, }); return forward(operation); }); const link = middleware.concat( - new BatchHttpLink({ uri: "/data", fetchOptions: { someOption: "bar" } }) + new BatchHttpLink({ uri: "/data", fetchOptions: { mode: "no-cors" } }) ); const stream = new ObservableStream( @@ -918,8 +917,8 @@ describe("SharedHttpTest", () => { await expect(stream).toEmitTypedValue(data); await expect(stream).toComplete(); - const { someOption } = fetchMock.lastCall()![1]! as any; - expect(someOption).toBe("foo"); + const { mode } = fetchMock.lastCall()![1]! as any; + expect(mode).toBe("cors"); }); it("allows for not sending the query with the request", async () => { diff --git a/src/link/http/HttpLink.ts b/src/link/http/HttpLink.ts index 0390ffb05ca..09a563e3781 100644 --- a/src/link/http/HttpLink.ts +++ b/src/link/http/HttpLink.ts @@ -129,12 +129,12 @@ export declare namespace HttpLink { /** * The credentials policy you want to use for the fetch call. */ - credentials?: string; + credentials?: RequestCredentials; /** * Any overrides of the fetch options argument to pass to the fetch call. */ - fetchOptions?: any; + fetchOptions?: RequestInit; /** * If set to true, use the HTTP GET method for query operations. Mutations diff --git a/src/link/http/__tests__/HttpLink.ts b/src/link/http/__tests__/HttpLink.ts index 4508c1f6cbb..6058722acbb 100644 --- a/src/link/http/__tests__/HttpLink.ts +++ b/src/link/http/__tests__/HttpLink.ts @@ -709,7 +709,7 @@ describe("HttpLink", () => { it("adds creds to the request from the setup", async () => { const variables = { params: "stub" }; - const link = createHttpLink({ uri: "data", credentials: "same-team-yo" }); + const link = createHttpLink({ uri: "data", credentials: "include" }); const observable = execute(link, { query: sampleQuery, variables }); const stream = new ObservableStream(observable); @@ -717,19 +717,19 @@ describe("HttpLink", () => { await expect(stream).toEmitTypedValue(data); const creds = fetchMock.lastCall()![1]!.credentials; - expect(creds).toBe("same-team-yo"); + expect(creds).toBe("include"); }); it("prioritizes creds from the context over the setup", async () => { const variables = { params: "stub" }; const middleware = new ApolloLink((operation, forward) => { operation.setContext({ - credentials: "same-team-yo", + credentials: "omit", }); return forward(operation); }); const link = middleware.concat( - createHttpLink({ uri: "data", credentials: "error" }) + createHttpLink({ uri: "data", credentials: "include" }) ); const observable = execute(link, { query: sampleQuery, variables }); @@ -738,7 +738,7 @@ describe("HttpLink", () => { await expect(stream).toEmitTypedValue(data); const creds = fetchMock.lastCall()![1]!.credentials; - expect(creds).toBe("same-team-yo"); + expect(creds).toBe("omit"); }); it("adds uri to the request from the context", async () => { @@ -782,7 +782,7 @@ describe("HttpLink", () => { return forward(operation); }); const link = middleware.concat( - createHttpLink({ uri: "data", credentials: "error" }) + createHttpLink({ uri: "data", credentials: "include" }) ); const observable = execute(link, { query: sampleQuery, variables }); @@ -817,7 +817,7 @@ describe("HttpLink", () => { const variables = { params: "stub" }; const link = createHttpLink({ uri: "data", - fetchOptions: { someOption: "foo", mode: "no-cors" }, + fetchOptions: { mode: "no-cors" }, }); const observable = execute(link, { query: sampleQuery, variables }); @@ -825,8 +825,7 @@ describe("HttpLink", () => { await expect(stream).toEmitTypedValue(data); - const { someOption, mode, headers } = fetchMock.lastCall()![1] as any; - expect(someOption).toBe("foo"); + const { mode, headers } = fetchMock.lastCall()![1] as any; expect(mode).toBe("no-cors"); expect(headers["content-type"]).toBe("application/json"); }); @@ -914,13 +913,13 @@ describe("HttpLink", () => { const middleware = new ApolloLink((operation, forward) => { operation.setContext({ fetchOptions: { - someOption: "foo", + mode: "cors", }, }); return forward(operation); }); const link = middleware.concat( - createHttpLink({ uri: "data", fetchOptions: { someOption: "bar" } }) + createHttpLink({ uri: "data", fetchOptions: { mode: "no-cors" } }) ); const observable = execute(link, { query: sampleQuery, variables }); @@ -928,8 +927,8 @@ describe("HttpLink", () => { await expect(stream).toEmitTypedValue(data); - const { someOption } = fetchMock.lastCall()![1] as any; - expect(someOption).toBe("foo"); + const { mode } = fetchMock.lastCall()![1] as any; + expect(mode).toBe("cors"); }); it("allows for not sending the query with the request", async () => {