Skip to content

openapi-fetch: Request parameter in client methods was ignored #2332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eager-steaks-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Request parameter in client methods is not ignored now
4 changes: 3 additions & 1 deletion docs/openapi-fetch/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ createClient<paths>(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) |
Expand All @@ -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)) |

Expand Down
1 change: 1 addition & 0 deletions packages/openapi-fetch/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export type RequestOptions<T> = ParamsOption<T> &
bodySerializer?: BodySerializer<T>;
parseAs?: ParseAs;
fetch?: ClientOptions["fetch"];
Request?: ClientOptions["Request"];
headers?: HeadersOptions;
};

Expand Down
8 changes: 4 additions & 4 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = {},
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions packages/openapi-fetch/test/common/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<paths>({
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 = {
Expand Down