-
-
Notifications
You must be signed in to change notification settings - Fork 533
feat: narrow type based on status code #1970
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
5e5b7fd
de4b652
7ce026e
af40b61
650c88b
6c405f8
aea776a
17dd38a
9e4e0a0
da41303
c11622d
a969c13
a3422b2
c4f66ce
8fcfc57
21c7f26
4f8192a
7aa73ca
30031bb
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,7 @@ | ||
--- | ||
"openapi-typescript-helpers": patch | ||
"openapi-react-query": patch | ||
"openapi-fetch": patch | ||
--- | ||
|
||
add `status` to openapi-fetch & type narrowing for `data` and `error` based on `status` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import type { | ||
ErrorResponse, | ||
FilterKeys, | ||
HttpMethod, | ||
IsOperationRequestBodyOptional, | ||
|
@@ -8,7 +7,10 @@ import type { | |
PathsWithMethod, | ||
RequiredKeysOf, | ||
ResponseObjectMap, | ||
SuccessResponse, | ||
GetResponseContent, | ||
ErrorStatus, | ||
OkStatus, | ||
OpenApiStatusToHttpStatus, | ||
} from "openapi-typescript-helpers"; | ||
|
||
/** Options for each client instance */ | ||
|
@@ -100,17 +102,14 @@ export type RequestBodyOption<T> = OperationRequestBodyContent<T> extends never | |
|
||
export type FetchOptions<T> = RequestOptions<T> & Omit<RequestInit, "body" | "headers">; | ||
|
||
export type FetchResponse<T extends Record<string | number, any>, Options, Media extends MediaType> = | ||
| { | ||
data: ParseAsResponse<SuccessResponse<ResponseObjectMap<T>, Media>, Options>; | ||
error?: never; | ||
response: Response; | ||
} | ||
| { | ||
data?: never; | ||
error: ErrorResponse<ResponseObjectMap<T>, Media>; | ||
response: Response; | ||
}; | ||
export type FetchResponse<T extends Record<string | number, any>, Options, Media extends MediaType> = { | ||
[S in keyof ResponseObjectMap<T>]: { | ||
response: Response; | ||
status: OpenApiStatusToHttpStatus<S, keyof ResponseObjectMap<T>>; | ||
data: S extends OkStatus ? ParseAsResponse<GetResponseContent<ResponseObjectMap<T>, Media, S>, Options> : never; | ||
error: S extends ErrorStatus ? GetResponseContent<ResponseObjectMap<T>, Media, S> : never; | ||
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. This is super nice! I like how it removes "specialness" from data versus error. IIUC, at this point, the only difference between data and error is parseAs. (totally unrelated to this PR). 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. I already got a PR semi-ready about that #1986 :) |
||
}; | ||
}[keyof ResponseObjectMap<T>]; | ||
|
||
export type RequestOptions<T> = ParamsOption<T> & | ||
RequestBodyOption<T> & { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,22 +20,38 @@ describe("response", () => { | |
// 2. assert data is not undefined inside condition block | ||
if (result.data) { | ||
assertType<NonNullable<Resource[]>>(result.data); | ||
assertType<undefined>(result.error); | ||
// @ts-expect-error FIXME: This is a limitation within Typescript | ||
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. I love these tests, and this is a great start! But I fear that we’ll have to fix this before merging (it’ll be much more difficult to fix in a followup, when other work has built off the inference). I don’t really care if the type is |
||
assertType<never>(result.error); | ||
} | ||
// 2b. inverse should work, too | ||
if (!result.error) { | ||
assertType<NonNullable<Resource[]>>(result.data); | ||
assertType<undefined>(result.error); | ||
assertType<never>(result.error); | ||
} | ||
|
||
if (result.status === 200) { | ||
assertType<NonNullable<Resource[]>>(result.data); | ||
assertType<never>(result.error); | ||
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. These are good tests! I would want to see the other common codes tested here, too— |
||
} | ||
|
||
if (result.status === 500) { | ||
assertType<never>(result.data); | ||
assertType<Error>(result.error); | ||
} | ||
|
||
// @ts-expect-error 204 is not defined in the schema | ||
if (result.status === 204) { | ||
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. This is a REALLY good test I want to keep—testing what happens when a status code is impossible for a given endpoint! But we should test that |
||
} | ||
|
||
// 3. assert error is not undefined inside condition block | ||
if (result.error) { | ||
assertType<undefined>(result.data); | ||
// @ts-expect-error FIXME: This is a limitation within Typescript | ||
assertType<never>(result.data); | ||
assertType<NonNullable<Error>>(result.error); | ||
} | ||
// 3b. inverse should work, too | ||
if (!result.data) { | ||
assertType<undefined>(result.data); | ||
assertType<never>(result.data); | ||
assertType<NonNullable<Error>>(result.error); | ||
} | ||
}); | ||
|
@@ -49,9 +65,8 @@ describe("response", () => { | |
{}, | ||
); | ||
|
||
//@ts-expect-error impossible to determine data type for invalid path | ||
assertType<never>(result.data); | ||
assertType<undefined>(result.error); | ||
assertType<never>(result.error); | ||
}); | ||
|
||
test("returns union for mismatched response", async () => { | ||
|
@@ -65,14 +80,14 @@ describe("response", () => { | |
} | ||
}); | ||
|
||
test("returns union for mismatched errors", async () => { | ||
test("returns union for mismatched errors", async () => { | ||
const client = createObservedClient<paths>(); | ||
const result = await client.GET("/mismatched-errors"); | ||
if (result.data) { | ||
expectTypeOf(result.data).toEqualTypeOf<Resource>(); | ||
expectTypeOf(result.data).toEqualTypeOf<MethodResponse<typeof client, "get", "/mismatched-errors">>(); | ||
} else { | ||
expectTypeOf(result.data).toBeUndefined(); | ||
expectTypeOf(result.data).toBeNever(); | ||
expectTypeOf(result.error).extract<{ code: number }>().toEqualTypeOf<{ code: number; message: string }>(); | ||
expectTypeOf(result.error).exclude<{ code: number }>().toEqualTypeOf(undefined); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great docs updates! Thank you