-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathutils.js
275 lines (239 loc) · 8.71 KB
/
utils.js
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
'use strict'
const { readPackageJson, formatParamUrl, resolveLocalRef } = require('../../util/common')
function prepareDefaultOptions (opts) {
const openapi = opts.openapi
const info = openapi.info || null
const servers = openapi.servers || null
const components = openapi.components || null
const security = openapi.security || null
const tags = openapi.tags || null
const externalDocs = openapi.externalDocs || null
const stripBasePath = opts.stripBasePath
const transform = opts.transform
const hiddenTag = opts.hiddenTag
const extensions = []
for (const [key, value] of Object.entries(opts.openapi)) {
if (key.startsWith('x-')) {
extensions.push([key, value])
}
}
return {
info,
servers,
components,
security,
tags,
externalDocs,
stripBasePath,
transform,
hiddenTag,
extensions
}
}
function prepareOpenapiObject (opts) {
const pkg = readPackageJson()
const openapiObject = {
openapi: '3.0.3',
info: {
version: pkg.version || '1.0.0',
title: pkg.name || ''
},
components: { schemas: {} },
paths: {}
}
if (opts.info) openapiObject.info = opts.info
if (opts.servers) openapiObject.servers = opts.servers
if (opts.components) openapiObject.components = Object.assign({}, opts.components, { schemas: Object.assign({}, opts.components.schemas) })
if (opts.security) openapiObject.security = opts.security
if (opts.tags) openapiObject.tags = opts.tags
if (opts.externalDocs) openapiObject.externalDocs = opts.externalDocs
for (const [key, value] of opts.extensions) {
// "x-" extension can not be typed
openapiObject[key] = value
}
return openapiObject
}
function normalizeUrl (url, servers, stripBasePath) {
if (!stripBasePath) return url
servers = Array.isArray(servers) ? servers : []
servers.forEach(function (server) {
const basePath = new URL(server.url).pathname
if (url.startsWith(basePath) && basePath !== '/') {
url = url.replace(basePath, '')
}
})
return formatParamUrl(url)
}
function transformDefsToComponents (jsonSchema) {
if (typeof jsonSchema === 'object') {
Object.keys(jsonSchema).forEach(function (key) {
if (key === '$ref') {
jsonSchema[key] = jsonSchema[key].replace('definitions', 'components/schemas')
} else {
jsonSchema[key] = transformDefsToComponents(jsonSchema[key])
}
})
}
return jsonSchema
}
// For supported keys read:
// https://swagger.io/docs/specification/describing-parameters/
function plainJsonObjectToOpenapi3 (container, jsonSchema, externalSchemas) {
const obj = transformDefsToComponents(resolveLocalRef(jsonSchema, externalSchemas))
let toOpenapiProp
switch (container) {
case 'cookie':
case 'query':
toOpenapiProp = function (propertyName, jsonSchemaElement) {
const result = {
in: container,
name: propertyName,
required: jsonSchemaElement.required,
schema: jsonSchemaElement
}
// description should be optional
if (jsonSchemaElement.description) result.description = jsonSchemaElement.description
return result
}
break
case 'path':
toOpenapiProp = function (propertyName, jsonSchemaElement) {
const result = {
in: container,
name: propertyName,
required: true,
schema: jsonSchemaElement
}
// description should be optional
if (jsonSchemaElement.description) result.description = jsonSchemaElement.description
return result
}
break
case 'header':
toOpenapiProp = function (propertyName, jsonSchemaElement) {
return {
in: 'header',
name: propertyName,
required: jsonSchemaElement.required,
description: jsonSchemaElement.description,
schema: {
type: jsonSchemaElement.type
}
}
}
break
}
return Object.keys(obj).map((propKey) => {
const jsonSchema = toOpenapiProp(propKey, obj[propKey])
// it is needed as required in schema is invalid prop - delete only if needed
if (typeof jsonSchema.schema.required !== 'undefined') delete jsonSchema.schema.required
// it is needed as description in schema is invalid prop - delete only if needed
if (typeof jsonSchema.schema.description !== 'undefined') delete jsonSchema.schema.description
return jsonSchema
})
}
function resolveBodyParams (content, schema, consumes, ref) {
const resolved = transformDefsToComponents(ref.resolve(schema))
if ((Array.isArray(consumes) && consumes.length === 0) || typeof consumes === 'undefined') {
consumes = ['application/json']
}
consumes.forEach((consume) => {
content[consume] = {
schema: resolved
}
})
}
function resolveCommonParams (container, parameters, schema, ref, sharedSchemas) {
const resolved = transformDefsToComponents(ref.resolve(schema))
const arr = plainJsonObjectToOpenapi3(container, resolved, sharedSchemas)
arr.forEach(swaggerSchema => parameters.push(swaggerSchema))
}
// https://swagger.io/docs/specification/describing-responses/
function resolveResponse (fastifyResponseJson, produces, ref) {
// if the user does not provided an out schema
if (!fastifyResponseJson) {
return { 200: { description: 'Default Response' } }
}
const responsesContainer = {}
const statusCodes = Object.keys(fastifyResponseJson)
statusCodes.forEach(statusCode => {
const rawJsonSchema = fastifyResponseJson[statusCode]
const resolved = transformDefsToComponents(ref.resolve(rawJsonSchema))
// 2xx require to be all upper-case
statusCode = statusCode.toUpperCase()
const response = {
description: resolved['x-response-description'] || rawJsonSchema.description || 'Default Response'
}
// add headers when there are any.
if (rawJsonSchema.headers) {
response.headers = {}
Object.keys(rawJsonSchema.headers).forEach(function (key) {
const header = {
schema: rawJsonSchema.headers[key]
}
if (rawJsonSchema.headers[key].description) {
header.description = rawJsonSchema.headers[key].description
// remove invalid field
delete header.schema.description
}
response.headers[key] = header
})
// remove invalid field
delete resolved.headers
}
if (statusCode.toString() !== '204') {
const content = {}
if ((Array.isArray(produces) && produces.length === 0) || typeof produces === 'undefined') {
produces = ['application/json']
}
delete resolved['x-response-description']
produces.forEach((produce) => {
content[produce] = {
schema: resolved
}
})
response.content = content
}
responsesContainer[statusCode] = response
})
return responsesContainer
}
function prepareOpenapiMethod (schema, ref, openapiObject) {
const openapiMethod = {}
const parameters = []
// All the data the user can give us, is via the schema object
if (schema) {
if (schema.operationId) openapiMethod.operationId = schema.operationId
if (schema.summary) openapiMethod.summary = schema.summary
if (schema.tags) openapiMethod.tags = schema.tags
if (schema.description) openapiMethod.description = schema.description
if (schema.externalDocs) openapiMethod.externalDocs = schema.externalDocs
if (schema.querystring) resolveCommonParams('query', parameters, schema.querystring, ref, openapiObject.definitions)
if (schema.body) {
openapiMethod.requestBody = { content: {} }
resolveBodyParams(openapiMethod.requestBody.content, schema.body, schema.consumes, ref)
}
if (schema.params) resolveCommonParams('path', parameters, schema.params, ref, openapiObject.definitions)
if (schema.headers) resolveCommonParams('header', parameters, schema.headers, ref, openapiObject.definitions)
// TODO: need to documentation, we treat it same as the querystring
// fastify do not support cookies schema in first place
if (schema.cookies) resolveCommonParams('cookie', parameters, schema.cookies, ref, openapiObject.definitions)
if (parameters.length > 0) openapiMethod.parameters = parameters
if (schema.deprecated) openapiMethod.deprecated = schema.deprecated
if (schema.security) openapiMethod.security = schema.security
if (schema.servers) openapiMethod.servers = schema.servers
for (const key of Object.keys(schema)) {
if (key.startsWith('x-')) {
openapiMethod[key] = schema[key]
}
}
}
openapiMethod.responses = resolveResponse(schema ? schema.response : null, schema ? schema.produces : null, ref)
return openapiMethod
}
module.exports = {
prepareDefaultOptions,
prepareOpenapiObject,
prepareOpenapiMethod,
normalizeUrl
}