-
-
Notifications
You must be signed in to change notification settings - Fork 539
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 14 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 |
---|---|---|
|
@@ -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); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import { assertType, describe, test } from "vitest"; | ||
import type { ErrorResponse, GetResponseContent, OkStatus, SuccessResponse } from "openapi-typescript-helpers"; | ||
import type { | ||
ErrorResponse, | ||
GetResponseContent, | ||
OkStatus, | ||
OpenApiStatusToHttpStatus, | ||
SuccessResponse, | ||
} from "openapi-typescript-helpers"; | ||
|
||
describe("types", () => { | ||
describe("GetResponseContent", () => { | ||
|
@@ -280,4 +286,179 @@ describe("types", () => { | |
assertType<Response>({ error: "default application/json" }); | ||
}); | ||
}); | ||
|
||
describe("OpenApiStatusToHttpStatus", () => { | ||
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. It is super nice to see unit tests for this helper! I was wondering: IIUC the second argument ( @drwpow self note to maintainers: we should probably set up the infrastructure for |
||
test("returns numeric status code", () => { | ||
assertType<OpenApiStatusToHttpStatus<200, number>>(200); | ||
assertType<OpenApiStatusToHttpStatus<200, string>>(200); | ||
assertType<OpenApiStatusToHttpStatus<204, string>>(204); | ||
|
||
assertType<OpenApiStatusToHttpStatus<204, string>>( | ||
// @ts-expect-error 200 is not a valid | ||
200, | ||
); | ||
assertType<OpenApiStatusToHttpStatus<204, number>>( | ||
// @ts-expect-error 200 is not a valid | ||
200, | ||
); | ||
|
||
assertType<OpenApiStatusToHttpStatus<404, 200 | 204 | 206 | 404 | 500 | "default">>(404); | ||
}); | ||
|
||
test("returns default response", () => { | ||
type Status = OpenApiStatusToHttpStatus<"default", 200 | 204 | 206 | 404 | 500 | "default">; | ||
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. Would it make sense add a test case where there also is a wildcard (e.g. |
||
assertType<Status>( | ||
// @ts-expect-error 200 has been manually defined | ||
200, | ||
); | ||
assertType<Status>( | ||
// @ts-expect-error 204 has been manually defined | ||
204, | ||
); | ||
assertType<Status>(201); | ||
assertType<Status>(504); | ||
}); | ||
|
||
test("returns 200 likes response", () => { | ||
type Status = OpenApiStatusToHttpStatus<"2XX", 200 | 204 | 206 | 404 | 500 | "default">; | ||
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. It feels to me like this test case is "degenerate" at least if I understand the usage in this PR correctly: I would expect the For this test case specifically, I would expect that the explicitly / numerically defined status codes are not part of the end result (precedence, as I pointed out in the doc comment of |
||
assertType<Status>(200); | ||
assertType<Status>(201); | ||
assertType<Status>(202); | ||
assertType<Status>(203); | ||
assertType<Status>(204); | ||
assertType<Status>( | ||
// @ts-expect-error 205 is not a valid 2XX status code | ||
205, | ||
); | ||
assertType<Status>(206); | ||
assertType<Status>(207); | ||
assertType<Status>( | ||
// @ts-expect-error '2XX' is not a numeric status code | ||
"2XX", | ||
); | ||
assertType<Status>( | ||
// @ts-expect-error 205 is not a valid 2XX status code | ||
208, | ||
); | ||
|
||
assertType<Status>( | ||
// @ts-expect-error '4XX' is not a numeric status code | ||
"4XX", | ||
); | ||
assertType<Status>( | ||
// @ts-expect-error '5XX' is not a numeric status code | ||
"5XX", | ||
); | ||
}); | ||
|
||
test("returns error responses for 4XX", () => { | ||
type Status = OpenApiStatusToHttpStatus<"4XX", 200 | 204 | 206 | 404 | 500 | "default">; | ||
assertType<Status>(400); | ||
assertType<Status>(401); | ||
assertType<Status>(402); | ||
assertType<Status>(403); | ||
assertType<Status>(404); | ||
assertType<Status>(405); | ||
assertType<Status>(406); | ||
assertType<Status>(407); | ||
assertType<Status>(408); | ||
assertType<Status>(409); | ||
assertType<Status>(410); | ||
assertType<Status>(411); | ||
assertType<Status>(412); | ||
assertType<Status>(413); | ||
assertType<Status>(414); | ||
assertType<Status>(415); | ||
assertType<Status>(416); | ||
assertType<Status>(417); | ||
assertType<Status>(418); | ||
assertType<Status>(500); | ||
assertType<Status>(501); | ||
assertType<Status>(502); | ||
assertType<Status>(503); | ||
assertType<Status>(504); | ||
assertType<Status>(505); | ||
assertType<Status>(506); | ||
assertType<Status>(507); | ||
assertType<Status>(508); | ||
assertType<Status>( | ||
// @ts-expect-error 509 is not a valid error status code | ||
509, | ||
); | ||
assertType<Status>(510); | ||
assertType<Status>(511); | ||
|
||
assertType<Status>( | ||
// @ts-expect-error 200 is not a valid error status code | ||
200, | ||
); | ||
assertType<Status>( | ||
// @ts-expect-error '2XX' is not a numeric status code | ||
"2XX", | ||
); | ||
assertType<Status>( | ||
// @ts-expect-error '4XX' is not a numeric status code | ||
"4XX", | ||
); | ||
assertType<Status>( | ||
// @ts-expect-error '5XX' is not a numeric status code | ||
"5XX", | ||
); | ||
}); | ||
|
||
test("returns error responses for 5XX", () => { | ||
type Status = OpenApiStatusToHttpStatus<"5XX", 200 | 204 | 206 | 404 | 500 | "default">; | ||
assertType<Status>(400); | ||
assertType<Status>(401); | ||
assertType<Status>(402); | ||
assertType<Status>(403); | ||
assertType<Status>(404); | ||
assertType<Status>(405); | ||
assertType<Status>(406); | ||
assertType<Status>(407); | ||
assertType<Status>(408); | ||
assertType<Status>(409); | ||
assertType<Status>(410); | ||
assertType<Status>(411); | ||
assertType<Status>(412); | ||
assertType<Status>(413); | ||
assertType<Status>(414); | ||
assertType<Status>(415); | ||
assertType<Status>(416); | ||
assertType<Status>(417); | ||
assertType<Status>(418); | ||
assertType<Status>(500); | ||
assertType<Status>(501); | ||
assertType<Status>(502); | ||
assertType<Status>(503); | ||
assertType<Status>(504); | ||
assertType<Status>(505); | ||
assertType<Status>(506); | ||
assertType<Status>(507); | ||
assertType<Status>(508); | ||
assertType<Status>( | ||
// @ts-expect-error 509 is not a valid error status code | ||
509, | ||
); | ||
assertType<Status>(510); | ||
assertType<Status>(511); | ||
|
||
assertType<Status>( | ||
// @ts-expect-error 200 is not a valid error status code | ||
200, | ||
); | ||
assertType<Status>( | ||
// @ts-expect-error '2XX' is not a numeric status code | ||
"2XX", | ||
); | ||
assertType<Status>( | ||
// @ts-expect-error '4XX' is not a numeric status code | ||
"4XX", | ||
); | ||
assertType<Status>( | ||
// @ts-expect-error '5XX' is not a numeric status code | ||
"5XX", | ||
); | ||
}); | ||
}); | ||
}); |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I already got a PR semi-ready about that #1986 :)