1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports.makeApisTagOperation = makeApisTagOperation;
|
---|
5 | exports.makeApisTagOperationsOperationExecute = makeApisTagOperationsOperationExecute;
|
---|
6 | exports.makeExecute = makeExecute;
|
---|
7 | exports.mapTagOperations = mapTagOperations;
|
---|
8 | exports.self = void 0;
|
---|
9 | var _index = require("./helpers/index.js");
|
---|
10 | const nullFn = () => null;
|
---|
11 | const normalizeArray = arg => Array.isArray(arg) ? arg : [arg];
|
---|
12 |
|
---|
13 | // To allow stubbing of functions
|
---|
14 | const self = exports.self = {
|
---|
15 | mapTagOperations,
|
---|
16 | makeExecute
|
---|
17 | };
|
---|
18 |
|
---|
19 | // Make an execute, bound to arguments defined in mapTagOperation's callback (cb)
|
---|
20 | function makeExecute(swaggerJs = {}) {
|
---|
21 | return ({
|
---|
22 | pathName,
|
---|
23 | method,
|
---|
24 | operationId
|
---|
25 | }) => (parameters, opts = {}) => {
|
---|
26 | const {
|
---|
27 | requestInterceptor,
|
---|
28 | responseInterceptor,
|
---|
29 | userFetch
|
---|
30 | } = swaggerJs;
|
---|
31 | return swaggerJs.execute({
|
---|
32 | spec: swaggerJs.spec,
|
---|
33 | requestInterceptor,
|
---|
34 | responseInterceptor,
|
---|
35 | userFetch,
|
---|
36 | pathName,
|
---|
37 | method,
|
---|
38 | parameters,
|
---|
39 | operationId,
|
---|
40 | ...opts
|
---|
41 | });
|
---|
42 | };
|
---|
43 | }
|
---|
44 |
|
---|
45 | // Creates an interface with tags+operations = execute
|
---|
46 | // The shape
|
---|
47 | // { apis: { [tag]: { operations: [operation]: { execute }}}}
|
---|
48 | // NOTE: this is mostly for compatibility
|
---|
49 | function makeApisTagOperationsOperationExecute(swaggerJs = {}) {
|
---|
50 | // { apis: tag: operations: execute }
|
---|
51 | const cb = self.makeExecute(swaggerJs);
|
---|
52 | const tagOperations = self.mapTagOperations({
|
---|
53 | v2OperationIdCompatibilityMode: swaggerJs.v2OperationIdCompatibilityMode,
|
---|
54 | spec: swaggerJs.spec,
|
---|
55 | cb
|
---|
56 | });
|
---|
57 | const apis = {};
|
---|
58 | // eslint-disable-next-line no-restricted-syntax, guard-for-in
|
---|
59 | for (const tag in tagOperations) {
|
---|
60 | apis[tag] = {
|
---|
61 | operations: {}
|
---|
62 | };
|
---|
63 | // eslint-disable-next-line no-restricted-syntax, guard-for-in
|
---|
64 | for (const op in tagOperations[tag]) {
|
---|
65 | apis[tag].operations[op] = {
|
---|
66 | execute: tagOperations[tag][op]
|
---|
67 | };
|
---|
68 | }
|
---|
69 | }
|
---|
70 | return {
|
---|
71 | apis
|
---|
72 | };
|
---|
73 | }
|
---|
74 |
|
---|
75 | // .apis[tag][operationId]:ExecuteFunction interface
|
---|
76 | function makeApisTagOperation(swaggerJs = {}) {
|
---|
77 | const cb = self.makeExecute(swaggerJs);
|
---|
78 | return {
|
---|
79 | apis: self.mapTagOperations({
|
---|
80 | v2OperationIdCompatibilityMode: swaggerJs.v2OperationIdCompatibilityMode,
|
---|
81 | spec: swaggerJs.spec,
|
---|
82 | cb
|
---|
83 | })
|
---|
84 | };
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Iterates over a spec, creating a hash of {[tag]: { [operationId], ... }, ...}
|
---|
89 | * with the value of calling `cb`.
|
---|
90 | *
|
---|
91 | * `spec` is a OAI v2.0 compliant specification object
|
---|
92 | * `cb` is called with ({ spec, operation, path, method })
|
---|
93 | * `defaultTag` will house all non-tagged operations
|
---|
94 | *
|
---|
95 | */
|
---|
96 | function mapTagOperations({
|
---|
97 | spec,
|
---|
98 | cb = nullFn,
|
---|
99 | defaultTag = 'default',
|
---|
100 | v2OperationIdCompatibilityMode
|
---|
101 | }) {
|
---|
102 | const operationIdCounter = {};
|
---|
103 | const tagOperations = {}; // Will house all tags + operations
|
---|
104 | (0, _index.eachOperation)(spec, ({
|
---|
105 | pathName,
|
---|
106 | method,
|
---|
107 | operation
|
---|
108 | }) => {
|
---|
109 | const tags = operation.tags ? normalizeArray(operation.tags) : [defaultTag];
|
---|
110 | tags.forEach(tag => {
|
---|
111 | if (typeof tag !== 'string') {
|
---|
112 | return;
|
---|
113 | }
|
---|
114 | tagOperations[tag] = tagOperations[tag] || {};
|
---|
115 | const tagObj = tagOperations[tag];
|
---|
116 | const id = (0, _index.opId)(operation, pathName, method, {
|
---|
117 | v2OperationIdCompatibilityMode
|
---|
118 | });
|
---|
119 | const cbResult = cb({
|
---|
120 | spec,
|
---|
121 | pathName,
|
---|
122 | method,
|
---|
123 | operation,
|
---|
124 | operationId: id
|
---|
125 | });
|
---|
126 | if (operationIdCounter[id]) {
|
---|
127 | operationIdCounter[id] += 1;
|
---|
128 | tagObj[`${id}${operationIdCounter[id]}`] = cbResult;
|
---|
129 | } else if (typeof tagObj[id] !== 'undefined') {
|
---|
130 | // Bump counter ( for this operationId )
|
---|
131 | const originalCounterValue = operationIdCounter[id] || 1;
|
---|
132 | operationIdCounter[id] = originalCounterValue + 1;
|
---|
133 | // Append _x to the operationId
|
---|
134 | tagObj[`${id}${operationIdCounter[id]}`] = cbResult;
|
---|
135 |
|
---|
136 | // Rename the first operationId
|
---|
137 | const temp = tagObj[id];
|
---|
138 | delete tagObj[id];
|
---|
139 | tagObj[`${id}${originalCounterValue}`] = temp;
|
---|
140 | } else {
|
---|
141 | // Assign callback result ( usually a bound function )
|
---|
142 | tagObj[id] = cbResult;
|
---|
143 | }
|
---|
144 | });
|
---|
145 | });
|
---|
146 | return tagOperations;
|
---|
147 | } |
---|