source: node_modules/swagger-client/es/resolver/strategies/generic/normalize.js@ 65b6638

main
Last change on this file since 65b6638 was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 3.4 KB
Line 
1import opId from '../../../helpers/op-id.js';
2export default function normalize(parsedSpec) {
3 const {
4 spec
5 } = parsedSpec;
6 const {
7 paths
8 } = spec;
9 const map = {};
10 if (!paths || spec.$$normalized) {
11 return parsedSpec;
12 }
13
14 // eslint-disable-next-line no-restricted-syntax, guard-for-in
15 for (const pathName in paths) {
16 const path = paths[pathName];
17 if (path == null || !['object', 'function'].includes(typeof path)) {
18 continue; // eslint-disable-line no-continue
19 }
20 const pathParameters = path.parameters;
21
22 // eslint-disable-next-line no-restricted-syntax, guard-for-in
23 for (const method in path) {
24 const operation = path[method];
25 if (operation == null || !['object', 'function'].includes(typeof operation)) {
26 continue; // eslint-disable-line no-continue
27 }
28 const oid = opId(operation, pathName, method);
29 if (oid) {
30 if (map[oid]) {
31 map[oid].push(operation);
32 } else {
33 map[oid] = [operation];
34 }
35 const opList = map[oid];
36 if (opList.length > 1) {
37 opList.forEach((o, i) => {
38 // eslint-disable-next-line no-underscore-dangle
39 o.__originalOperationId = o.__originalOperationId || o.operationId;
40 o.operationId = `${oid}${i + 1}`;
41 });
42 } else if (typeof operation.operationId !== 'undefined') {
43 // Ensure we always add the normalized operation ID if one already exists
44 // ( potentially different, given that we normalize our IDs)
45 // ... _back_ to the spec. Otherwise, they might not line up
46 const obj = opList[0];
47 // eslint-disable-next-line no-underscore-dangle
48 obj.__originalOperationId = obj.__originalOperationId || operation.operationId;
49 obj.operationId = oid;
50 }
51 }
52 if (method !== 'parameters') {
53 // Add inherited consumes, produces, parameters, securities
54 const inheritsList = [];
55 const toBeInherit = {};
56
57 // Global-levels
58 // eslint-disable-next-line no-restricted-syntax
59 for (const key in spec) {
60 if (key === 'produces' || key === 'consumes' || key === 'security') {
61 toBeInherit[key] = spec[key];
62 inheritsList.push(toBeInherit);
63 }
64 }
65
66 // Path-levels
67 if (pathParameters) {
68 toBeInherit.parameters = pathParameters;
69 inheritsList.push(toBeInherit);
70 }
71 if (inheritsList.length) {
72 // eslint-disable-next-line no-restricted-syntax
73 for (const inherits of inheritsList) {
74 // eslint-disable-next-line no-restricted-syntax
75 for (const inheritName in inherits) {
76 if (!operation[inheritName]) {
77 operation[inheritName] = inherits[inheritName];
78 } else if (inheritName === 'parameters') {
79 // eslint-disable-next-line no-restricted-syntax
80 for (const param of inherits[inheritName]) {
81 const exists = operation[inheritName].some(opParam => opParam.name && opParam.name === param.name || opParam.$ref && opParam.$ref === param.$ref || opParam.$$ref && opParam.$$ref === param.$$ref || opParam === param);
82 if (!exists) {
83 operation[inheritName].push(param);
84 }
85 }
86 }
87 }
88 }
89 }
90 }
91 }
92 }
93 spec.$$normalized = true;
94 return parsedSpec;
95}
Note: See TracBrowser for help on using the repository browser.