UNPKG

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