swagger-client 3.27.1 → 3.27.3

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.
Files changed (48) hide show
  1. package/dist/swagger-client.browser.js +10806 -11986
  2. package/dist/swagger-client.browser.min.js +1 -1
  3. package/dist/swagger-client.browser.min.js.map +1 -1
  4. package/es/execute/index.js +5 -8
  5. package/es/execute/oas3/build-request.js +22 -1
  6. package/es/http/index.js +6 -370
  7. package/es/http/serializers/request/file.js +46 -0
  8. package/es/http/serializers/request/format.js +143 -0
  9. package/es/http/serializers/request/index.js +111 -0
  10. package/es/http/serializers/response/index.js +55 -0
  11. package/es/index.js +2 -1
  12. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/errors/SchemaRefError.js +3 -0
  13. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/visitors/dereference.js +5 -4
  14. package/es/{specmap → resolver/specmap}/helpers.js +1 -1
  15. package/es/{specmap → resolver/specmap}/lib/refs.js +8 -8
  16. package/es/resolver/strategies/generic/index.js +1 -1
  17. package/es/resolver/strategies/generic/resolve.js +1 -1
  18. package/lib/execute/index.js +8 -11
  19. package/lib/execute/oas3/build-request.js +22 -1
  20. package/lib/http/index.js +6 -379
  21. package/lib/http/serializers/request/file.js +53 -0
  22. package/lib/http/serializers/request/format.js +147 -0
  23. package/lib/http/serializers/request/index.js +117 -0
  24. package/lib/http/serializers/response/index.js +63 -0
  25. package/lib/index.js +34 -33
  26. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/errors/SchemaRefError.js +7 -0
  27. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/visitors/dereference.js +6 -5
  28. package/lib/{specmap → resolver/specmap}/helpers.js +1 -1
  29. package/lib/{specmap → resolver/specmap}/lib/refs.js +8 -8
  30. package/lib/resolver/strategies/generic/index.js +1 -1
  31. package/lib/resolver/strategies/generic/resolve.js +1 -1
  32. package/package.json +1 -1
  33. package/es/resolver/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/errors/index.js +0 -7
  34. package/es/specmap/lib/create-error.js +0 -17
  35. package/lib/resolver/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/errors/index.js +0 -11
  36. package/lib/specmap/lib/create-error.js +0 -21
  37. /package/es/{specmap → resolver/specmap}/index.js +0 -0
  38. /package/es/{specmap → resolver/specmap}/lib/all-of.js +0 -0
  39. /package/es/{specmap → resolver/specmap}/lib/context-tree.js +0 -0
  40. /package/es/{specmap → resolver/specmap}/lib/index.js +0 -0
  41. /package/es/{specmap → resolver/specmap}/lib/parameters.js +0 -0
  42. /package/es/{specmap → resolver/specmap}/lib/properties.js +0 -0
  43. /package/lib/{specmap → resolver/specmap}/index.js +0 -0
  44. /package/lib/{specmap → resolver/specmap}/lib/all-of.js +0 -0
  45. /package/lib/{specmap → resolver/specmap}/lib/context-tree.js +0 -0
  46. /package/lib/{specmap → resolver/specmap}/lib/index.js +0 -0
  47. /package/lib/{specmap → resolver/specmap}/lib/parameters.js +0 -0
  48. /package/lib/{specmap → resolver/specmap}/lib/properties.js +0 -0
package/lib/http/index.js CHANGED
@@ -1,26 +1,12 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
3
  exports.__esModule = true;
5
4
  exports.default = http;
6
- exports.encodeFormOrQuery = encodeFormOrQuery;
7
- exports.isFile = isFile;
8
5
  exports.makeHttp = makeHttp;
9
- exports.mergeInQueryOrForm = mergeInQueryOrForm;
10
- exports.self = void 0;
11
- exports.serializeHeaders = serializeHeaders;
12
- exports.serializeRes = serializeRes;
13
- exports.shouldDownloadAsText = void 0;
14
- var _qs = _interopRequireDefault(require("qs"));
15
- var _jsYaml = _interopRequireDefault(require("js-yaml"));
16
6
  require("../helpers/fetch-polyfill.node.js");
17
- var _styleSerializer = require("../execute/oas3/style-serializer.js");
18
- // For testing
19
- const self = exports.self = {
20
- serializeRes,
21
- mergeInQueryOrForm
22
- };
23
-
7
+ var _index = require("./serializers/request/index.js");
8
+ var _index2 = require("./serializers/response/index.js");
9
+ exports.serializeRes = _index2.serializeResponse;
24
10
  // Handles fetch-like syntax and the case where there is only one object passed-in
25
11
  // (which will have the URL as a property). Also serializes the response.
26
12
  async function http(url, request = {}) {
@@ -33,7 +19,7 @@ async function http(url, request = {}) {
33
19
  // Serializes query, for convenience
34
20
  // Should be the last thing we do, as its hard to mutate the URL with
35
21
  // the search string, but much easier to manipulate the req.query object
36
- self.mergeInQueryOrForm(request);
22
+ request = (0, _index.serializeRequest)(request);
37
23
 
38
24
  // Newlines in header values cause weird error messages from `window.fetch`,
39
25
  // so let's message them out.
@@ -68,7 +54,7 @@ async function http(url, request = {}) {
68
54
  let res;
69
55
  try {
70
56
  res = await (request.userFetch || fetch)(request.url, request);
71
- res = await self.serializeRes(res, url, request);
57
+ res = await (0, _index2.serializeResponse)(res, url, request);
72
58
  if (request.responseInterceptor) {
73
59
  res = (await request.responseInterceptor(res)) || res;
74
60
  }
@@ -94,365 +80,6 @@ async function http(url, request = {}) {
94
80
  return res;
95
81
  }
96
82
 
97
- // exported for testing
98
- const shouldDownloadAsText = (contentType = '') => /(json|xml|yaml|text)\b/.test(contentType);
99
- exports.shouldDownloadAsText = shouldDownloadAsText;
100
- function parseBody(body, contentType) {
101
- if (contentType && (contentType.indexOf('application/json') === 0 || contentType.indexOf('+json') > 0)) {
102
- return JSON.parse(body);
103
- }
104
- return _jsYaml.default.load(body);
105
- }
106
-
107
- // Serialize the response, returns a promise with headers and the body part of the hash
108
- function serializeRes(oriRes, url, {
109
- loadSpec = false
110
- } = {}) {
111
- const res = {
112
- ok: oriRes.ok,
113
- url: oriRes.url || url,
114
- status: oriRes.status,
115
- statusText: oriRes.statusText,
116
- headers: serializeHeaders(oriRes.headers)
117
- };
118
- const contentType = res.headers['content-type'];
119
- const useText = loadSpec || shouldDownloadAsText(contentType);
120
- const getBody = useText ? oriRes.text : oriRes.blob || oriRes.buffer;
121
- return getBody.call(oriRes).then(body => {
122
- res.text = body;
123
- res.data = body;
124
- if (useText) {
125
- try {
126
- const obj = parseBody(body, contentType);
127
- res.body = obj;
128
- res.obj = obj;
129
- } catch (e) {
130
- res.parseError = e;
131
- }
132
- }
133
- return res;
134
- });
135
- }
136
- function serializeHeaderValue(value) {
137
- const isMulti = value.includes(', ');
138
- return isMulti ? value.split(', ') : value;
139
- }
140
-
141
- // Serialize headers into a hash, where mutliple-headers result in an array.
142
- //
143
- // eg: Cookie: one
144
- // Cookie: two
145
- // = { Cookie: [ "one", "two" ]
146
- function serializeHeaders(headers = {}) {
147
- if (typeof headers.entries !== 'function') return {};
148
- return Array.from(headers.entries()).reduce((acc, [header, value]) => {
149
- acc[header] = serializeHeaderValue(value);
150
- return acc;
151
- }, {});
152
- }
153
- function isFile(obj, navigatorObj) {
154
- if (!navigatorObj && typeof navigator !== 'undefined') {
155
- // eslint-disable-next-line no-undef
156
- navigatorObj = navigator;
157
- }
158
- if (navigatorObj && navigatorObj.product === 'ReactNative') {
159
- if (obj && typeof obj === 'object' && typeof obj.uri === 'string') {
160
- return true;
161
- }
162
- return false;
163
- }
164
- if (typeof File !== 'undefined' && obj instanceof File) {
165
- return true;
166
- }
167
- if (typeof Blob !== 'undefined' && obj instanceof Blob) {
168
- return true;
169
- }
170
- if (ArrayBuffer.isView(obj)) {
171
- return true;
172
- }
173
- return obj !== null && typeof obj === 'object' && typeof obj.pipe === 'function';
174
- }
175
- function isArrayOfFile(obj, navigatorObj) {
176
- return Array.isArray(obj) && obj.some(v => isFile(v, navigatorObj));
177
- }
178
- const STYLE_SEPARATORS = {
179
- form: ',',
180
- spaceDelimited: '%20',
181
- pipeDelimited: '|'
182
- };
183
- const SEPARATORS = {
184
- csv: ',',
185
- ssv: '%20',
186
- tsv: '%09',
187
- pipes: '|'
188
- };
189
-
190
- /**
191
- * Specialized sub-class of File class, that only
192
- * accepts string data and retain this data in `data`
193
- * public property throughout the lifecycle of its instances.
194
- *
195
- * This sub-class is exclusively used only when Encoding Object
196
- * is defined within the Media Type Object (OpenAPI 3.x.y).
197
- */
198
- class FileWithData extends File {
199
- constructor(data, name = '', options = {}) {
200
- super([data], name, options);
201
- this.data = data;
202
- }
203
- valueOf() {
204
- return this.data;
205
- }
206
- toString() {
207
- return this.valueOf();
208
- }
209
- }
210
-
211
- // Formats a key-value and returns an array of key-value pairs.
212
- //
213
- // Return value example 1: [['color', 'blue']]
214
- // Return value example 2: [['color', 'blue,black,brown']]
215
- // Return value example 3: [['color', ['blue', 'black', 'brown']]]
216
- // Return value example 4: [['color', 'R,100,G,200,B,150']]
217
- // Return value example 5: [['R', '100'], ['G', '200'], ['B', '150']]
218
- // Return value example 6: [['color[R]', '100'], ['color[G]', '200'], ['color[B]', '150']]
219
- function formatKeyValue(key, input, skipEncoding = false) {
220
- const {
221
- collectionFormat,
222
- allowEmptyValue,
223
- serializationOption,
224
- encoding
225
- } = input;
226
- // `input` can be string
227
- const value = typeof input === 'object' && !Array.isArray(input) ? input.value : input;
228
- const encodeFn = skipEncoding ? k => k.toString() : k => encodeURIComponent(k);
229
- const encodedKey = encodeFn(key);
230
- if (typeof value === 'undefined' && allowEmptyValue) {
231
- return [[encodedKey, '']];
232
- }
233
-
234
- // file
235
- if (isFile(value) || isArrayOfFile(value)) {
236
- return [[encodedKey, value]];
237
- }
238
-
239
- // for OAS 3 Parameter Object for serialization
240
- if (serializationOption) {
241
- return formatKeyValueBySerializationOption(key, value, skipEncoding, serializationOption);
242
- }
243
-
244
- // for OAS 3 Encoding Object
245
- if (encoding) {
246
- if ([typeof encoding.style, typeof encoding.explode, typeof encoding.allowReserved].some(type => type !== 'undefined')) {
247
- const {
248
- style,
249
- explode,
250
- allowReserved
251
- } = encoding;
252
- return formatKeyValueBySerializationOption(key, value, skipEncoding, {
253
- style,
254
- explode,
255
- allowReserved
256
- });
257
- }
258
- if (typeof encoding.contentType === 'string') {
259
- if (encoding.contentType.startsWith('application/json')) {
260
- // if value is a string, assume value is already a JSON string
261
- const json = typeof value === 'string' ? value : JSON.stringify(value);
262
- const encodedJson = encodeFn(json);
263
- const file = new FileWithData(encodedJson, 'blob', {
264
- type: encoding.contentType
265
- });
266
- return [[encodedKey, file]];
267
- }
268
- const encodedData = encodeFn(String(value));
269
- const blob = new FileWithData(encodedData, 'blob', {
270
- type: encoding.contentType
271
- });
272
- return [[encodedKey, blob]];
273
- }
274
-
275
- // Primitive
276
- if (typeof value !== 'object') {
277
- return [[encodedKey, encodeFn(value)]];
278
- }
279
-
280
- // Array of primitives
281
- if (Array.isArray(value) && value.every(v => typeof v !== 'object')) {
282
- return [[encodedKey, value.map(encodeFn).join(',')]];
283
- }
284
-
285
- // Array or object
286
- return [[encodedKey, encodeFn(JSON.stringify(value))]];
287
- }
288
-
289
- // for OAS 2 Parameter Object
290
- // Primitive
291
- if (typeof value !== 'object') {
292
- return [[encodedKey, encodeFn(value)]];
293
- }
294
-
295
- // Array
296
- if (Array.isArray(value)) {
297
- if (collectionFormat === 'multi') {
298
- // In case of multipart/formdata, it is used as array.
299
- // Otherwise, the caller will convert it to a query by qs.stringify.
300
- return [[encodedKey, value.map(encodeFn)]];
301
- }
302
- return [[encodedKey, value.map(encodeFn).join(SEPARATORS[collectionFormat || 'csv'])]];
303
- }
304
-
305
- // Object
306
- return [[encodedKey, '']];
307
- }
308
- function formatKeyValueBySerializationOption(key, value, skipEncoding, serializationOption) {
309
- const style = serializationOption.style || 'form';
310
- const explode = typeof serializationOption.explode === 'undefined' ? style === 'form' : serializationOption.explode;
311
- // eslint-disable-next-line no-nested-ternary
312
- const escape = skipEncoding ? false : serializationOption && serializationOption.allowReserved ? 'unsafe' : 'reserved';
313
- const encodeFn = v => (0, _styleSerializer.valueEncoder)(v, escape);
314
- const encodeKeyFn = skipEncoding ? k => k : k => encodeFn(k);
315
- if (typeof value === 'string') {
316
- try {
317
- value = JSON.parse(value);
318
- } catch {
319
- // can't parse the value so treat it as as a simple string
320
- }
321
- }
322
-
323
- // Primitive
324
- if (typeof value !== 'object') {
325
- return [[encodeKeyFn(key), encodeFn(value)]];
326
- }
327
-
328
- // Array
329
- if (Array.isArray(value)) {
330
- if (explode) {
331
- // In case of multipart/formdata, it is used as array.
332
- // Otherwise, the caller will convert it to a query by qs.stringify.
333
- return [[encodeKeyFn(key), value.map(encodeFn)]];
334
- }
335
- return [[encodeKeyFn(key), value.map(encodeFn).join(STYLE_SEPARATORS[style])]];
336
- }
337
-
338
- // Object
339
- if (style === 'deepObject') {
340
- return Object.keys(value).map(valueKey => [encodeKeyFn(`${key}[${valueKey}]`), encodeFn(value[valueKey])]);
341
- }
342
- if (explode) {
343
- return Object.keys(value).map(valueKey => [encodeKeyFn(valueKey), encodeFn(value[valueKey])]);
344
- }
345
- return [[encodeKeyFn(key), Object.keys(value).map(valueKey => [`${encodeKeyFn(valueKey)},${encodeFn(value[valueKey])}`]).join(',')]];
346
- }
347
- function buildFormData(reqForm) {
348
- /**
349
- * Build a new FormData instance, support array as field value
350
- * OAS2.0 - when collectionFormat is multi
351
- * OAS3.0 - when explode of Encoding Object is true
352
- *
353
- * This function explicitly handles Buffers (for backward compatibility)
354
- * if provided as a values to FormData. FormData can only handle USVString
355
- * or Blob.
356
- *
357
- * @param {Object} reqForm - ori req.form
358
- * @return {FormData} - new FormData instance
359
- */
360
- return Object.entries(reqForm).reduce((formData, [name, input]) => {
361
- // eslint-disable-next-line no-restricted-syntax
362
- for (const [key, value] of formatKeyValue(name, input, true)) {
363
- if (Array.isArray(value)) {
364
- // eslint-disable-next-line no-restricted-syntax
365
- for (const v of value) {
366
- if (ArrayBuffer.isView(v)) {
367
- const blob = new Blob([v]);
368
- formData.append(key, blob);
369
- } else {
370
- formData.append(key, v);
371
- }
372
- }
373
- } else if (ArrayBuffer.isView(value)) {
374
- const blob = new Blob([value]);
375
- formData.append(key, blob);
376
- } else {
377
- formData.append(key, value);
378
- }
379
- }
380
- return formData;
381
- }, new FormData());
382
- }
383
-
384
- // Encodes an object using appropriate serializer.
385
- function encodeFormOrQuery(data) {
386
- /**
387
- * Encode parameter names and values
388
- * @param {Object} result - parameter names and values
389
- * @param {string} parameterName - Parameter name
390
- * @return {object} encoded parameter names and values
391
- */
392
- const encodedQuery = Object.keys(data).reduce((result, parameterName) => {
393
- // eslint-disable-next-line no-restricted-syntax
394
- for (const [key, value] of formatKeyValue(parameterName, data[parameterName])) {
395
- if (value instanceof FileWithData) {
396
- result[key] = value.valueOf();
397
- } else {
398
- result[key] = value;
399
- }
400
- }
401
- return result;
402
- }, {});
403
- return _qs.default.stringify(encodedQuery, {
404
- encode: false,
405
- indices: false
406
- }) || '';
407
- }
408
-
409
- // If the request has a `query` object, merge it into the request.url, and delete the object
410
- // If file and/or multipart, also create FormData instance
411
- function mergeInQueryOrForm(req = {}) {
412
- const {
413
- url = '',
414
- query,
415
- form
416
- } = req;
417
- const joinSearch = (...strs) => {
418
- const search = strs.filter(a => a).join('&'); // Only truthy value
419
- return search ? `?${search}` : ''; // Only add '?' if there is a str
420
- };
421
- if (form) {
422
- const hasFile = Object.keys(form).some(key => {
423
- const {
424
- value
425
- } = form[key];
426
- return isFile(value) || isArrayOfFile(value);
427
- });
428
- const contentType = req.headers['content-type'] || req.headers['Content-Type'];
429
- if (hasFile || /multipart\/form-data/i.test(contentType)) {
430
- const formdata = buildFormData(req.form);
431
- req.formdata = formdata;
432
- req.body = formdata;
433
- } else {
434
- req.body = encodeFormOrQuery(form);
435
- }
436
- delete req.form;
437
- }
438
- if (query) {
439
- const [baseUrl, oriSearch] = url.split('?');
440
- let newStr = '';
441
- if (oriSearch) {
442
- const oriQuery = _qs.default.parse(oriSearch);
443
- const keysToRemove = Object.keys(query);
444
- keysToRemove.forEach(key => delete oriQuery[key]);
445
- newStr = _qs.default.stringify(oriQuery, {
446
- encode: true
447
- });
448
- }
449
- const finalStr = joinSearch(newStr, encodeFormOrQuery(query));
450
- req.url = baseUrl + finalStr;
451
- delete req.query;
452
- }
453
- return req;
454
- }
455
-
456
83
  // Wrap a http function ( there are otherways to do this, consider this deprecated )
457
84
  function makeHttp(httpFn, preFetch, postFetch) {
458
85
  postFetch = postFetch || (a => a);
@@ -463,7 +90,7 @@ function makeHttp(httpFn, preFetch, postFetch) {
463
90
  url: req
464
91
  };
465
92
  }
466
- self.mergeInQueryOrForm(req);
93
+ req = (0, _index.serializeRequest)(req);
467
94
  req = preFetch(req);
468
95
  return postFetch(httpFn(req));
469
96
  };
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.FileWithData = void 0;
5
+ exports.isArrayOfFile = isArrayOfFile;
6
+ exports.isFile = isFile;
7
+ function isFile(obj, navigatorObj) {
8
+ if (!navigatorObj && typeof navigator !== 'undefined') {
9
+ // eslint-disable-next-line no-undef
10
+ navigatorObj = navigator;
11
+ }
12
+ if (navigatorObj && navigatorObj.product === 'ReactNative') {
13
+ if (obj && typeof obj === 'object' && typeof obj.uri === 'string') {
14
+ return true;
15
+ }
16
+ return false;
17
+ }
18
+ if (typeof File !== 'undefined' && obj instanceof File) {
19
+ return true;
20
+ }
21
+ if (typeof Blob !== 'undefined' && obj instanceof Blob) {
22
+ return true;
23
+ }
24
+ if (ArrayBuffer.isView(obj)) {
25
+ return true;
26
+ }
27
+ return obj !== null && typeof obj === 'object' && typeof obj.pipe === 'function';
28
+ }
29
+ function isArrayOfFile(obj, navigatorObj) {
30
+ return Array.isArray(obj) && obj.some(v => isFile(v, navigatorObj));
31
+ }
32
+
33
+ /**
34
+ * Specialized sub-class of File class, that only
35
+ * accepts string data and retain this data in `data`
36
+ * public property throughout the lifecycle of its instances.
37
+ *
38
+ * This sub-class is exclusively used only when Encoding Object
39
+ * is defined within the Media Type Object (OpenAPI 3.x.y).
40
+ */
41
+ class FileWithData extends File {
42
+ constructor(data, name = '', options = {}) {
43
+ super([data], name, options);
44
+ this.data = data;
45
+ }
46
+ valueOf() {
47
+ return this.data;
48
+ }
49
+ toString() {
50
+ return this.valueOf();
51
+ }
52
+ }
53
+ exports.FileWithData = FileWithData;
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = formatKeyValue;
5
+ var _file = require("./file.js");
6
+ var _styleSerializer = require("../../../execute/oas3/style-serializer.js");
7
+ const STYLE_SEPARATORS = {
8
+ form: ',',
9
+ spaceDelimited: '%20',
10
+ pipeDelimited: '|'
11
+ };
12
+ const SEPARATORS = {
13
+ csv: ',',
14
+ ssv: '%20',
15
+ tsv: '%09',
16
+ pipes: '|'
17
+ };
18
+
19
+ // Formats a key-value and returns an array of key-value pairs.
20
+ //
21
+ // Return value example 1: [['color', 'blue']]
22
+ // Return value example 2: [['color', 'blue,black,brown']]
23
+ // Return value example 3: [['color', ['blue', 'black', 'brown']]]
24
+ // Return value example 4: [['color', 'R,100,G,200,B,150']]
25
+ // Return value example 5: [['R', '100'], ['G', '200'], ['B', '150']]
26
+ // Return value example 6: [['color[R]', '100'], ['color[G]', '200'], ['color[B]', '150']]
27
+ function formatKeyValue(key, input, skipEncoding = false) {
28
+ const {
29
+ collectionFormat,
30
+ allowEmptyValue,
31
+ serializationOption,
32
+ encoding
33
+ } = input;
34
+ // `input` can be string
35
+ const value = typeof input === 'object' && !Array.isArray(input) ? input.value : input;
36
+ const encodeFn = skipEncoding ? k => k.toString() : k => encodeURIComponent(k);
37
+ const encodedKey = encodeFn(key);
38
+ if (typeof value === 'undefined' && allowEmptyValue) {
39
+ return [[encodedKey, '']];
40
+ }
41
+
42
+ // file
43
+ if ((0, _file.isFile)(value) || (0, _file.isArrayOfFile)(value)) {
44
+ return [[encodedKey, value]];
45
+ }
46
+
47
+ // for OAS 3 Parameter Object for serialization
48
+ if (serializationOption) {
49
+ return formatKeyValueBySerializationOption(key, value, skipEncoding, serializationOption);
50
+ }
51
+
52
+ // for OAS 3 Encoding Object
53
+ if (encoding) {
54
+ if ([typeof encoding.style, typeof encoding.explode, typeof encoding.allowReserved].some(type => type !== 'undefined')) {
55
+ const {
56
+ style,
57
+ explode,
58
+ allowReserved
59
+ } = encoding;
60
+ return formatKeyValueBySerializationOption(key, value, skipEncoding, {
61
+ style,
62
+ explode,
63
+ allowReserved
64
+ });
65
+ }
66
+ if (typeof encoding.contentType === 'string') {
67
+ if (encoding.contentType.startsWith('application/json')) {
68
+ // if value is a string, assume value is already a JSON string
69
+ const json = typeof value === 'string' ? value : JSON.stringify(value);
70
+ const encodedJson = encodeFn(json);
71
+ const file = new _file.FileWithData(encodedJson, 'blob', {
72
+ type: encoding.contentType
73
+ });
74
+ return [[encodedKey, file]];
75
+ }
76
+ const encodedData = encodeFn(String(value));
77
+ const blob = new _file.FileWithData(encodedData, 'blob', {
78
+ type: encoding.contentType
79
+ });
80
+ return [[encodedKey, blob]];
81
+ }
82
+
83
+ // Primitive
84
+ if (typeof value !== 'object') {
85
+ return [[encodedKey, encodeFn(value)]];
86
+ }
87
+
88
+ // Array of primitives
89
+ if (Array.isArray(value) && value.every(v => typeof v !== 'object')) {
90
+ return [[encodedKey, value.map(encodeFn).join(',')]];
91
+ }
92
+
93
+ // Array or object
94
+ return [[encodedKey, encodeFn(JSON.stringify(value))]];
95
+ }
96
+
97
+ // for OAS 2 Parameter Object
98
+ // Primitive
99
+ if (typeof value !== 'object') {
100
+ return [[encodedKey, encodeFn(value)]];
101
+ }
102
+
103
+ // Array
104
+ if (Array.isArray(value)) {
105
+ if (collectionFormat === 'multi') {
106
+ // In case of multipart/formdata, it is used as array.
107
+ // Otherwise, the caller will convert it to a query by qs.stringify.
108
+ return [[encodedKey, value.map(encodeFn)]];
109
+ }
110
+ return [[encodedKey, value.map(encodeFn).join(SEPARATORS[collectionFormat || 'csv'])]];
111
+ }
112
+
113
+ // Object
114
+ return [[encodedKey, '']];
115
+ }
116
+ function formatKeyValueBySerializationOption(key, value, skipEncoding, serializationOption) {
117
+ const style = serializationOption.style || 'form';
118
+ const explode = typeof serializationOption.explode === 'undefined' ? style === 'form' : serializationOption.explode;
119
+ // eslint-disable-next-line no-nested-ternary
120
+ const escape = skipEncoding ? false : serializationOption && serializationOption.allowReserved ? 'unsafe' : 'reserved';
121
+ const encodeFn = v => (0, _styleSerializer.valueEncoder)(v, escape);
122
+ const encodeKeyFn = skipEncoding ? k => k : k => encodeFn(k);
123
+
124
+ // Primitive
125
+ if (typeof value !== 'object') {
126
+ return [[encodeKeyFn(key), encodeFn(value)]];
127
+ }
128
+
129
+ // Array
130
+ if (Array.isArray(value)) {
131
+ if (explode) {
132
+ // In case of multipart/formdata, it is used as array.
133
+ // Otherwise, the caller will convert it to a query by qs.stringify.
134
+ return [[encodeKeyFn(key), value.map(encodeFn)]];
135
+ }
136
+ return [[encodeKeyFn(key), value.map(encodeFn).join(STYLE_SEPARATORS[style])]];
137
+ }
138
+
139
+ // Object
140
+ if (style === 'deepObject') {
141
+ return Object.keys(value).map(valueKey => [encodeKeyFn(`${key}[${valueKey}]`), encodeFn(value[valueKey])]);
142
+ }
143
+ if (explode) {
144
+ return Object.keys(value).map(valueKey => [encodeKeyFn(valueKey), encodeFn(value[valueKey])]);
145
+ }
146
+ return [[encodeKeyFn(key), Object.keys(value).map(valueKey => [`${encodeKeyFn(valueKey)},${encodeFn(value[valueKey])}`]).join(',')]];
147
+ }