forked from swagger-api/swagger-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
61 lines (51 loc) · 1.6 KB
/
helpers.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
const operationKeys = ["get", "post", "put", "delete", "options", "head", "patch", "trace"]
export const PATH_TEMPLATES_REGEX = /\{(.*?)\}/g
export function checkForDefinition(paramName, pathItem) {
const pathItemParameters = pathItem.parameters
const operationsInPathItem = (Object.keys(pathItem) || [])
.filter(key => operationKeys.indexOf(key) > -1)
.map(key => {
const obj = pathItem[key]
obj.method = key
return obj
})
const res = {
found: false,
inPath: false,
inOperation: false,
caseMatch: false,
paramCase: "",
missingFromOperations: []
}
// Look at the path parameters
if(Array.isArray(pathItemParameters)) {
pathItemParameters.forEach(param => {
if(param.name === paramName && param.in === "path") {
res.found = true
res.inPath = true
}
})
}
// Next, look at the operations...
if(!res.found && operationsInPathItem.length) {
operationsInPathItem
.forEach(op => {
const inThisOperation = (op.parameters || [])
.some(param => param.name === paramName && param.in === "path")
const caseMatch = (op.parameters || [])
.find(param => !(param.name === paramName) && (param.name.toLowerCase() === paramName.toLowerCase()) && param.in === "path")
if(inThisOperation) {
res.found = true
res.inOperation = true
}
if(caseMatch) {
res.caseMatch = true
res.paramCase = caseMatch.name
}
if(!inThisOperation) {
res.missingFromOperations.push(op.method)
}
})
}
return res
}