diff --git a/src/types.ts b/src/types.ts index 9d51fb00..f8936ee0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -49,6 +49,8 @@ type _OpReturnType = 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 @@ -65,11 +67,11 @@ export type OpDefaultReturnType = _OpDefaultReturnType> const never: unique symbol = Symbol() type _OpErrorType = { - [S in Exclude]: { + [S in Exclude]: { status: S extends 'default' ? typeof never : S data: T[S] } -}[Exclude] +}[Exclude] type Coalesce = [T] extends [never] ? D : T diff --git a/test/fetch.test.ts b/test/fetch.test.ts index 974dbdda..56e572c8 100644 --- a/test/fetch.test.ts +++ b/test/fetch.test.ts @@ -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) diff --git a/test/mocks/handlers.ts b/test/mocks/handlers.ts index 1ea77152..4bdb668e 100644 --- a/test/mocks/handlers.ts +++ b/test/mocks/handlers.ts @@ -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) @@ -81,5 +86,6 @@ export const handlers = [ ...methods.withBody, ...methods.withBodyArray, ...methods.withBodyAndQuery, + ...methods.withAccepted, ...methods.withError, ] diff --git a/test/paths.ts b/test/paths.ts index afc3831a..06083727 100644 --- a/test/paths.ts +++ b/test/paths.ts @@ -61,6 +61,14 @@ export type paths = { patch: BodyAndQuery delete: BodyAndQuery } + '/accepted': { + post: { + parameters: {} + responses: { + 202: { schema: { message: string } } + } + } + } '/nocontent': { post: { parameters: {}