Skip to content

Fix Request parameter being ignored by client methods #2407

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 3 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/empty-rooms-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Fix Request parameter being ignored by client methods
4 changes: 2 additions & 2 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
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
26 changes: 25 additions & 1 deletion packages/openapi-fetch/test/common/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe("request", () => {
});

test("uses provided Request class", async () => {
// santity check to make sure the profided fetch function is actually called
// sanity check to make sure the provided fetch function is actually called
expect.assertions(1);

class SpecialRequestImplementation extends Request {}
Expand All @@ -316,6 +316,30 @@ describe("request", () => {
await client.GET("/resources");
});

describe("custom Request", () => {
const ALL_METHODS = [["GET"], ["HEAD"], ["PUT"], ["POST"], ["DELETE"], ["OPTIONS"], ["PATCH"]] as const;

test.each(ALL_METHODS)("uses custom Request class - %s", async (method) => {
// sanity check to make sure the provided fetch function is actually called
expect.assertions(1);

class SpecialRequestImplementation extends Request {}

const customFetch = 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: customFetch,
});

await (client as any)[method]("/resources", { Request: SpecialRequestImplementation });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, oh. With the as any, we don't actually test if the typings allow passing the { Request: SpecialRequestImplementation } (because with any anything will be allowed).

Why is it necessary?

If it is difficult to get the typing to work with test.each, it feels like it might also just test a single method.

});
});

test("can attach custom properties to request", async () => {
function createCustomFetch(data: any) {
const response = {
Expand Down
Loading