Skip to content

Commit eb37cbe

Browse files
committed
feat(openapi): override success response status
1 parent b6be6f0 commit eb37cbe

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

packages/contract/src/procedure.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import { z } from 'zod'
22
import { ContractProcedure, isContractProcedure } from './procedure'
33

4+
describe('contractProcedure', () => {
5+
it('throws error when route.successStatus is not between 200 and 299', () => {
6+
expect(
7+
() => new ContractProcedure({ InputSchema: undefined, OutputSchema: undefined, route: { successStatus: 100 } }),
8+
).toThrowError()
9+
expect(
10+
() => new ContractProcedure({ InputSchema: undefined, OutputSchema: undefined, route: { successStatus: 300 } }),
11+
).toThrowError()
12+
expect(
13+
() => new ContractProcedure({ InputSchema: undefined, OutputSchema: undefined, route: { successStatus: 299 } }),
14+
).not.toThrowError()
15+
})
16+
})
17+
418
describe('isContractProcedure', () => {
519
it('works', () => {
620
expect(new ContractProcedure({ InputSchema: undefined, OutputSchema: undefined })).toSatisfy(isContractProcedure)

packages/contract/src/procedure.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface RouteOptions {
1212
description?: string
1313
deprecated?: boolean
1414
tags?: readonly string[]
15+
successStatus?: number
1516
}
1617

1718
export interface ContractProcedureDef<TInputSchema extends Schema, TOutputSchema extends Schema> {
@@ -27,6 +28,10 @@ export class ContractProcedure<TInputSchema extends Schema, TOutputSchema extend
2728
'~orpc': ContractProcedureDef<TInputSchema, TOutputSchema>
2829

2930
constructor(def: ContractProcedureDef<TInputSchema, TOutputSchema>) {
31+
if (def.route?.successStatus && (def.route.successStatus < 200 || def.route?.successStatus > 299)) {
32+
throw new Error('[ContractProcedure] The successStatus must be between 200 and 299')
33+
}
34+
3035
this['~orpc'] = def
3136
}
3237
}

packages/openapi/src/fetch/openapi-handler.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,29 @@ describe.each(hono)('openAPIHandler: %s', (_, hono) => {
220220
expect(vi.mocked(createProcedureClient).mock.results[0]?.value).toBeCalledTimes(1)
221221
expect(vi.mocked(createProcedureClient).mock.results[0]?.value).toBeCalledWith('__mocked__', { signal: undefined })
222222
})
223+
224+
it('custom success status', async () => {
225+
const router = {
226+
ping: new Procedure({
227+
contract: new ContractProcedure({
228+
route: {
229+
method: 'GET',
230+
path: '/ping',
231+
successStatus: 298,
232+
},
233+
InputSchema: undefined,
234+
OutputSchema: undefined,
235+
}),
236+
handler: vi.fn(),
237+
}),
238+
}
239+
240+
const handler = new OpenAPIHandler(hono, router)
241+
242+
const mockRequest = new Request('https://example.com/ping')
243+
244+
const response = await handler.fetch(mockRequest)
245+
246+
expect(response?.status).toBe(298)
247+
})
223248
})

packages/openapi/src/fetch/openapi-handler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ export class OpenAPIHandler<T extends Context> implements ConditionalFetchHandle
8787

8888
const { body, headers } = this.payloadCodec.encode(output)
8989

90-
return new Response(body, { headers })
90+
return new Response(body, {
91+
headers,
92+
status: match.procedure['~orpc'].contract['~orpc'].route?.successStatus ?? 200,
93+
})
9194
}
9295

9396
try {

packages/openapi/src/openapi-generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export class OpenAPIGenerator {
180180
parameters: parameters.length ? parameters : undefined,
181181
requestBody,
182182
responses: {
183-
200: successResponse,
183+
[def.route?.successStatus ?? 200]: successResponse,
184184
},
185185
}
186186

0 commit comments

Comments
 (0)