-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path52.js
More file actions
136 lines (110 loc) · 3.53 KB
/
Copy path52.js
File metadata and controls
136 lines (110 loc) · 3.53 KB
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
'use strict'
const joiToSchema = require('joi-to-json-schema')
const {get} = require('lodash')
const addReference = function (spec) {
const info = typeof spec.describe === 'function' ? spec.describe() : spec
const id = get(info, 'meta.0.id')
if (id) return {$ref: `#/components/${id}`}
else if (spec.isJoi) return joiToSchema(spec)
else return spec
}
const parseResponses = function (route) {
const responses = {}
let specObject = get(route, 'config.response.status')
// Get the pairs and sort by HTTP code (lower first)
const specPairs = Object.entries(specObject).sort((a, b) => a[0] - b[0])
// For each response
for (const [code, response] of specPairs) {
// Get its info and any reference id
const info = response.describe()
if (code === '204') { // No body reply
responses[code.toString()] = {description: info.description}
} else { // Assign the new response, either with a reference or by converting the object
responses[code.toString()] = {
description: info.description,
content: {
'application/json': {
schema: addReference(response)
}
}
}
}
}
return responses
}
const parseParameters = function (route) {
// If there is a already defined format, use it
let specObject = get(route, 'config.validate.params')
if (!specObject) return null
return Object.entries(specObject).map(([name, spec]) => {
const info = spec.describe()
return {
name,
in: 'path',
description: info.description,
required: get(info, 'flags.presence') === 'required',
schema: addReference(info)
}
})
}
const parseQuerystring = function (route) {
// If there is a already defined format, use it
let specObject = get(route, 'config.validate.query')
if (!specObject) return null
return Object.entries(specObject).map(([name, spec]) => {
const info = spec.describe()
return {
name,
in: 'query',
description: info.description,
required: get(info, 'flags.presence') === 'required',
schema: addReference(info)
}
})
}
const parseBody = function (route) {
// If there is a already defined format, use it
let specObject = get(route, 'config.validate.payload')
if (!specObject) return null
return {
description: specObject.description,
content: {
'application/json': {
schema: addReference(specObject)
}
}
}
}
module.exports = (function () {
const routes = []
return {
addApiRoute (server, collection, routeSpec) {
if (!routes[collection]) routes[collection] = []
routes[collection].push(routeSpec)
return server.route(routeSpec)
},
generateSpec (spec, collection) {
// Sort by path
const apiRoutes = routes[collection].sort((a, b) => a.path.localeCompare(b.path))
// Add each route to the path
for (const route of apiRoutes) {
// Make sure routes are grouped by path
const path = route.path
if (!spec.paths.hasOwnProperty(path)) spec.paths[path] = {}
const {description, tags} = route.config
// Add the route to the path group
spec.paths[path][route.method.toLowerCase()] = {
summary: description,
tags: tags.filter(t => t !== 'api'),
responses: parseResponses(route),
requestBody: parseBody(route),
parameters: [
parseParameters(route),
parseQuerystring(route)
].filter(l => l).reduce((a, b) => a.concat(b), [])
}
}
return spec
}
}
})()