From 1e06f5e86bf96378b26cba8d8b34b3be5e6850f9 Mon Sep 17 00:00:00 2001 From: sergeigrishchenko Date: Sun, 25 May 2025 23:01:12 +0300 Subject: [PATCH 1/5] openapi-fetch: Request parameter in client methods was ignored --- packages/openapi-fetch/src/index.js | 6 ++--- .../openapi-fetch/test/common/request.test.ts | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/openapi-fetch/src/index.js b/packages/openapi-fetch/src/index.js index 4b226c870..d268d332f 100644 --- a/packages/openapi-fetch/src/index.js +++ b/packages/openapi-fetch/src/index.js @@ -24,8 +24,8 @@ export function randomID() { export default function createClient(clientOptions) { let { baseUrl = "", + fetch: customFetch = globalThis.fetch, Request: CustomRequest = globalThis.Request, - fetch: baseFetch = globalThis.fetch, querySerializer: globalQuerySerializer, bodySerializer: globalBodySerializer, headers: baseHeaders, @@ -44,7 +44,7 @@ export default function createClient(clientOptions) { async function coreFetch(schemaPath, fetchOptions) { const { baseUrl: localBaseUrl, - fetch = baseFetch, + fetch = customFetch, Request = CustomRequest, headers, params = {}, @@ -109,7 +109,7 @@ export default function createClient(clientOptions) { let id; let options; - let request = new CustomRequest( + let request = new Request( createFinalURL(schemaPath, { baseUrl: finalBaseUrl, params, querySerializer }), requestInit, ); diff --git a/packages/openapi-fetch/test/common/request.test.ts b/packages/openapi-fetch/test/common/request.test.ts index f5fb7c309..6073880bc 100644 --- a/packages/openapi-fetch/test/common/request.test.ts +++ b/packages/openapi-fetch/test/common/request.test.ts @@ -316,6 +316,28 @@ describe("request", () => { await client.GET("/resources"); }); + test("uses provided Request class in client method", async () => { + // santity check to make sure the profided fetch function is actually called + expect.assertions(1); + + class SpecialRequestImplementation extends Request {} + + const specialFetch = async (input: Request) => { + // make sure that the request is actually an instance of the custom request we provided + expect(input).instanceOf(SpecialRequestImplementation); + return Promise.resolve(Response.json({ hello: "world" })); + }; + + const client = createClient({ + baseUrl: "https://fakeurl.example", + fetch: specialFetch, + }); + + await client.GET("/resources", { + Request: SpecialRequestImplementation, + }); + }); + test("can attach custom properties to request", async () => { function createCustomFetch(data: any) { const response = { From beda35dd91100df69d19e4338859f31d9d303eab Mon Sep 17 00:00:00 2001 From: sergeigrishchenko Date: Sun, 25 May 2025 23:14:33 +0300 Subject: [PATCH 2/5] openapi-fetch: add changeset --- .changeset/eager-steaks-juggle.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/eager-steaks-juggle.md diff --git a/.changeset/eager-steaks-juggle.md b/.changeset/eager-steaks-juggle.md new file mode 100644 index 000000000..f969434a2 --- /dev/null +++ b/.changeset/eager-steaks-juggle.md @@ -0,0 +1,5 @@ +--- +"openapi-fetch": patch +--- + +Request parameter in client methods is not ignored now From 81753c5c177f667f3429db5c9c983cbe88c7ed42 Mon Sep 17 00:00:00 2001 From: sergeigrishchenko Date: Sun, 25 May 2025 23:25:31 +0300 Subject: [PATCH 3/5] openapi-fetch: use correct local Request option to check for instance in middlware check --- packages/openapi-fetch/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openapi-fetch/src/index.js b/packages/openapi-fetch/src/index.js index d268d332f..a5914db75 100644 --- a/packages/openapi-fetch/src/index.js +++ b/packages/openapi-fetch/src/index.js @@ -143,7 +143,7 @@ export default function createClient(clientOptions) { id, }); if (result) { - if (result instanceof CustomRequest) { + if (result instanceof Request) { request = result; } else if (result instanceof Response) { response = result; From 35b4509b9ca5f31bd528dc1b51ef4e0f0c3f8384 Mon Sep 17 00:00:00 2001 From: sergeigrishchenko Date: Sun, 25 May 2025 23:28:29 +0300 Subject: [PATCH 4/5] openapi-fetch: provide type for Request option in client methods --- packages/openapi-fetch/src/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/openapi-fetch/src/index.d.ts b/packages/openapi-fetch/src/index.d.ts index 79dec3d77..ef48f0c6e 100644 --- a/packages/openapi-fetch/src/index.d.ts +++ b/packages/openapi-fetch/src/index.d.ts @@ -119,6 +119,7 @@ export type RequestOptions = ParamsOption & bodySerializer?: BodySerializer; parseAs?: ParseAs; fetch?: ClientOptions["fetch"]; + Request?: ClientOptions["Request"]; headers?: HeadersOptions; }; From 771b03f385fe7ebe3350e5412a6762b674a99eee Mon Sep 17 00:00:00 2001 From: sergeigrishchenko Date: Sun, 25 May 2025 23:42:24 +0300 Subject: [PATCH 5/5] openapi-fetch: provide API docs for `Request` parameter --- docs/openapi-fetch/api.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/openapi-fetch/api.md b/docs/openapi-fetch/api.md index 9926adbbd..60773ef6d 100644 --- a/docs/openapi-fetch/api.md +++ b/docs/openapi-fetch/api.md @@ -17,6 +17,7 @@ createClient(options); | :---------------- | :-------------- | :-------------------------------------------------------------------------------------------------------------------------------------- | | `baseUrl` | `string` | Prefix all fetch URLs with this option (e.g. `"https://myapi.dev/v1/"`) | | `fetch` | `fetch` | Fetch instance used for requests (default: `globalThis.fetch`) | +| `Request` | `Request` | Request constructor used for requests (default: `globalThis.Request`) | | `querySerializer` | QuerySerializer | (optional) Provide a [querySerializer](#queryserializer) | | `bodySerializer` | BodySerializer | (optional) Provide a [bodySerializer](#bodyserializer) | | (Fetch options) | | Any valid fetch option (`headers`, `mode`, `cache`, `signal` …) ([docs](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options) | @@ -36,8 +37,9 @@ client.GET("/my-url", options); | `querySerializer` | QuerySerializer | (optional) Provide a [querySerializer](#queryserializer) | | `bodySerializer` | BodySerializer | (optional) Provide a [bodySerializer](#bodyserializer) | | `parseAs` | `"json"` \| `"text"` \| `"arrayBuffer"` \| `"blob"` \| `"stream"` | (optional) Parse the response using [a built-in instance method](https://developer.mozilla.org/en-US/docs/Web/API/Response#instance_methods) (default: `"json"`). `"stream"` skips parsing altogether and returns the raw stream. | -| `baseUrl` | `string` | Prefix the fetch URL with this option (e.g. `"https://myapi.dev/v1/"`) | +| `baseUrl` | `string` | Prefix the fetch URL with this option (e.g. `"https://myapi.dev/v1/"`) | | `fetch` | `fetch` | Fetch instance used for requests (default: fetch from `createClient`) | +| `Request` | `Request` | Request constructor used for requests (default: Request from `createClient`) | | `middleware` | `Middleware[]` | [See docs](/openapi-fetch/middleware-auth) | | (Fetch options) | | Any valid fetch option (`headers`, `mode`, `cache`, `signal`, …) ([docs](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)) |