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