swagger-client 3.10.8 → 3.10.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/dist/swagger-client.browser.js +24191 -0
- package/dist/swagger-client.browser.min.js +3 -0
- package/dist/swagger-client.browser.min.js.map +1 -0
- package/es/commonjs.js +9 -0
- package/es/constants.js +2 -0
- package/es/execute/index.js +391 -0
- package/es/execute/oas3/build-request.js +149 -0
- package/es/execute/oas3/content-serializer.js +18 -0
- package/es/execute/oas3/parameter-builders.js +119 -0
- package/es/execute/oas3/style-serializer.js +232 -0
- package/es/execute/swagger2/build-request.js +119 -0
- package/es/execute/swagger2/parameter-builders.js +78 -0
- package/es/helpers.js +272 -0
- package/es/http.js +621 -0
- package/es/index.js +116 -0
- package/es/interfaces.js +145 -0
- package/es/internal/form-data-monkey-patch.js +94 -0
- package/es/resolver.js +123 -0
- package/es/specmap/helpers.js +62 -0
- package/es/specmap/index.js +613 -0
- package/es/specmap/lib/all-of.js +81 -0
- package/es/specmap/lib/context-tree.js +111 -0
- package/es/specmap/lib/create-error.js +24 -0
- package/es/specmap/lib/index.js +391 -0
- package/es/specmap/lib/parameters.js +31 -0
- package/es/specmap/lib/properties.js +23 -0
- package/es/specmap/lib/refs.js +516 -0
- package/es/subtree-resolver/index.js +92 -0
- package/lib/commonjs.js +10 -0
- package/lib/constants.js +7 -0
- package/lib/execute/index.js +421 -0
- package/lib/execute/oas3/build-request.js +161 -0
- package/lib/execute/oas3/content-serializer.js +21 -0
- package/lib/execute/oas3/parameter-builders.js +138 -0
- package/lib/execute/oas3/style-serializer.js +208 -0
- package/lib/execute/swagger2/build-request.js +120 -0
- package/lib/execute/swagger2/parameter-builders.js +88 -0
- package/lib/helpers.js +261 -0
- package/lib/http.js +470 -0
- package/lib/index.js +142 -0
- package/lib/interfaces.js +159 -0
- package/lib/internal/form-data-monkey-patch.js +83 -0
- package/lib/resolver.js +125 -0
- package/lib/specmap/helpers.js +65 -0
- package/lib/specmap/index.js +446 -0
- package/lib/specmap/lib/all-of.js +89 -0
- package/lib/specmap/lib/context-tree.js +111 -0
- package/lib/specmap/lib/create-error.js +25 -0
- package/lib/specmap/lib/index.js +402 -0
- package/lib/specmap/lib/parameters.js +42 -0
- package/lib/specmap/lib/properties.js +38 -0
- package/lib/specmap/lib/refs.js +509 -0
- package/lib/subtree-resolver/index.js +55 -0
- package/package.json +80 -106
- package/browser/index.js +0 -54
- package/dist/index.js +0 -4372
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.execute = execute;
|
|
5
|
+
exports.buildRequest = buildRequest;
|
|
6
|
+
exports.baseUrl = baseUrl;
|
|
7
|
+
exports.self = void 0;
|
|
8
|
+
|
|
9
|
+
var _get = _interopRequireDefault(require("lodash/get"));
|
|
10
|
+
|
|
11
|
+
var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject"));
|
|
12
|
+
|
|
13
|
+
var _isArray = _interopRequireDefault(require("lodash/isArray"));
|
|
14
|
+
|
|
15
|
+
var _url = _interopRequireDefault(require("url"));
|
|
16
|
+
|
|
17
|
+
var _cookie = _interopRequireDefault(require("cookie"));
|
|
18
|
+
|
|
19
|
+
var _http = _interopRequireWildcard(require("../http"));
|
|
20
|
+
|
|
21
|
+
var _createError = _interopRequireDefault(require("../specmap/lib/create-error"));
|
|
22
|
+
|
|
23
|
+
var _parameterBuilders = _interopRequireDefault(require("./swagger2/parameter-builders"));
|
|
24
|
+
|
|
25
|
+
var OAS3_PARAMETER_BUILDERS = _interopRequireWildcard(require("./oas3/parameter-builders"));
|
|
26
|
+
|
|
27
|
+
var _buildRequest = _interopRequireDefault(require("./oas3/build-request"));
|
|
28
|
+
|
|
29
|
+
var _buildRequest2 = _interopRequireDefault(require("./swagger2/build-request"));
|
|
30
|
+
|
|
31
|
+
var _helpers = require("../helpers");
|
|
32
|
+
|
|
33
|
+
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
|
34
|
+
|
|
35
|
+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
36
|
+
|
|
37
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
38
|
+
|
|
39
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
40
|
+
|
|
41
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
42
|
+
|
|
43
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
44
|
+
|
|
45
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
46
|
+
|
|
47
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
48
|
+
|
|
49
|
+
const arrayOrEmpty = ar => {
|
|
50
|
+
return Array.isArray(ar) ? ar : [];
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const OperationNotFoundError = (0, _createError.default)('OperationNotFoundError', function cb(message, extra, oriError) {
|
|
54
|
+
this.originalError = oriError;
|
|
55
|
+
Object.assign(this, extra || {});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const findParametersWithName = (name, parameters) => {
|
|
59
|
+
return parameters.filter(p => p.name === name);
|
|
60
|
+
}; // removes parameters that have duplicate 'in' and 'name' properties
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
const deduplicateParameters = parameters => {
|
|
64
|
+
const paramsMap = {};
|
|
65
|
+
parameters.forEach(p => {
|
|
66
|
+
if (!paramsMap[p.in]) {
|
|
67
|
+
paramsMap[p.in] = {};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
paramsMap[p.in][p.name] = p;
|
|
71
|
+
});
|
|
72
|
+
const dedupedParameters = [];
|
|
73
|
+
Object.keys(paramsMap).forEach(i => {
|
|
74
|
+
Object.keys(paramsMap[i]).forEach(p => {
|
|
75
|
+
dedupedParameters.push(paramsMap[i][p]);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
return dedupedParameters;
|
|
79
|
+
}; // For stubbing in tests
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
const self = {
|
|
83
|
+
buildRequest
|
|
84
|
+
}; // Execute request, with the given operationId and parameters
|
|
85
|
+
// pathName/method or operationId is optional
|
|
86
|
+
|
|
87
|
+
exports.self = self;
|
|
88
|
+
|
|
89
|
+
function execute(_ref) {
|
|
90
|
+
let {
|
|
91
|
+
http: userHttp,
|
|
92
|
+
fetch,
|
|
93
|
+
// This is legacy
|
|
94
|
+
spec,
|
|
95
|
+
operationId,
|
|
96
|
+
pathName,
|
|
97
|
+
method,
|
|
98
|
+
parameters,
|
|
99
|
+
securities
|
|
100
|
+
} = _ref,
|
|
101
|
+
extras = _objectWithoutProperties(_ref, ["http", "fetch", "spec", "operationId", "pathName", "method", "parameters", "securities"]);
|
|
102
|
+
|
|
103
|
+
// Provide default fetch implementation
|
|
104
|
+
const http = userHttp || fetch || _http.default; // Default to _our_ http
|
|
105
|
+
|
|
106
|
+
if (pathName && method && !operationId) {
|
|
107
|
+
operationId = (0, _helpers.legacyIdFromPathMethod)(pathName, method);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const request = self.buildRequest(_objectSpread({
|
|
111
|
+
spec,
|
|
112
|
+
operationId,
|
|
113
|
+
parameters,
|
|
114
|
+
securities,
|
|
115
|
+
http
|
|
116
|
+
}, extras));
|
|
117
|
+
|
|
118
|
+
if (request.body && ((0, _isPlainObject.default)(request.body) || (0, _isArray.default)(request.body))) {
|
|
119
|
+
request.body = JSON.stringify(request.body);
|
|
120
|
+
} // Build request and execute it
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
return http(request);
|
|
124
|
+
} // Build a request, which can be handled by the `http.js` implementation.
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
function buildRequest(options) {
|
|
128
|
+
const {
|
|
129
|
+
spec,
|
|
130
|
+
operationId,
|
|
131
|
+
responseContentType,
|
|
132
|
+
scheme,
|
|
133
|
+
requestInterceptor,
|
|
134
|
+
responseInterceptor,
|
|
135
|
+
contextUrl,
|
|
136
|
+
userFetch,
|
|
137
|
+
server,
|
|
138
|
+
serverVariables,
|
|
139
|
+
http
|
|
140
|
+
} = options;
|
|
141
|
+
let {
|
|
142
|
+
parameters,
|
|
143
|
+
parameterBuilders
|
|
144
|
+
} = options;
|
|
145
|
+
const specIsOAS3 = (0, _helpers.isOAS3)(spec);
|
|
146
|
+
|
|
147
|
+
if (!parameterBuilders) {
|
|
148
|
+
// user did not provide custom parameter builders
|
|
149
|
+
if (specIsOAS3) {
|
|
150
|
+
parameterBuilders = OAS3_PARAMETER_BUILDERS;
|
|
151
|
+
} else {
|
|
152
|
+
parameterBuilders = _parameterBuilders.default;
|
|
153
|
+
}
|
|
154
|
+
} // Set credentials with 'http.withCredentials' value
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
const credentials = http && http.withCredentials ? 'include' : 'same-origin'; // Base Template
|
|
158
|
+
|
|
159
|
+
let req = {
|
|
160
|
+
url: '',
|
|
161
|
+
credentials,
|
|
162
|
+
headers: {},
|
|
163
|
+
cookies: {}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
if (requestInterceptor) {
|
|
167
|
+
req.requestInterceptor = requestInterceptor;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (responseInterceptor) {
|
|
171
|
+
req.responseInterceptor = responseInterceptor;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (userFetch) {
|
|
175
|
+
req.userFetch = userFetch;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const operationRaw = (0, _helpers.getOperationRaw)(spec, operationId);
|
|
179
|
+
|
|
180
|
+
if (!operationRaw) {
|
|
181
|
+
throw new OperationNotFoundError(`Operation ${operationId} not found`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const {
|
|
185
|
+
operation = {},
|
|
186
|
+
method,
|
|
187
|
+
pathName
|
|
188
|
+
} = operationRaw;
|
|
189
|
+
req.url += baseUrl({
|
|
190
|
+
spec,
|
|
191
|
+
scheme,
|
|
192
|
+
contextUrl,
|
|
193
|
+
server,
|
|
194
|
+
serverVariables,
|
|
195
|
+
pathName,
|
|
196
|
+
method
|
|
197
|
+
}); // Mostly for testing
|
|
198
|
+
|
|
199
|
+
if (!operationId) {
|
|
200
|
+
// Not removing req.cookies causes testing issues and would
|
|
201
|
+
// change our interface, so we're always sure to remove it.
|
|
202
|
+
// See the same statement lower down in this function for
|
|
203
|
+
// more context.
|
|
204
|
+
delete req.cookies;
|
|
205
|
+
return req;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
req.url += pathName; // Have not yet replaced the path parameters
|
|
209
|
+
|
|
210
|
+
req.method = `${method}`.toUpperCase();
|
|
211
|
+
parameters = parameters || {};
|
|
212
|
+
const path = spec.paths[pathName] || {};
|
|
213
|
+
|
|
214
|
+
if (responseContentType) {
|
|
215
|
+
req.headers.accept = responseContentType;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const combinedParameters = deduplicateParameters([].concat(arrayOrEmpty(operation.parameters)) // operation parameters
|
|
219
|
+
.concat(arrayOrEmpty(path.parameters))); // path parameters
|
|
220
|
+
// REVIEW: OAS3: have any key names or parameter shapes changed?
|
|
221
|
+
// Any new features that need to be plugged in here?
|
|
222
|
+
// Add values to request
|
|
223
|
+
|
|
224
|
+
combinedParameters.forEach(parameter => {
|
|
225
|
+
const builder = parameterBuilders[parameter.in];
|
|
226
|
+
let value;
|
|
227
|
+
|
|
228
|
+
if (parameter.in === 'body' && parameter.schema && parameter.schema.properties) {
|
|
229
|
+
value = parameters;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
value = parameter && parameter.name && parameters[parameter.name];
|
|
233
|
+
|
|
234
|
+
if (typeof value === 'undefined') {
|
|
235
|
+
// check for `name-in` formatted key
|
|
236
|
+
value = parameter && parameter.name && parameters[`${parameter.in}.${parameter.name}`];
|
|
237
|
+
} else if (findParametersWithName(parameter.name, combinedParameters).length > 1) {
|
|
238
|
+
// value came from `parameters[parameter.name]`
|
|
239
|
+
// check to see if this is an ambiguous parameter
|
|
240
|
+
// eslint-disable-next-line no-console
|
|
241
|
+
console.warn(`Parameter '${parameter.name}' is ambiguous because the defined spec has more than one parameter with the name: '${parameter.name}' and the passed-in parameter values did not define an 'in' value.`);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (value === null) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (typeof parameter.default !== 'undefined' && typeof value === 'undefined') {
|
|
249
|
+
value = parameter.default;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (typeof value === 'undefined' && parameter.required && !parameter.allowEmptyValue) {
|
|
253
|
+
throw new Error(`Required parameter ${parameter.name} is not provided`);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (specIsOAS3 && parameter.schema && parameter.schema.type === 'object' && typeof value === 'string') {
|
|
257
|
+
try {
|
|
258
|
+
value = JSON.parse(value);
|
|
259
|
+
} catch (e) {
|
|
260
|
+
throw new Error('Could not parse object parameter value string as JSON');
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (builder) {
|
|
265
|
+
builder({
|
|
266
|
+
req,
|
|
267
|
+
parameter,
|
|
268
|
+
value,
|
|
269
|
+
operation,
|
|
270
|
+
spec
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}); // Do version-specific tasks, then return those results.
|
|
274
|
+
|
|
275
|
+
const versionSpecificOptions = _objectSpread(_objectSpread({}, options), {}, {
|
|
276
|
+
operation
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
if (specIsOAS3) {
|
|
280
|
+
req = (0, _buildRequest.default)(versionSpecificOptions, req);
|
|
281
|
+
} else {
|
|
282
|
+
// If not OAS3, then treat as Swagger2.
|
|
283
|
+
req = (0, _buildRequest2.default)(versionSpecificOptions, req);
|
|
284
|
+
} // If the cookie convenience object exists in our request,
|
|
285
|
+
// serialize its content and then delete the cookie object.
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
if (req.cookies && Object.keys(req.cookies).length) {
|
|
289
|
+
const cookieString = Object.keys(req.cookies).reduce((prev, cookieName) => {
|
|
290
|
+
const cookieValue = req.cookies[cookieName];
|
|
291
|
+
const prefix = prev ? '&' : '';
|
|
292
|
+
|
|
293
|
+
const stringified = _cookie.default.serialize(cookieName, cookieValue);
|
|
294
|
+
|
|
295
|
+
return prev + prefix + stringified;
|
|
296
|
+
}, '');
|
|
297
|
+
req.headers.Cookie = cookieString;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (req.cookies) {
|
|
301
|
+
// even if no cookies were defined, we need to remove
|
|
302
|
+
// the cookies key from our request, or many many legacy
|
|
303
|
+
// tests will break.
|
|
304
|
+
delete req.cookies;
|
|
305
|
+
} // Will add the query object into the URL, if it exists
|
|
306
|
+
// ... will also create a FormData instance, if multipart/form-data (eg: a file)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
(0, _http.mergeInQueryOrForm)(req);
|
|
310
|
+
return req;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const stripNonAlpha = str => str ? str.replace(/\W/g, '') : null; // be careful when modifying this! it is a publicly-exposed method.
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
function baseUrl(obj) {
|
|
317
|
+
const specIsOAS3 = (0, _helpers.isOAS3)(obj.spec);
|
|
318
|
+
return specIsOAS3 ? oas3BaseUrl(obj) : swagger2BaseUrl(obj);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function oas3BaseUrl({
|
|
322
|
+
spec,
|
|
323
|
+
pathName,
|
|
324
|
+
method,
|
|
325
|
+
server,
|
|
326
|
+
contextUrl,
|
|
327
|
+
serverVariables = {}
|
|
328
|
+
}) {
|
|
329
|
+
const servers = (0, _get.default)(spec, ['paths', pathName, (method || '').toLowerCase(), 'servers']) || (0, _get.default)(spec, ['paths', pathName, 'servers']) || (0, _get.default)(spec, ['servers']);
|
|
330
|
+
let selectedServerUrl = '';
|
|
331
|
+
let selectedServerObj = null;
|
|
332
|
+
|
|
333
|
+
if (server && servers && servers.length) {
|
|
334
|
+
const serverUrls = servers.map(srv => srv.url);
|
|
335
|
+
|
|
336
|
+
if (serverUrls.indexOf(server) > -1) {
|
|
337
|
+
selectedServerUrl = server;
|
|
338
|
+
selectedServerObj = servers[serverUrls.indexOf(server)];
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (!selectedServerUrl && servers && servers.length) {
|
|
343
|
+
// default to the first server if we don't have one by now
|
|
344
|
+
selectedServerUrl = servers[0].url; // eslint-disable-line semi
|
|
345
|
+
|
|
346
|
+
[selectedServerObj] = servers;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (selectedServerUrl.indexOf('{') > -1) {
|
|
350
|
+
// do variable substitution
|
|
351
|
+
const varNames = getVariableTemplateNames(selectedServerUrl);
|
|
352
|
+
varNames.forEach(vari => {
|
|
353
|
+
if (selectedServerObj.variables && selectedServerObj.variables[vari]) {
|
|
354
|
+
// variable is defined in server
|
|
355
|
+
const variableDefinition = selectedServerObj.variables[vari];
|
|
356
|
+
const variableValue = serverVariables[vari] || variableDefinition.default;
|
|
357
|
+
const re = new RegExp(`{${vari}}`, 'g');
|
|
358
|
+
selectedServerUrl = selectedServerUrl.replace(re, variableValue);
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return buildOas3UrlWithContext(selectedServerUrl, contextUrl);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function buildOas3UrlWithContext(ourUrl = '', contextUrl = '') {
|
|
367
|
+
const parsedUrl = _url.default.parse(ourUrl);
|
|
368
|
+
|
|
369
|
+
const parsedContextUrl = _url.default.parse(contextUrl);
|
|
370
|
+
|
|
371
|
+
const computedScheme = stripNonAlpha(parsedUrl.protocol) || stripNonAlpha(parsedContextUrl.protocol) || '';
|
|
372
|
+
const computedHost = parsedUrl.host || parsedContextUrl.host;
|
|
373
|
+
const computedPath = parsedUrl.pathname || '';
|
|
374
|
+
let res;
|
|
375
|
+
|
|
376
|
+
if (computedScheme && computedHost) {
|
|
377
|
+
res = `${computedScheme}://${computedHost + computedPath}`; // If last character is '/', trim it off
|
|
378
|
+
} else {
|
|
379
|
+
res = computedPath;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return res[res.length - 1] === '/' ? res.slice(0, -1) : res;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function getVariableTemplateNames(str) {
|
|
386
|
+
const results = [];
|
|
387
|
+
const re = /{([^}]+)}/g;
|
|
388
|
+
let text; // eslint-disable-next-line no-cond-assign
|
|
389
|
+
|
|
390
|
+
while (text = re.exec(str)) {
|
|
391
|
+
results.push(text[1]);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return results;
|
|
395
|
+
} // Compose the baseUrl ( scheme + host + basePath )
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
function swagger2BaseUrl({
|
|
399
|
+
spec,
|
|
400
|
+
scheme,
|
|
401
|
+
contextUrl = ''
|
|
402
|
+
}) {
|
|
403
|
+
const parsedContextUrl = _url.default.parse(contextUrl);
|
|
404
|
+
|
|
405
|
+
const firstSchemeInSpec = Array.isArray(spec.schemes) ? spec.schemes[0] : null;
|
|
406
|
+
const computedScheme = scheme || firstSchemeInSpec || stripNonAlpha(parsedContextUrl.protocol) || 'http';
|
|
407
|
+
const computedHost = spec.host || parsedContextUrl.host || '';
|
|
408
|
+
const computedPath = spec.basePath || '';
|
|
409
|
+
let res;
|
|
410
|
+
|
|
411
|
+
if (computedScheme && computedHost) {
|
|
412
|
+
// we have what we need for an absolute URL
|
|
413
|
+
res = `${computedScheme}://${computedHost + computedPath}`;
|
|
414
|
+
} else {
|
|
415
|
+
// if not, a relative URL will have to do
|
|
416
|
+
res = computedPath;
|
|
417
|
+
} // If last character is '/', trim it off
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
return res[res.length - 1] === '/' ? res.slice(0, -1) : res;
|
|
421
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = buildRequest;
|
|
5
|
+
exports.applySecurities = applySecurities;
|
|
6
|
+
|
|
7
|
+
var _assign = _interopRequireDefault(require("lodash/assign"));
|
|
8
|
+
|
|
9
|
+
var _get = _interopRequireDefault(require("lodash/get"));
|
|
10
|
+
|
|
11
|
+
var _btoa = _interopRequireDefault(require("btoa"));
|
|
12
|
+
|
|
13
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
|
+
|
|
15
|
+
// This function runs after the common function,
|
|
16
|
+
// `src/execute/index.js#buildRequest`
|
|
17
|
+
function buildRequest(options, req) {
|
|
18
|
+
const {
|
|
19
|
+
operation,
|
|
20
|
+
requestBody,
|
|
21
|
+
securities,
|
|
22
|
+
spec,
|
|
23
|
+
attachContentTypeForEmptyPayload
|
|
24
|
+
} = options;
|
|
25
|
+
let {
|
|
26
|
+
requestContentType
|
|
27
|
+
} = options;
|
|
28
|
+
req = applySecurities({
|
|
29
|
+
request: req,
|
|
30
|
+
securities,
|
|
31
|
+
operation,
|
|
32
|
+
spec
|
|
33
|
+
});
|
|
34
|
+
const requestBodyDef = operation.requestBody || {};
|
|
35
|
+
const requestBodyMediaTypes = Object.keys(requestBodyDef.content || {});
|
|
36
|
+
const isExplicitContentTypeValid = requestContentType && requestBodyMediaTypes.indexOf(requestContentType) > -1; // for OAS3: set the Content-Type
|
|
37
|
+
|
|
38
|
+
if (requestBody || attachContentTypeForEmptyPayload) {
|
|
39
|
+
// does the passed requestContentType appear in the requestBody definition?
|
|
40
|
+
if (requestContentType && isExplicitContentTypeValid) {
|
|
41
|
+
req.headers['Content-Type'] = requestContentType;
|
|
42
|
+
} else if (!requestContentType) {
|
|
43
|
+
const firstMediaType = requestBodyMediaTypes[0];
|
|
44
|
+
|
|
45
|
+
if (firstMediaType) {
|
|
46
|
+
req.headers['Content-Type'] = firstMediaType;
|
|
47
|
+
requestContentType = firstMediaType;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} else if (requestContentType && isExplicitContentTypeValid) {
|
|
51
|
+
req.headers['Content-Type'] = requestContentType;
|
|
52
|
+
} // for OAS3: add requestBody to request
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
if (requestBody) {
|
|
56
|
+
if (requestContentType) {
|
|
57
|
+
if (requestBodyMediaTypes.indexOf(requestContentType) > -1) {
|
|
58
|
+
// only attach body if the requestBody has a definition for the
|
|
59
|
+
// contentType that has been explicitly set
|
|
60
|
+
if (requestContentType === 'application/x-www-form-urlencoded' || requestContentType === 'multipart/form-data') {
|
|
61
|
+
if (typeof requestBody === 'object') {
|
|
62
|
+
const encoding = (requestBodyDef.content[requestContentType] || {}).encoding || {};
|
|
63
|
+
req.form = {};
|
|
64
|
+
Object.keys(requestBody).forEach(k => {
|
|
65
|
+
req.form[k] = {
|
|
66
|
+
value: requestBody[k],
|
|
67
|
+
encoding: encoding[k] || {}
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
} else {
|
|
71
|
+
req.form = requestBody;
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
req.body = requestBody;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
req.body = requestBody;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return req;
|
|
83
|
+
} // Add security values, to operations - that declare their need on them
|
|
84
|
+
// Adapted from the Swagger2 implementation
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
function applySecurities({
|
|
88
|
+
request,
|
|
89
|
+
securities = {},
|
|
90
|
+
operation = {},
|
|
91
|
+
spec
|
|
92
|
+
}) {
|
|
93
|
+
const result = (0, _assign.default)({}, request);
|
|
94
|
+
const {
|
|
95
|
+
authorized = {}
|
|
96
|
+
} = securities;
|
|
97
|
+
const security = operation.security || spec.security || [];
|
|
98
|
+
const isAuthorized = authorized && !!Object.keys(authorized).length;
|
|
99
|
+
const securityDef = (0, _get.default)(spec, ['components', 'securitySchemes']) || {};
|
|
100
|
+
result.headers = result.headers || {};
|
|
101
|
+
result.query = result.query || {};
|
|
102
|
+
|
|
103
|
+
if (!Object.keys(securities).length || !isAuthorized || !security || Array.isArray(operation.security) && !operation.security.length) {
|
|
104
|
+
return request;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
security.forEach(securityObj => {
|
|
108
|
+
Object.keys(securityObj).forEach(key => {
|
|
109
|
+
const auth = authorized[key];
|
|
110
|
+
const schema = securityDef[key];
|
|
111
|
+
|
|
112
|
+
if (!auth) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const value = auth.value || auth;
|
|
117
|
+
const {
|
|
118
|
+
type
|
|
119
|
+
} = schema;
|
|
120
|
+
|
|
121
|
+
if (auth) {
|
|
122
|
+
if (type === 'apiKey') {
|
|
123
|
+
if (schema.in === 'query') {
|
|
124
|
+
result.query[schema.name] = value;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (schema.in === 'header') {
|
|
128
|
+
result.headers[schema.name] = value;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (schema.in === 'cookie') {
|
|
132
|
+
result.cookies[schema.name] = value;
|
|
133
|
+
}
|
|
134
|
+
} else if (type === 'http') {
|
|
135
|
+
if (/^basic$/i.test(schema.scheme)) {
|
|
136
|
+
const username = value.username || '';
|
|
137
|
+
const password = value.password || '';
|
|
138
|
+
const encoded = (0, _btoa.default)(`${username}:${password}`);
|
|
139
|
+
result.headers.Authorization = `Basic ${encoded}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (/^bearer$/i.test(schema.scheme)) {
|
|
143
|
+
result.headers.Authorization = `Bearer ${value}`;
|
|
144
|
+
}
|
|
145
|
+
} else if (type === 'oauth2') {
|
|
146
|
+
const token = auth.token || {};
|
|
147
|
+
const tokenName = schema['x-tokenName'] || 'access_token';
|
|
148
|
+
const tokenValue = token[tokenName];
|
|
149
|
+
let tokenType = token.token_type;
|
|
150
|
+
|
|
151
|
+
if (!tokenType || tokenType.toLowerCase() === 'bearer') {
|
|
152
|
+
tokenType = 'Bearer';
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
result.headers.Authorization = `${tokenType} ${tokenValue}`;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = serialize;
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
Serializer that serializes according to a media type instead of OpenAPI's
|
|
8
|
+
`style` + `explode` constructs.
|
|
9
|
+
*/
|
|
10
|
+
function serialize(value, mediaType) {
|
|
11
|
+
if (mediaType.includes('application/json')) {
|
|
12
|
+
if (typeof value === 'string') {
|
|
13
|
+
// Assume the user has a JSON string
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return JSON.stringify(value);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return value.toString();
|
|
21
|
+
}
|