-
Notifications
You must be signed in to change notification settings - Fork 1
/
zod-api.controller.ts
355 lines (313 loc) · 11.6 KB
/
zod-api.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import express, { NextFunction, RequestHandler, Response } from 'express';
import {
ParameterObject,
PathItemObject,
PathsObject,
RequestBodyObject,
ResponseObject,
SchemaObject,
} from 'openapi3-ts/oas31';
import z, { SomeZodObject, ZodSchema } from 'zod';
import { TypedRequest, validateRequest } from './request-validation.middleware';
import { generateSchema } from '@anatine/zod-openapi';
import { OperationObject } from 'openapi3-ts/src/model/openapi31';
type HttpMethods = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'options' | 'head' | 'trace';
// type OpenApiOptions = Partial<{
// path: Omit<PathItemObject, 'parameters' | HttpMethods | 'description'>
// operation: Omit<OperationObject, 'parameters' | 'requestBody' | 'responses' | 'description'>
// }>;
/** OpenAPI schema that excludes fields inferred from a route config. */
type OpenApiOperationOptions = Omit<OperationObject, 'parameters' | 'requestBody' | 'responses' | 'description'>;
/**
* Define an express route with Zod-validated request parameters, query, and body.
*/
export type RouteConfig<
TParams extends ZodSchema = ZodSchema,
TQuery extends ZodSchema = ZodSchema,
TBody extends ZodSchema = ZodSchema,
THeaders extends ZodSchema = ZodSchema,
> = {
/** The HTTP method for the route. */
method: HttpMethods;
/** A Zod schema for the request parameters. Used to generate OpenAPI parameter definition. */
params?: TParams;
/** A Zod schema for the request query. Used to generate OpenAPI parameter definition. */
query?: TQuery;
/** A Zod schema for the request body. Used to generate OpenAPI parameter definition. */
body?: TBody | RequestBodyObject;
/** A Zod schema for the request headers. Used to generate OpenAPI parameter definition. */
headers?: THeaders;
/** OpenAPI description of the route. */
description?: string;
/** Define the responses for the route. */
responses: RouteResponses;
/** Middleware to run before the route handler. */
middleware?: RequestHandler[];
} & OpenApiOperationOptions;
type ShortRouteConfig<TParams extends ZodSchema = ZodSchema, TQuery extends ZodSchema = ZodSchema, TBody extends ZodSchema = ZodSchema> = Omit<RouteConfig<TParams, TQuery, TBody>, 'method'>;
export type RouteResponse = ZodSchema | ResponseObject;
/**
* Define the responses for a route.
*/
export interface RouteResponses {
[statusCode: number]: RouteResponse;
}
export interface ZodApiControllerConfig {
/** Default OpenAPI responses for all routes. */
defaultResponses?: RouteResponses;
/** Express router to use for the controller. */
router?: express.Router;
}
const constants = {
noContent: 'No content',
emptyResponses: [z.never()._type, z.void()._type, z.undefined()._type],
jsonContent: 'application/json',
};
type RouteConfigOmitMethod<
TParams extends ZodSchema = ZodSchema,
TQuery extends ZodSchema = ZodSchema,
TBody extends ZodSchema = ZodSchema,
> = Omit<RouteConfig<TParams, TQuery, TBody>, 'method'>;
/**
* A controller that manages express routes and generates OpenAPI paths.
*/
export class ZodApiController {
router: express.Router;
openApiPaths: PathsObject = {};
readonly defaultResponses: RouteResponses;
constructor({ router, defaultResponses }: ZodApiControllerConfig = {}) {
this.router = router ?? express.Router();
this.defaultResponses = defaultResponses ?? {};
}
/**
* Define an express route with Zod-validated request parameters, query,
* and body and create OpenAPI definitions for it.
*
* @param path The express route path in the format `/segment/:param`. Must be from the root, not relative.
* @param config The route configuration.
* @param handler The route handler.
*/
route<
TParams extends ZodSchema = ZodSchema,
TQuery extends ZodSchema = ZodSchema,
TBody extends ZodSchema = ZodSchema,
>(
path: string,
config: RouteConfig<TParams, TQuery, TBody>,
handler: (req: TypedRequest<TParams, TQuery, TBody>, res: Response, next: NextFunction) => void,
): ZodApiController {
const { method, params, query, body, responses, middleware = [], operationId: initialOperationId } = config;
const operationId = initialOperationId ?? `${method}_${path}`;
const validate = params != null || query != null || (body != null && body instanceof ZodSchema);
const middlewares = validate
? [
...middleware,
validateRequest({
params,
query,
body: body instanceof ZodSchema ? body : undefined,
}),
]
: middleware;
// register express route
this.router = this.router[method](path, ...middlewares, handler);
return this.addRouteToOpenApi(path, { ...config, responses: { ...this.defaultResponses, ...responses }, operationId });
}
// Shortcut for `route` with `method: 'get'`
get<TParams extends ZodSchema = ZodSchema, TQuery extends ZodSchema = ZodSchema, TBody extends ZodSchema = ZodSchema>(
path: string,
config: ShortRouteConfig<TParams, TQuery, TBody>,
handler: (req: TypedRequest<TParams, TQuery, TBody>, res: Response, next: NextFunction) => void,
): ZodApiController {
return this.route(path, { ...config, method: 'get' }, handler);
}
// Shortcut for `route` with `method: 'post'`
post<
TParams extends ZodSchema = ZodSchema,
TQuery extends ZodSchema = ZodSchema,
TBody extends ZodSchema = ZodSchema,
>(
path: string,
config: Omit<RouteConfig<TParams, TQuery, TBody>, 'method'>,
handler: (req: TypedRequest<TParams, TQuery, TBody>, res: Response, next: NextFunction) => void,
): ZodApiController {
return this.route(path, { ...config, method: 'post' }, handler);
}
// Shortcut for `route` with `method: 'put'`
put<TParams extends ZodSchema = ZodSchema, TQuery extends ZodSchema = ZodSchema, TBody extends ZodSchema = ZodSchema>(
path: string,
config: ShortRouteConfig<TParams, TQuery, TBody>,
handler: (req: TypedRequest<TParams, TQuery, TBody>, res: Response, next: NextFunction) => void,
): ZodApiController {
return this.route(path, { ...config, method: 'put' }, handler);
}
// Shortcut for `route` with `method: 'patch'`
patch<
TParams extends ZodSchema = ZodSchema,
TQuery extends ZodSchema = ZodSchema,
TBody extends ZodSchema = ZodSchema,
>(
path: string,
config: Omit<RouteConfig<TParams, TQuery, TBody>, 'method'>,
handler: (req: TypedRequest<TParams, TQuery, TBody>, res: Response, next: NextFunction) => void,
): ZodApiController {
return this.route(path, { ...config, method: 'patch' }, handler);
}
// An alias for the `route` function with `method: 'delete'`
delete<
TParams extends ZodSchema = ZodSchema,
TQuery extends ZodSchema = ZodSchema,
TBody extends ZodSchema = ZodSchema,
>(
path: string,
config: ShortRouteConfig<TParams, TQuery, TBody>,
handler: (req: TypedRequest<TParams, TQuery, TBody>, res: Response, next: NextFunction) => void,
): ZodApiController {
return this.route(path, { ...config, method: 'delete' }, handler);
}
private addRouteToOpenApi(path: string, config: RouteConfig): ZodApiController {
const {
method,
params,
query,
body,
responses,
description,
summary,
security,
servers,
deprecated,
externalDocs,
operationId,
callbacks,
tags,
} = config;
const openApiPath = convertExpressPathToOpenApiPath(path);
const pathItem: PathItemObject = this.openApiPaths[openApiPath] || {};
const parameters = new Array<ParameterObject>();
if (params) {
this.addOpenApiParams('path', params, parameters);
}
if (query) {
this.addOpenApiParams('query', query, parameters);
}
pathItem[method] = {
description,
security,
servers,
summary,
deprecated,
externalDocs,
operationId,
callbacks,
tags,
parameters,
requestBody: body
? body instanceof ZodSchema
? {
content: {
[constants.jsonContent]: {
schema: generateSchema(body),
},
},
required: !body.isOptional(),
description: body.description,
}
: body
: undefined,
responses: {},
};
Object.entries(responses).forEach(([statusCode, response]: [string, RouteResponse]) => {
if (pathItem[method]?.responses) {
let responseDef = response;
if (response instanceof ZodSchema) {
const typeName = response._type;
if (constants.emptyResponses.includes(typeName)) {
responseDef = { description: constants.noContent };
} else {
responseDef = {
description: response.description ?? '',
content: {
[constants.jsonContent]: {
schema: generateSchema(response as SomeZodObject),
},
},
};
}
}
pathItem[method]!.responses = {
[+statusCode]: responseDef,
...(pathItem[method]?.responses ?? {}),
};
}
});
this.openApiPaths[openApiPath] = pathItem;
return this;
}
private addOpenApiParams(
inType: 'path' | 'query',
schema: ZodSchema<any>,
parameters: ParameterObject[],
): ParameterObject[] {
const schemaObject = generateSchema(schema) as SchemaObject;
for (const key in schemaObject.properties) {
const property = schemaObject.properties[key];
const required = schemaObject.required?.includes(key) ?? false;
parameters.push({
in: inType,
name: key,
schema: property,
required,
});
}
return parameters;
}
}
/** Converts `api/:someParam` to `api/{someParam}` */
function convertExpressPathToOpenApiPath(input: string): string {
// https://regex101.com/r/6uTvFu/1
const regex = /:([A-Za-z0-9\-_.~]+)/g;
return input.replace(regex, '{$1}');
}
type JsonValue = string | number | boolean | null | JsonArray | JsonObject;
interface JsonArray extends Array<JsonValue> {}
interface JsonObject {
[key: string | number | symbol]: JsonValue;
}
function isObject(item: JsonValue): item is JsonObject {
return item !== null && typeof item === 'object' && !Array.isArray(item);
}
/**
* Deeply merges two JSON values (objects, arrays, or primitives).
* - Objects are merged by combining properties, with source properties overriding target properties.
* - Arrays are concatenated.
* - Primitive values from the source override those from the target.
*
* @param target The target value to merge into. Can be an object, array, or primitive.
* @param source The source value to merge from. Can be an object, array, or primitive.
* @returns The result of merging `source` into `target`.
*/
export function mergeDeep<T extends JsonValue, U extends JsonValue>(target: T, source: U): T & U {
if (isObject(target) && isObject(source)) {
const mergedOutput: JsonObject = { ...target };
Object.keys(source).forEach((key) => {
if (isObject(source[key])) {
if (key in target && isObject(target[key])) {
mergedOutput[key] = mergeDeep(target[key] as JsonObject, source[key] as JsonObject);
} else {
mergedOutput[key] = source[key];
}
} else if (Array.isArray(source[key])) {
mergedOutput[key] = Array.isArray(target[key])
? [...(target[key] as JsonArray), ...(source[key] as JsonArray)]
: source[key];
} else {
mergedOutput[key] = source[key];
}
});
return mergedOutput as T & U;
} else if (Array.isArray(target) && Array.isArray(source)) {
return [...target, ...source] as T & U;
}
return source as T & U;
}