-
-
Notifications
You must be signed in to change notification settings - Fork 565
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 {} | ||
|
@@ -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 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uh, oh. With the Why is it necessary? If it is difficult to get the typing to work with There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
It actually does seem to work, I get 7 failures without the fix implemented, here's a sample
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, but these are runtime failures. Precisely the type of thing we'd like to catch with typescript.
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 = { | ||
|
Uh oh!
There was an error while loading. Please reload this page.