Skip to content
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

Responses are a union of possible operations response types #3

Open
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/types.ts
Expand Up @@ -54,7 +54,9 @@ type OpResponseTypes<OP> = OP extends {
responses: infer R
}
? {
[S in keyof R]: R[S] extends { schema?: infer S } // openapi 2
[S in keyof R]: R[S] extends never
? undefined
: R[S] extends { schema?: infer S } // openapi 2
? S
: R[S] extends JSONBody<infer C> // openapi 3
? C
Expand All @@ -64,15 +66,7 @@ type OpResponseTypes<OP> = OP extends {
}
: never

type _OpReturnType<T> = 200 extends keyof T
? T[200]
: 201 extends keyof T
? T[201]
: 202 extends keyof T
? T[202]
: 'default' extends keyof T
? T['default']
: unknown
type _OpReturnType<T> = keyof T extends number | 'default' ? T[keyof T] : never
Copy link

Choose a reason for hiding this comment

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

doesn't this include all the error types as well?

Copy link
Author

Choose a reason for hiding this comment

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

I believe so, that's the point of this PR, currently you can write code that doesn't handle error cases and typescript won't catch it.


export type OpReturnType<OP> = _OpReturnType<OpResponseTypes<OP>>

Expand Down
12 changes: 9 additions & 3 deletions test/fetch.test.ts
Expand Up @@ -112,17 +112,23 @@ describe('fetch', () => {
})

it(`POST /accepted`, async () => {
const fun = fetcher.path('/accepted').method('post').create()
const fun = fetcher.path('/accepted').method('post').create({})
const { status, data } = await fun(undefined)
expect(status).toBe(202)
expect(data.message).toBe('Accepted')
})

it(`POST /nocontent`, async () => {
const fun = fetcher.path('/nocontent').method('post').create()
const { status, data } = await fun(undefined)
const fun = fetcher.path('/nocontent').method('post').create({})
const response = await fun(undefined)
const { status, data } = response
// if (response.status === 200) {
// response.data.message
// }
expect(status).toBe(204)
expect(data).toBeUndefined()
expect(data).toEqual(undefined)
assertType<{ message: string } | undefined>()(data)
})

it('GET /error', async () => {
Expand Down
2 changes: 2 additions & 0 deletions test/infer.test.ts
Expand Up @@ -71,6 +71,8 @@ describe('infer', () => {
expect(same).toBe(true)

const ret: Openapi2['Return'] = {} as any
// url is does not exist on the error case
// the status code should descriminate the type
expect(ret.url).toBeUndefined()
})

Expand Down
3 changes: 2 additions & 1 deletion test/paths.ts
Expand Up @@ -73,7 +73,8 @@ export type paths = {
post: {
parameters: {}
responses: {
204: unknown
200: { schema: { message: string } }
204: never
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/util.d.ts
@@ -0,0 +1,3 @@
declare const assertType: <T>() => <U extends T>(
result: [T] extends [U] ? U : never,
) => void