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.

Copy link
Author

Choose a reason for hiding this comment

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

The reason I added the type assertion was due to an error about the signatures not being compatible.

This expression is not callable.
  Each member of the union type 'ClientMethod<paths, "get", `${string}/${string}`> | ClientMethod<paths, "put", `${string}/${string}`> | ClientMethod<paths, "post", `${string}/${string}`> | ClientMethod<...> | ClientMethod<...> | ClientMethod<...> | ClientMethod<...>' has signatures, but none of those signatures are compatible with each other.ts(2349)

It actually does seem to work, I get 7 failures without the fix implemented, here's a sample

 FAIL  test/common/request.test.ts > request > custom Request > uses custom Request class - PATCH
AssertionError: expected Request{} to be an instance of SpecialRequestImplementation
 ❯ customFetch test/common/request.test.ts:330:23
    328|       const customFetch = async (input: Request) => {
    329|         // make sure that the request is actually an instance of the custom request we provided
    330|         expect(input).instanceOf(SpecialRequestImplementation);
       |                       ^
    331|         return Promise.resolve(Response.json({ hello: "world" }));
    332|       };
 ❯ coreFetch src/index.js:162:26
 ❯ Object.PATCH src/index.js:283:14
 ❯ test/common/request.test.ts:339:35

I'm not an expert in TypeScript though so happy to adjust as desired. Would you prefer the more explicit non type asserted single method version? I.e. client.GET(...)? Based on the implementation this should have the same result as the current testing approach.

Copy link
Contributor

Choose a reason for hiding this comment

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

It actually does seem to work, I get 7 failures without the fix implemented, here's a sample

Yes, but these are runtime failures. Precisely the type of thing we'd like to catch with typescript.

Would you prefer the more explicit non type asserted single method version? I.e. client.GET(...)? Based on the implementation this should have the same result as the current testing approach.

Yes, I think that would be more appropriate here. Given the implementation, testing all HTTP methods gives us marginal benefits. So I think I'd rather go with a simpler test that is closer to (expected) real-life usage.

});
});

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