Skip to content

Commit b17895b

Browse files
authored
docs: default 4xx error schemas for all routes (#2159)
1 parent ed87f15 commit b17895b

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

.vscode/launch.json

+7
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,13 @@
460460
"TS_NODE_SKIP_IGNORE": "true"
461461
}
462462
},
463+
{
464+
"type": "node",
465+
"request": "launch",
466+
"name": "docs: openapi-generator",
467+
"runtimeArgs": ["-r", "ts-node/register/transpile-only"],
468+
"args": ["${workspaceFolder}/src/openapi-generator.ts"]
469+
},
463470
{
464471
"type": "node",
465472
"request": "launch",

src/api/schemas/responses/responses.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import { NakamotoBlockSchema, SignerSignatureSchema } from '../entities/block';
1717
export const ErrorResponseSchema = Type.Object(
1818
{
1919
error: Type.String(),
20+
message: Type.Optional(Type.String()),
2021
},
21-
{ title: 'Error Response' }
22+
{ title: 'Error Response', additionalProperties: true }
2223
);
2324

2425
export const ServerStatusResponseSchema = Type.Object(

src/openapi-generator.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import Fastify from 'fastify';
2-
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
2+
import { TSchema, TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
33
import FastifySwagger from '@fastify/swagger';
44
import { writeFileSync } from 'fs';
55
import { OpenApiSchemaOptions } from './api/schemas/openapi';
66
import { StacksApiRoutes } from './api/init';
7+
import { ErrorResponseSchema } from './api/schemas/responses/responses';
78

89
/**
910
* Generates `openapi.yaml` based on current Swagger definitions.
@@ -14,6 +15,16 @@ async function generateOpenApiFiles() {
1415
logger: true,
1516
}).withTypeProvider<TypeBoxTypeProvider>();
1617

18+
// If a response schema is defined but lacks a '4xx' response, add it
19+
fastify.addHook(
20+
'onRoute',
21+
(route: { schema?: { response: Record<string | number, TSchema> } }) => {
22+
if (route.schema?.response && !route.schema?.response['4xx']) {
23+
route.schema.response['4xx'] = ErrorResponseSchema;
24+
}
25+
}
26+
);
27+
1728
await fastify.register(FastifySwagger, OpenApiSchemaOptions);
1829
await fastify.register(StacksApiRoutes);
1930
await fastify.ready();

0 commit comments

Comments
 (0)