UNPKG

8.81 kBJavaScriptView Raw
1import _createForOfIteratorHelper from "@babel/runtime-corejs2/helpers/createForOfIteratorHelper";
2import _typeof from "@babel/runtime-corejs2/helpers/typeof";
3import isObject from 'lodash/isObject';
4import startsWith from 'lodash/startsWith';
5
6var toLower = function toLower(str) {
7 return String.prototype.toLowerCase.call(str);
8};
9
10var escapeString = function escapeString(str) {
11 return str.replace(/[^\w]/gi, '_');
12}; // Spec version detection
13
14
15export function isOAS3(spec) {
16 var oasVersion = spec.openapi;
17
18 if (!oasVersion) {
19 return false;
20 }
21
22 return startsWith(oasVersion, '3');
23}
24export function isSwagger2(spec) {
25 var swaggerVersion = spec.swagger;
26
27 if (!swaggerVersion) {
28 return false;
29 }
30
31 return startsWith(swaggerVersion, '2');
32} // Strategy for determining operationId
33
34export function opId(operation, pathName) {
35 var method = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
36
37 var _ref = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
38 v2OperationIdCompatibilityMode = _ref.v2OperationIdCompatibilityMode;
39
40 if (!operation || _typeof(operation) !== 'object') {
41 return null;
42 }
43
44 var idWithoutWhitespace = (operation.operationId || '').replace(/\s/g, '');
45
46 if (idWithoutWhitespace.length) {
47 return escapeString(operation.operationId);
48 }
49
50 return idFromPathMethod(pathName, method, {
51 v2OperationIdCompatibilityMode: v2OperationIdCompatibilityMode
52 });
53} // Create a generated operationId from pathName + method
54
55export function idFromPathMethod(pathName, method) {
56 var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
57 v2OperationIdCompatibilityMode = _ref2.v2OperationIdCompatibilityMode;
58
59 if (v2OperationIdCompatibilityMode) {
60 var res = "".concat(method.toLowerCase(), "_").concat(pathName).replace(/[\s!@#$%^&*()_+=[{\]};:<>|./?,\\'""-]/g, '_');
61 res = res || "".concat(pathName.substring(1), "_").concat(method);
62 return res.replace(/((_){2,})/g, '_').replace(/^(_)*/g, '').replace(/([_])*$/g, '');
63 }
64
65 return "".concat(toLower(method)).concat(escapeString(pathName));
66}
67export function legacyIdFromPathMethod(pathName, method) {
68 return "".concat(toLower(method), "-").concat(pathName);
69} // Get the operation, based on operationId ( just return the object, no inheritence )
70
71export function getOperationRaw(spec, id) {
72 if (!spec || !spec.paths) {
73 return null;
74 }
75
76 return findOperation(spec, function (_ref3) {
77 var pathName = _ref3.pathName,
78 method = _ref3.method,
79 operation = _ref3.operation;
80
81 if (!operation || _typeof(operation) !== 'object') {
82 return false;
83 }
84
85 var rawOperationId = operation.operationId; // straight from the source
86
87 var operationId = opId(operation, pathName, method);
88 var legacyOperationId = legacyIdFromPathMethod(pathName, method);
89 return [operationId, legacyOperationId, rawOperationId].some(function (val) {
90 return val && val === id;
91 });
92 });
93} // Will stop iterating over the operations and return the operationObj
94// as soon as predicate returns true
95
96export function findOperation(spec, predicate) {
97 return eachOperation(spec, predicate, true) || null;
98} // iterate over each operation, and fire a callback with details
99// `find=true` will stop iterating, when the cb returns truthy
100
101export function eachOperation(spec, cb, find) {
102 if (!spec || _typeof(spec) !== 'object' || !spec.paths || _typeof(spec.paths) !== 'object') {
103 return null;
104 }
105
106 var paths = spec.paths; // Iterate over the spec, collecting operations
107 // eslint-disable-next-line no-restricted-syntax, guard-for-in
108
109 for (var pathName in paths) {
110 // eslint-disable-next-line no-restricted-syntax, guard-for-in
111 for (var method in paths[pathName]) {
112 if (method.toUpperCase() === 'PARAMETERS') {
113 continue; // eslint-disable-line no-continue
114 }
115
116 var operation = paths[pathName][method];
117
118 if (!operation || _typeof(operation) !== 'object') {
119 continue; // eslint-disable-line no-continue
120 }
121
122 var operationObj = {
123 spec: spec,
124 pathName: pathName,
125 method: method.toUpperCase(),
126 operation: operation
127 };
128 var cbValue = cb(operationObj);
129
130 if (find && cbValue) {
131 return operationObj;
132 }
133 }
134 }
135
136 return undefined;
137} // REVIEW: OAS3: identify normalization steps that need changes
138// ...maybe create `normalizeOAS3`?
139
140export function normalizeSwagger(parsedSpec) {
141 var spec = parsedSpec.spec;
142 var paths = spec.paths;
143 var map = {};
144
145 if (!paths || spec.$$normalized) {
146 return parsedSpec;
147 } // eslint-disable-next-line no-restricted-syntax, guard-for-in
148
149
150 for (var pathName in paths) {
151 var path = paths[pathName];
152
153 if (!isObject(path)) {
154 continue; // eslint-disable-line no-continue
155 }
156
157 var pathParameters = path.parameters; // eslint-disable-next-line no-restricted-syntax, guard-for-in
158
159 var _loop = function _loop(method) {
160 var operation = path[method];
161
162 if (!isObject(operation)) {
163 return "continue"; // eslint-disable-line no-continue
164 }
165
166 var oid = opId(operation, pathName, method);
167
168 if (oid) {
169 if (map[oid]) {
170 map[oid].push(operation);
171 } else {
172 map[oid] = [operation];
173 }
174
175 var opList = map[oid];
176
177 if (opList.length > 1) {
178 opList.forEach(function (o, i) {
179 // eslint-disable-next-line no-underscore-dangle
180 o.__originalOperationId = o.__originalOperationId || o.operationId;
181 o.operationId = "".concat(oid).concat(i + 1);
182 });
183 } else if (typeof operation.operationId !== 'undefined') {
184 // Ensure we always add the normalized operation ID if one already exists
185 // ( potentially different, given that we normalize our IDs)
186 // ... _back_ to the spec. Otherwise, they might not line up
187 var obj = opList[0]; // eslint-disable-next-line no-underscore-dangle
188
189 obj.__originalOperationId = obj.__originalOperationId || operation.operationId;
190 obj.operationId = oid;
191 }
192 }
193
194 if (method !== 'parameters') {
195 // Add inherited consumes, produces, parameters, securities
196 var inheritsList = [];
197 var toBeInherit = {}; // Global-levels
198 // eslint-disable-next-line no-restricted-syntax
199
200 for (var key in spec) {
201 if (key === 'produces' || key === 'consumes' || key === 'security') {
202 toBeInherit[key] = spec[key];
203 inheritsList.push(toBeInherit);
204 }
205 } // Path-levels
206
207
208 if (pathParameters) {
209 toBeInherit.parameters = pathParameters;
210 inheritsList.push(toBeInherit);
211 }
212
213 if (inheritsList.length) {
214 // eslint-disable-next-line no-restricted-syntax
215 var _iterator = _createForOfIteratorHelper(inheritsList),
216 _step;
217
218 try {
219 for (_iterator.s(); !(_step = _iterator.n()).done;) {
220 var inherits = _step.value;
221
222 // eslint-disable-next-line no-restricted-syntax
223 for (var inheritName in inherits) {
224 if (!operation[inheritName]) {
225 operation[inheritName] = inherits[inheritName];
226 } else if (inheritName === 'parameters') {
227 // eslint-disable-next-line no-restricted-syntax
228 var _iterator2 = _createForOfIteratorHelper(inherits[inheritName]),
229 _step2;
230
231 try {
232 var _loop2 = function _loop2() {
233 var param = _step2.value;
234 var exists = operation[inheritName].some(function (opParam) {
235 return opParam.name && opParam.name === param.name || opParam.$ref && opParam.$ref === param.$ref || opParam.$$ref && opParam.$$ref === param.$$ref || opParam === param;
236 });
237
238 if (!exists) {
239 operation[inheritName].push(param);
240 }
241 };
242
243 for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
244 _loop2();
245 }
246 } catch (err) {
247 _iterator2.e(err);
248 } finally {
249 _iterator2.f();
250 }
251 }
252 }
253 }
254 } catch (err) {
255 _iterator.e(err);
256 } finally {
257 _iterator.f();
258 }
259 }
260 }
261 };
262
263 for (var method in path) {
264 var _ret = _loop(method);
265
266 if (_ret === "continue") continue;
267 }
268 }
269
270 spec.$$normalized = true;
271 return parsedSpec;
272}
\No newline at end of file