-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
extractPaths.js
48 lines (42 loc) · 1.4 KB
/
extractPaths.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
const { METHODS } = require('../../constants')
const normaliseSecurity = require('../../normalise/v2/normaliseSecurity')
const normaliseOperationId = require('../../normalise/normaliseOperationId')
const normaliseMiddleware = require('../../normalise/normaliseMiddleware')
const normaliseRoute = require('../../normalise/normaliseRoute')
/*
Extracts all of the path data into an array of routes
[
{
method,
route, (normalised and inclues basePath if not a root route)
operationId,
security,
middleware
}
]
*/
const extractPaths = ({ basePath, paths }, options = {}) => {
const {
apiSeparator, // What to swap for `/` in the swagger doc
rootTag = 'root', // The tag that tells us not to prepend the basePath
middleware = {}
} = options
const reduceRoutes = (acc, elem) => {
METHODS.forEach(method => {
const op = paths[elem][method]
if (op) {
const isRoot = Array.isArray(op.tags) && op.tags[0] === rootTag
acc.push({
method,
route: normaliseRoute(`${isRoot ? '' : basePath}${elem}`),
operationId: normaliseOperationId(op.operationId, apiSeparator),
security: normaliseSecurity(op.security),
middleware: normaliseMiddleware(middleware, op['x-middleware'])
})
}
})
return acc
}
return Object.keys(paths).reduce(reduceRoutes, [])
}
module.exports = extractPaths