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