Skip to content

Fix OpReturnType to support 202 status code #36

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 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ 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
Expand All @@ -65,11 +67,11 @@ export type OpDefaultReturnType<OP> = _OpDefaultReturnType<OpResponseTypes<OP>>
const never: unique symbol = Symbol()

type _OpErrorType<T> = {
[S in Exclude<keyof T, 200 | 201>]: {
[S in Exclude<keyof T, 200 | 201 | 202>]: {
status: S extends 'default' ? typeof never : S
data: T[S]
}
}[Exclude<keyof T, 200 | 201>]
}[Exclude<keyof T, 200 | 201 | 202>]

type Coalesce<T, D> = [T] extends [never] ? D : T

Expand Down
7 changes: 7 additions & 0 deletions test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ describe('fetch', () => {
expect(data.headers).not.toHaveProperty('content-type')
})

it(`POST /accepted`, async () => {
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)
Expand Down
6 changes: 6 additions & 0 deletions test/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ const methods = {
withBodyAndQuery: ['post', 'put', 'patch', 'delete'].map((method) => {
return (rest as any)[method](`${HOST}/bodyquery/:id`, getResult)
}),
withAccepted: [
rest.post(`${HOST}/accepted`, (req, res, ctx) => {
return res(ctx.status(202), ctx.json({ message: 'Accepted' }))
}),
],
withError: [
rest.get(`${HOST}/error/:status`, (req, res, ctx) => {
const status = Number(req.params.status)
Expand Down Expand Up @@ -81,5 +86,6 @@ export const handlers = [
...methods.withBody,
...methods.withBodyArray,
...methods.withBodyAndQuery,
...methods.withAccepted,
...methods.withError,
]
8 changes: 8 additions & 0 deletions test/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ export type paths = {
patch: BodyAndQuery
delete: BodyAndQuery
}
'/accepted': {
post: {
parameters: {}
responses: {
202: { schema: { message: string } }
}
}
}
'/nocontent': {
post: {
parameters: {}
Expand Down