-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathgenerateSpec.ts
370 lines (334 loc) · 10.1 KB
/
generateSpec.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// tslint:disable:no-submodule-imports
import _merge from 'lodash.merge'
import _capitalize from 'lodash.capitalize'
import _startCase from 'lodash.startcase'
import * as oa from 'openapi3-ts'
import * as pathToRegexp from 'path-to-regexp'
import 'reflect-metadata'
import { ParamMetadataArgs } from 'routing-controllers/types/metadata/args/ParamMetadataArgs'
import { applyOpenAPIDecorator } from './decorators'
import { IRoute } from './index'
/** Return full Express path of given route. */
export function getFullExpressPath(route: IRoute): string {
const { action, controller, options } = route
return (
(options.routePrefix || '') +
(controller.route || '') +
(action.route || '')
)
}
/**
* Return full OpenAPI-formatted path of given route.
*/
export function getFullPath(route: IRoute): string {
return expressToOpenAPIPath(getFullExpressPath(route))
}
/**
* Return OpenAPI Operation object for given route.
*/
export function getOperation(
route: IRoute,
schemas: { [p: string]: oa.SchemaObject }
): oa.OperationObject {
const operation: oa.OperationObject = {
operationId: getOperationId(route),
parameters: [
...getHeaderParams(route),
...getPathParams(route),
...getQueryParams(route, schemas),
],
requestBody: getRequestBody(route) || undefined,
responses: getResponses(route),
summary: getSummary(route),
tags: getTags(route),
}
const cleanedOperation = Object.entries(operation)
.filter(
([_, value]) => value && (value.length || Object.keys(value).length)
)
.reduce((acc, [key, value]) => {
acc[key] = value
return acc
}, {} as unknown as oa.OperationObject)
return applyOpenAPIDecorator(cleanedOperation, route)
}
/**
* Return OpenAPI Operation ID for given route.
*/
export function getOperationId(route: IRoute): string {
return `${route.action.target.name}.${route.action.method}`
}
/**
* Return OpenAPI Paths Object for given routes
*/
export function getPaths(
routes: IRoute[],
schemas: { [p: string]: oa.SchemaObject }
): oa.PathObject {
const routePaths = routes.map((route) => ({
[getFullPath(route)]: {
[route.action.type]: getOperation(route, schemas),
},
}))
// @ts-ignore: array spread
return _merge(...routePaths)
}
/**
* Return header parameters of given route.
*/
export function getHeaderParams(route: IRoute): oa.ParameterObject[] {
const headers: oa.ParameterObject[] = route.params
.filter((p) => p.type === 'header')
.map((headerMeta) => {
const schema = getParamSchema(headerMeta) as oa.SchemaObject
return {
in: 'header' as oa.ParameterLocation,
name: headerMeta.name || '',
required: isRequired(headerMeta, route),
schema,
}
})
const headersMeta = route.params.find((p) => p.type === 'headers')
if (headersMeta) {
const schema = getParamSchema(headersMeta) as oa.ReferenceObject
headers.push({
in: 'header',
name: schema.$ref.split('/').pop() || '',
required: isRequired(headersMeta, route),
schema,
})
}
return headers
}
/**
* Return path parameters of given route.
*
* Path parameters are first parsed from the path string itself, and then
* supplemented with possible @Param() decorator values.
*/
export function getPathParams(route: IRoute): oa.ParameterObject[] {
const path = getFullExpressPath(route)
const tokens = pathToRegexp.parse(path)
return tokens
.filter((token) => token && typeof token === 'object') // Omit non-parameter plain string tokens
.map((token: pathToRegexp.Key) => {
const name = token.name + ''
const param: oa.ParameterObject = {
in: 'path',
name,
required: token.modifier !== '?',
schema: { type: 'string' },
}
if (token.pattern && token.pattern !== '[^\\/]+?') {
param.schema = { pattern: token.pattern, type: 'string' }
}
const meta = route.params.find(
(p) => p.name === name && p.type === 'param'
)
if (meta) {
const metaSchema = getParamSchema(meta)
param.schema =
'type' in metaSchema ? { ...param.schema, ...metaSchema } : metaSchema
}
return param
})
}
/**
* Return query parameters of given route.
*/
export function getQueryParams(
route: IRoute,
schemas: { [p: string]: oa.SchemaObject }
): oa.ParameterObject[] {
const queries: oa.ParameterObject[] = route.params
.filter((p) => p.type === 'query')
.map((queryMeta) => {
const schema = getParamSchema(queryMeta) as oa.SchemaObject
return {
in: 'query' as oa.ParameterLocation,
name: queryMeta.name || '',
required: isRequired(queryMeta, route),
schema,
}
})
const queriesMeta = route.params.find((p) => p.type === 'queries')
if (queriesMeta) {
const paramSchema = getParamSchema(queriesMeta) as oa.ReferenceObject
// the last segment after '/'
const paramSchemaName = paramSchema.$ref.split('/').pop() || ''
const currentSchema = schemas[paramSchemaName]
for (const [name, schema] of Object.entries(
currentSchema?.properties || {}
)) {
queries.push({
in: 'query',
name,
required: currentSchema.required?.includes(name),
schema,
})
}
}
return queries
}
/**
* Return OpenAPI requestBody of given route, if it has one.
*/
export function getRequestBody(route: IRoute): oa.RequestBodyObject | void {
const bodyParamMetas = route.params.filter((d) => d.type === 'body-param')
const bodyParamsSchema: oa.SchemaObject | null =
bodyParamMetas.length > 0
? bodyParamMetas.reduce(
(acc: oa.SchemaObject, d) => ({
...acc,
properties: {
...acc.properties,
[d.name!]: getParamSchema(d),
},
required: isRequired(d, route)
? [...(acc.required || []), d.name!]
: acc.required,
}),
{ properties: {}, required: [], type: 'object' }
)
: null
const bodyMeta = route.params.find((d) => d.type === 'body')
if (bodyMeta) {
const bodySchema = getParamSchema(bodyMeta)
const { $ref } =
'items' in bodySchema && bodySchema.items ? bodySchema.items : bodySchema
return {
content: {
'application/json': {
schema: bodyParamsSchema
? { allOf: [bodySchema, bodyParamsSchema] }
: bodySchema,
},
},
description: ($ref || '').split('/').pop(),
required: isRequired(bodyMeta, route),
}
} else if (bodyParamsSchema) {
return {
content: { 'application/json': { schema: bodyParamsSchema } },
}
}
}
/**
* Return the content type of given route.
*/
export function getContentType(route: IRoute): string {
const defaultContentType =
route.controller.type === 'json'
? 'application/json'
: 'text/html; charset=utf-8'
const contentMeta = route.responseHandlers.find(
(h) => h.type === 'content-type'
)
return contentMeta ? contentMeta.value : defaultContentType
}
/**
* Return the status code of given route.
*/
export function getStatusCode(route: IRoute): string {
const successMeta = route.responseHandlers.find(
(h) => h.type === 'success-code'
)
return successMeta ? successMeta.value + '' : '200'
}
/**
* Return OpenAPI Responses object of given route.
*/
export function getResponses(route: IRoute): oa.ResponsesObject {
const contentType = getContentType(route)
const successStatus = getStatusCode(route)
return {
[successStatus]: {
content: { [contentType]: {} },
description: 'Successful response',
},
}
}
/**
* Return OpenAPI specification for given routes.
*/
export function getSpec(
routes: IRoute[],
schemas: { [p: string]: oa.SchemaObject }
): oa.OpenAPIObject {
return {
components: { schemas: {} },
info: { title: '', version: '1.0.0' },
openapi: '3.0.0',
paths: getPaths(routes, schemas),
}
}
/**
* Return OpenAPI Operation summary string for given route.
*/
export function getSummary(route: IRoute): string {
return _capitalize(_startCase(route.action.method))
}
/**
* Return OpenAPI tags for given route.
*/
export function getTags(route: IRoute): string[] {
return [_startCase(route.controller.target.name.replace(/Controller$/, ''))]
}
/**
* Convert an Express path into an OpenAPI-compatible path.
*/
export function expressToOpenAPIPath(expressPath: string) {
const tokens = pathToRegexp.parse(expressPath)
return tokens
.map((d) => (typeof d === 'string' ? d : `${d.prefix}{${d.name}}`))
.join('')
}
/**
* Return true if given metadata argument is required, checking for global
* setting if local setting is not defined.
*/
function isRequired(meta: { required?: boolean }, route: IRoute) {
const globalRequired = route.options?.defaults?.paramOptions?.required
return globalRequired ? meta.required !== false : !!meta.required
}
/**
* Parse given parameter's OpenAPI Schema or Reference object using metadata
* reflection.
*/
function getParamSchema(
param: ParamMetadataArgs
): oa.SchemaObject | oa.ReferenceObject {
const { explicitType, index, object, method } = param
const type: (() => unknown) | unknown = Reflect.getMetadata(
'design:paramtypes',
object,
method
)[index]
if (typeof type === 'function' && type.name === 'Array') {
const items = explicitType
? { $ref: '#/components/schemas/' + explicitType.name }
: { type: 'object' as const }
return { items, type: 'array' }
}
if (explicitType) {
if (explicitType.name === 'integer') {
return { type: 'integer' }
}
return { $ref: '#/components/schemas/' + explicitType.name }
}
if (typeof type === 'function') {
if (
type.prototype === String.prototype ||
type.prototype === Symbol.prototype
) {
return { type: 'string' }
} else if (type.prototype === Number.prototype) {
return { type: 'number' }
} else if (type.prototype === Boolean.prototype) {
return { type: 'boolean' }
} else if (type.name !== 'Object') {
return { $ref: '#/components/schemas/' + type.name }
}
}
return {}
}