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.
Files changed (57) hide show
  1. package/README.md +10 -2
  2. package/dist/swagger-client.browser.js +24191 -0
  3. package/dist/swagger-client.browser.min.js +3 -0
  4. package/dist/swagger-client.browser.min.js.map +1 -0
  5. package/es/commonjs.js +9 -0
  6. package/es/constants.js +2 -0
  7. package/es/execute/index.js +391 -0
  8. package/es/execute/oas3/build-request.js +149 -0
  9. package/es/execute/oas3/content-serializer.js +18 -0
  10. package/es/execute/oas3/parameter-builders.js +119 -0
  11. package/es/execute/oas3/style-serializer.js +232 -0
  12. package/es/execute/swagger2/build-request.js +119 -0
  13. package/es/execute/swagger2/parameter-builders.js +78 -0
  14. package/es/helpers.js +272 -0
  15. package/es/http.js +621 -0
  16. package/es/index.js +116 -0
  17. package/es/interfaces.js +145 -0
  18. package/es/internal/form-data-monkey-patch.js +94 -0
  19. package/es/resolver.js +123 -0
  20. package/es/specmap/helpers.js +62 -0
  21. package/es/specmap/index.js +613 -0
  22. package/es/specmap/lib/all-of.js +81 -0
  23. package/es/specmap/lib/context-tree.js +111 -0
  24. package/es/specmap/lib/create-error.js +24 -0
  25. package/es/specmap/lib/index.js +391 -0
  26. package/es/specmap/lib/parameters.js +31 -0
  27. package/es/specmap/lib/properties.js +23 -0
  28. package/es/specmap/lib/refs.js +516 -0
  29. package/es/subtree-resolver/index.js +92 -0
  30. package/lib/commonjs.js +10 -0
  31. package/lib/constants.js +7 -0
  32. package/lib/execute/index.js +421 -0
  33. package/lib/execute/oas3/build-request.js +161 -0
  34. package/lib/execute/oas3/content-serializer.js +21 -0
  35. package/lib/execute/oas3/parameter-builders.js +138 -0
  36. package/lib/execute/oas3/style-serializer.js +208 -0
  37. package/lib/execute/swagger2/build-request.js +120 -0
  38. package/lib/execute/swagger2/parameter-builders.js +88 -0
  39. package/lib/helpers.js +261 -0
  40. package/lib/http.js +470 -0
  41. package/lib/index.js +142 -0
  42. package/lib/interfaces.js +159 -0
  43. package/lib/internal/form-data-monkey-patch.js +83 -0
  44. package/lib/resolver.js +125 -0
  45. package/lib/specmap/helpers.js +65 -0
  46. package/lib/specmap/index.js +446 -0
  47. package/lib/specmap/lib/all-of.js +89 -0
  48. package/lib/specmap/lib/context-tree.js +111 -0
  49. package/lib/specmap/lib/create-error.js +25 -0
  50. package/lib/specmap/lib/index.js +402 -0
  51. package/lib/specmap/lib/parameters.js +42 -0
  52. package/lib/specmap/lib/properties.js +38 -0
  53. package/lib/specmap/lib/refs.js +509 -0
  54. package/lib/subtree-resolver/index.js +55 -0
  55. package/package.json +80 -106
  56. package/browser/index.js +0 -54
  57. package/dist/index.js +0 -4372
@@ -0,0 +1,613 @@
1
+ import _objectSpread from "@babel/runtime-corejs2/helpers/objectSpread2";
2
+ import _typeof from "@babel/runtime-corejs2/helpers/typeof";
3
+ import _Promise from "@babel/runtime-corejs2/core-js/promise";
4
+ import _regeneratorRuntime from "@babel/runtime-corejs2/regenerator";
5
+ import _Object$keys2 from "@babel/runtime-corejs2/core-js/object/keys";
6
+ import _createForOfIteratorHelper from "@babel/runtime-corejs2/helpers/createForOfIteratorHelper";
7
+ import _Array$isArray from "@babel/runtime-corejs2/core-js/array/is-array";
8
+ import _Object$create from "@babel/runtime-corejs2/core-js/object/create";
9
+ import _Object$assign from "@babel/runtime-corejs2/core-js/object/assign";
10
+ import _classCallCheck from "@babel/runtime-corejs2/helpers/classCallCheck";
11
+ import _createClass from "@babel/runtime-corejs2/helpers/createClass";
12
+ import find from 'lodash/find';
13
+ import noop from 'lodash/noop';
14
+ import lib from './lib';
15
+ import refs from './lib/refs';
16
+ import allOf from './lib/all-of';
17
+ import parameters from './lib/parameters';
18
+ import properties from './lib/properties';
19
+ import ContextTree from './lib/context-tree';
20
+ var HARD_LIMIT = 100;
21
+
22
+ var SpecMap = /*#__PURE__*/function () {
23
+ _createClass(SpecMap, null, [{
24
+ key: "getPluginName",
25
+ value: function getPluginName(plugin) {
26
+ return plugin.pluginName;
27
+ }
28
+ }, {
29
+ key: "getPatchesOfType",
30
+ value: function getPatchesOfType(patches, fn) {
31
+ return patches.filter(fn);
32
+ }
33
+ }]);
34
+
35
+ function SpecMap(opts) {
36
+ var _this = this;
37
+
38
+ _classCallCheck(this, SpecMap);
39
+
40
+ _Object$assign(this, {
41
+ spec: '',
42
+ debugLevel: 'info',
43
+ plugins: [],
44
+ pluginHistory: {},
45
+ errors: [],
46
+ mutations: [],
47
+ promisedPatches: [],
48
+ state: {},
49
+ patches: [],
50
+ context: {},
51
+ contextTree: new ContextTree(),
52
+ showDebug: false,
53
+ allPatches: [],
54
+ // only populated if showDebug is true
55
+ pluginProp: 'specMap',
56
+ libMethods: _Object$assign(_Object$create(this), lib, {
57
+ getInstance: function getInstance() {
58
+ return _this;
59
+ }
60
+ }),
61
+ allowMetaPatches: false
62
+ }, opts); // Lib methods bound
63
+
64
+
65
+ this.get = this._get.bind(this); // eslint-disable-line no-underscore-dangle
66
+
67
+ this.getContext = this._getContext.bind(this); // eslint-disable-line no-underscore-dangle
68
+
69
+ this.hasRun = this._hasRun.bind(this); // eslint-disable-line no-underscore-dangle
70
+
71
+ this.wrappedPlugins = this.plugins.map(this.wrapPlugin.bind(this)).filter(lib.isFunction); // Initial patch(s)
72
+
73
+ this.patches.push(lib.add([], this.spec));
74
+ this.patches.push(lib.context([], this.context));
75
+ this.updatePatches(this.patches);
76
+ }
77
+
78
+ _createClass(SpecMap, [{
79
+ key: "debug",
80
+ value: function debug(level) {
81
+ if (this.debugLevel === level) {
82
+ var _console;
83
+
84
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
85
+ args[_key - 1] = arguments[_key];
86
+ }
87
+
88
+ (_console = console).log.apply(_console, args); // eslint-disable-line no-console
89
+
90
+ }
91
+ }
92
+ }, {
93
+ key: "verbose",
94
+ value: function verbose(header) {
95
+ if (this.debugLevel === 'verbose') {
96
+ var _console2;
97
+
98
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
99
+ args[_key2 - 1] = arguments[_key2];
100
+ }
101
+
102
+ (_console2 = console).log.apply(_console2, ["[".concat(header, "] ")].concat(args)); // eslint-disable-line no-console
103
+
104
+ }
105
+ }
106
+ }, {
107
+ key: "wrapPlugin",
108
+ value: function wrapPlugin(plugin, name) {
109
+ var pathDiscriminator = this.pathDiscriminator;
110
+ var ctx = null;
111
+ var fn;
112
+
113
+ if (plugin[this.pluginProp]) {
114
+ ctx = plugin;
115
+ fn = plugin[this.pluginProp];
116
+ } else if (lib.isFunction(plugin)) {
117
+ fn = plugin;
118
+ } else if (lib.isObject(plugin)) {
119
+ fn = createKeyBasedPlugin(plugin);
120
+ }
121
+
122
+ return _Object$assign(fn.bind(ctx), {
123
+ pluginName: plugin.name || name,
124
+ isGenerator: lib.isGenerator(fn)
125
+ }); // Expected plugin interface: {key: string, plugin: fn*}
126
+ // This traverses depth-first and immediately applies yielded patches.
127
+ // This strategy should work well for most plugins (including the built-ins).
128
+ // We might consider making this (traversing & application) configurable later.
129
+
130
+ function createKeyBasedPlugin(pluginObj) {
131
+ var isSubPath = function isSubPath(path, tested) {
132
+ if (!_Array$isArray(path)) {
133
+ return true;
134
+ }
135
+
136
+ return path.every(function (val, i) {
137
+ return val === tested[i];
138
+ });
139
+ };
140
+
141
+ return /*#__PURE__*/_regeneratorRuntime.mark(function generator(patches, specmap) {
142
+ var _marked, refCache, _iterator, _step, patch, traverse;
143
+
144
+ return _regeneratorRuntime.wrap(function generator$(_context2) {
145
+ while (1) {
146
+ switch (_context2.prev = _context2.next) {
147
+ case 0:
148
+ traverse = function _traverse(obj, path, patch) {
149
+ var parentIndex, parent, indexOfFirstProperties, isRootProperties, traversed, _i, _Object$keys, key, val, updatedPath, isObj, objRef, isWithinPathDiscriminator;
150
+
151
+ return _regeneratorRuntime.wrap(function traverse$(_context) {
152
+ while (1) {
153
+ switch (_context.prev = _context.next) {
154
+ case 0:
155
+ if (lib.isObject(obj)) {
156
+ _context.next = 6;
157
+ break;
158
+ }
159
+
160
+ if (!(pluginObj.key === path[path.length - 1])) {
161
+ _context.next = 4;
162
+ break;
163
+ }
164
+
165
+ _context.next = 4;
166
+ return pluginObj.plugin(obj, pluginObj.key, path, specmap);
167
+
168
+ case 4:
169
+ _context.next = 30;
170
+ break;
171
+
172
+ case 6:
173
+ parentIndex = path.length - 1;
174
+ parent = path[parentIndex];
175
+ indexOfFirstProperties = path.indexOf('properties');
176
+ isRootProperties = parent === 'properties' && parentIndex === indexOfFirstProperties;
177
+ traversed = specmap.allowMetaPatches && refCache[obj.$$ref]; // eslint-disable-next-line no-restricted-syntax
178
+
179
+ _i = 0, _Object$keys = _Object$keys2(obj);
180
+
181
+ case 12:
182
+ if (!(_i < _Object$keys.length)) {
183
+ _context.next = 30;
184
+ break;
185
+ }
186
+
187
+ key = _Object$keys[_i];
188
+ val = obj[key];
189
+ updatedPath = path.concat(key);
190
+ isObj = lib.isObject(val);
191
+ objRef = obj.$$ref;
192
+
193
+ if (traversed) {
194
+ _context.next = 22;
195
+ break;
196
+ }
197
+
198
+ if (!isObj) {
199
+ _context.next = 22;
200
+ break;
201
+ }
202
+
203
+ // Only store the ref if it exists
204
+ if (specmap.allowMetaPatches && objRef) {
205
+ refCache[objRef] = true;
206
+ }
207
+
208
+ return _context.delegateYield(traverse(val, updatedPath, patch), "t0", 22);
209
+
210
+ case 22:
211
+ if (!(!isRootProperties && key === pluginObj.key)) {
212
+ _context.next = 27;
213
+ break;
214
+ }
215
+
216
+ isWithinPathDiscriminator = isSubPath(pathDiscriminator, path);
217
+
218
+ if (!(!pathDiscriminator || isWithinPathDiscriminator)) {
219
+ _context.next = 27;
220
+ break;
221
+ }
222
+
223
+ _context.next = 27;
224
+ return pluginObj.plugin(val, key, updatedPath, specmap, patch);
225
+
226
+ case 27:
227
+ _i++;
228
+ _context.next = 12;
229
+ break;
230
+
231
+ case 30:
232
+ case "end":
233
+ return _context.stop();
234
+ }
235
+ }
236
+ }, _marked);
237
+ };
238
+
239
+ _marked = /*#__PURE__*/_regeneratorRuntime.mark(traverse);
240
+ refCache = {}; // eslint-disable-next-line no-restricted-syntax
241
+
242
+ _iterator = _createForOfIteratorHelper(patches.filter(lib.isAdditiveMutation));
243
+ _context2.prev = 4;
244
+
245
+ _iterator.s();
246
+
247
+ case 6:
248
+ if ((_step = _iterator.n()).done) {
249
+ _context2.next = 11;
250
+ break;
251
+ }
252
+
253
+ patch = _step.value;
254
+ return _context2.delegateYield(traverse(patch.value, patch.path, patch), "t0", 9);
255
+
256
+ case 9:
257
+ _context2.next = 6;
258
+ break;
259
+
260
+ case 11:
261
+ _context2.next = 16;
262
+ break;
263
+
264
+ case 13:
265
+ _context2.prev = 13;
266
+ _context2.t1 = _context2["catch"](4);
267
+
268
+ _iterator.e(_context2.t1);
269
+
270
+ case 16:
271
+ _context2.prev = 16;
272
+
273
+ _iterator.f();
274
+
275
+ return _context2.finish(16);
276
+
277
+ case 19:
278
+ case "end":
279
+ return _context2.stop();
280
+ }
281
+ }
282
+ }, generator, null, [[4, 13, 16, 19]]);
283
+ });
284
+ }
285
+ }
286
+ }, {
287
+ key: "nextPlugin",
288
+ value: function nextPlugin() {
289
+ var _this2 = this;
290
+
291
+ // Array.prototype.find doesn't work in IE 11 :(
292
+ return find(this.wrappedPlugins, function (plugin) {
293
+ var mutations = _this2.getMutationsForPlugin(plugin);
294
+
295
+ return mutations.length > 0;
296
+ });
297
+ }
298
+ }, {
299
+ key: "nextPromisedPatch",
300
+ value: function nextPromisedPatch() {
301
+ if (this.promisedPatches.length > 0) {
302
+ return _Promise.race(this.promisedPatches.map(function (patch) {
303
+ return patch.value;
304
+ }));
305
+ }
306
+
307
+ return undefined;
308
+ }
309
+ }, {
310
+ key: "getPluginHistory",
311
+ value: function getPluginHistory(plugin) {
312
+ var name = this.constructor.getPluginName(plugin);
313
+ return this.pluginHistory[name] || [];
314
+ }
315
+ }, {
316
+ key: "getPluginRunCount",
317
+ value: function getPluginRunCount(plugin) {
318
+ return this.getPluginHistory(plugin).length;
319
+ }
320
+ }, {
321
+ key: "getPluginHistoryTip",
322
+ value: function getPluginHistoryTip(plugin) {
323
+ var history = this.getPluginHistory(plugin);
324
+ var val = history && history[history.length - 1];
325
+ return val || {};
326
+ }
327
+ }, {
328
+ key: "getPluginMutationIndex",
329
+ value: function getPluginMutationIndex(plugin) {
330
+ var mi = this.getPluginHistoryTip(plugin).mutationIndex;
331
+ return typeof mi !== 'number' ? -1 : mi;
332
+ }
333
+ }, {
334
+ key: "updatePluginHistory",
335
+ value: function updatePluginHistory(plugin, val) {
336
+ var name = this.constructor.getPluginName(plugin);
337
+ this.pluginHistory[name] = this.pluginHistory[name] || [];
338
+ this.pluginHistory[name].push(val);
339
+ }
340
+ }, {
341
+ key: "updatePatches",
342
+ value: function updatePatches(patches) {
343
+ var _this3 = this;
344
+
345
+ lib.normalizeArray(patches).forEach(function (patch) {
346
+ if (patch instanceof Error) {
347
+ _this3.errors.push(patch);
348
+
349
+ return;
350
+ }
351
+
352
+ try {
353
+ if (!lib.isObject(patch)) {
354
+ _this3.debug('updatePatches', 'Got a non-object patch', patch);
355
+
356
+ return;
357
+ }
358
+
359
+ if (_this3.showDebug) {
360
+ _this3.allPatches.push(patch);
361
+ }
362
+
363
+ if (lib.isPromise(patch.value)) {
364
+ _this3.promisedPatches.push(patch);
365
+
366
+ _this3.promisedPatchThen(patch);
367
+
368
+ return;
369
+ }
370
+
371
+ if (lib.isContextPatch(patch)) {
372
+ _this3.setContext(patch.path, patch.value);
373
+
374
+ return;
375
+ }
376
+
377
+ if (lib.isMutation(patch)) {
378
+ _this3.updateMutations(patch);
379
+
380
+ return;
381
+ }
382
+ } catch (e) {
383
+ console.error(e); // eslint-disable-line no-console
384
+
385
+ _this3.errors.push(e);
386
+ }
387
+ });
388
+ }
389
+ }, {
390
+ key: "updateMutations",
391
+ value: function updateMutations(patch) {
392
+ if (_typeof(patch.value) === 'object' && !_Array$isArray(patch.value) && this.allowMetaPatches) {
393
+ patch.value = _objectSpread({}, patch.value);
394
+ }
395
+
396
+ var result = lib.applyPatch(this.state, patch, {
397
+ allowMetaPatches: this.allowMetaPatches
398
+ });
399
+
400
+ if (result) {
401
+ this.mutations.push(patch);
402
+ this.state = result;
403
+ }
404
+ }
405
+ }, {
406
+ key: "removePromisedPatch",
407
+ value: function removePromisedPatch(patch) {
408
+ var index = this.promisedPatches.indexOf(patch);
409
+
410
+ if (index < 0) {
411
+ this.debug("Tried to remove a promisedPatch that isn't there!");
412
+ return;
413
+ }
414
+
415
+ this.promisedPatches.splice(index, 1);
416
+ }
417
+ }, {
418
+ key: "promisedPatchThen",
419
+ value: function promisedPatchThen(patch) {
420
+ var _this4 = this;
421
+
422
+ patch.value = patch.value.then(function (val) {
423
+ var promisedPatch = _objectSpread(_objectSpread({}, patch), {}, {
424
+ value: val
425
+ });
426
+
427
+ _this4.removePromisedPatch(patch);
428
+
429
+ _this4.updatePatches(promisedPatch);
430
+ }).catch(function (e) {
431
+ _this4.removePromisedPatch(patch);
432
+
433
+ _this4.updatePatches(e);
434
+ });
435
+ return patch.value;
436
+ }
437
+ }, {
438
+ key: "getMutations",
439
+ value: function getMutations(from, to) {
440
+ from = from || 0;
441
+
442
+ if (typeof to !== 'number') {
443
+ to = this.mutations.length;
444
+ }
445
+
446
+ return this.mutations.slice(from, to);
447
+ }
448
+ }, {
449
+ key: "getCurrentMutations",
450
+ value: function getCurrentMutations() {
451
+ return this.getMutationsForPlugin(this.getCurrentPlugin());
452
+ }
453
+ }, {
454
+ key: "getMutationsForPlugin",
455
+ value: function getMutationsForPlugin(plugin) {
456
+ var tip = this.getPluginMutationIndex(plugin);
457
+ return this.getMutations(tip + 1);
458
+ }
459
+ }, {
460
+ key: "getCurrentPlugin",
461
+ value: function getCurrentPlugin() {
462
+ return this.currentPlugin;
463
+ }
464
+ }, {
465
+ key: "getLib",
466
+ value: function getLib() {
467
+ return this.libMethods;
468
+ } // eslint-disable-next-line no-underscore-dangle
469
+
470
+ }, {
471
+ key: "_get",
472
+ value: function _get(path) {
473
+ return lib.getIn(this.state, path);
474
+ } // eslint-disable-next-line no-underscore-dangle
475
+
476
+ }, {
477
+ key: "_getContext",
478
+ value: function _getContext(path) {
479
+ return this.contextTree.get(path);
480
+ }
481
+ }, {
482
+ key: "setContext",
483
+ value: function setContext(path, value) {
484
+ return this.contextTree.set(path, value);
485
+ } // eslint-disable-next-line no-underscore-dangle
486
+
487
+ }, {
488
+ key: "_hasRun",
489
+ value: function _hasRun(count) {
490
+ var times = this.getPluginRunCount(this.getCurrentPlugin());
491
+ return times > (count || 0);
492
+ }
493
+ }, {
494
+ key: "dispatch",
495
+ value: function dispatch() {
496
+ var _this5 = this;
497
+
498
+ var that = this;
499
+ var plugin = this.nextPlugin();
500
+
501
+ if (!plugin) {
502
+ var nextPromise = this.nextPromisedPatch();
503
+
504
+ if (nextPromise) {
505
+ return nextPromise.then(function () {
506
+ return _this5.dispatch();
507
+ }).catch(function () {
508
+ return _this5.dispatch();
509
+ });
510
+ } // We're done!
511
+
512
+
513
+ var result = {
514
+ spec: this.state,
515
+ errors: this.errors
516
+ };
517
+
518
+ if (this.showDebug) {
519
+ result.patches = this.allPatches;
520
+ }
521
+
522
+ return _Promise.resolve(result);
523
+ } // Makes sure plugin isn't running an endless loop
524
+
525
+
526
+ that.pluginCount = that.pluginCount || {};
527
+ that.pluginCount[plugin] = (that.pluginCount[plugin] || 0) + 1;
528
+
529
+ if (that.pluginCount[plugin] > HARD_LIMIT) {
530
+ return _Promise.resolve({
531
+ spec: that.state,
532
+ errors: that.errors.concat(new Error("We've reached a hard limit of ".concat(HARD_LIMIT, " plugin runs")))
533
+ });
534
+ } // A different plugin runs, wait for all promises to resolve, then retry
535
+
536
+
537
+ if (plugin !== this.currentPlugin && this.promisedPatches.length) {
538
+ var promises = this.promisedPatches.map(function (p) {
539
+ return p.value;
540
+ }); // Waits for all to settle instead of Promise.all which stops on rejection
541
+
542
+ return _Promise.all(promises.map(function (promise) {
543
+ return promise.then(noop, noop);
544
+ })).then(function () {
545
+ return _this5.dispatch();
546
+ });
547
+ } // Ok, run the plugin
548
+
549
+
550
+ return executePlugin();
551
+
552
+ function executePlugin() {
553
+ that.currentPlugin = plugin;
554
+ var mutations = that.getCurrentMutations();
555
+ var lastMutationIndex = that.mutations.length - 1;
556
+
557
+ try {
558
+ if (plugin.isGenerator) {
559
+ // eslint-disable-next-line no-restricted-syntax
560
+ var _iterator2 = _createForOfIteratorHelper(plugin(mutations, that.getLib())),
561
+ _step2;
562
+
563
+ try {
564
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
565
+ var yieldedPatches = _step2.value;
566
+ updatePatches(yieldedPatches);
567
+ }
568
+ } catch (err) {
569
+ _iterator2.e(err);
570
+ } finally {
571
+ _iterator2.f();
572
+ }
573
+ } else {
574
+ var newPatches = plugin(mutations, that.getLib());
575
+ updatePatches(newPatches);
576
+ }
577
+ } catch (e) {
578
+ console.error(e); // eslint-disable-line no-console
579
+
580
+ updatePatches([_Object$assign(_Object$create(e), {
581
+ plugin: plugin
582
+ })]);
583
+ } finally {
584
+ that.updatePluginHistory(plugin, {
585
+ mutationIndex: lastMutationIndex
586
+ });
587
+ }
588
+
589
+ return that.dispatch();
590
+ }
591
+
592
+ function updatePatches(patches) {
593
+ if (patches) {
594
+ patches = lib.fullyNormalizeArray(patches);
595
+ that.updatePatches(patches, plugin);
596
+ }
597
+ }
598
+ }
599
+ }]);
600
+
601
+ return SpecMap;
602
+ }();
603
+
604
+ export default function mapSpec(opts) {
605
+ return new SpecMap(opts).dispatch();
606
+ }
607
+ var plugins = {
608
+ refs: refs,
609
+ allOf: allOf,
610
+ parameters: parameters,
611
+ properties: properties
612
+ };
613
+ export { SpecMap, plugins };
@@ -0,0 +1,81 @@
1
+ import _toConsumableArray from "@babel/runtime-corejs2/helpers/toConsumableArray";
2
+ import _objectSpread from "@babel/runtime-corejs2/helpers/objectSpread2";
3
+ import _Array$isArray from "@babel/runtime-corejs2/core-js/array/is-array";
4
+ import { isFreelyNamed, generateAbsoluteRefPatches } from '../helpers';
5
+ export default {
6
+ key: 'allOf',
7
+ plugin: function plugin(val, key, fullPath, specmap, patch) {
8
+ // Ignore replace patches created by $ref because the changes will
9
+ // occur in the original "add" patch and we don't want this plugin
10
+ // to redundantly processes those "relace" patches.
11
+ if (patch.meta && patch.meta.$$ref) {
12
+ return undefined;
13
+ }
14
+
15
+ var parent = fullPath.slice(0, -1);
16
+
17
+ if (isFreelyNamed(parent)) {
18
+ return undefined;
19
+ }
20
+
21
+ if (!_Array$isArray(val)) {
22
+ var err = new TypeError('allOf must be an array');
23
+ err.fullPath = fullPath; // This is an array
24
+
25
+ return err;
26
+ }
27
+
28
+ var alreadyAddError = false; // Find the original definition from the `patch.value` object
29
+ // Remove the `allOf` property so it doesn't get added to the result of the `allOf` plugin
30
+
31
+ var originalDefinitionObj = patch.value;
32
+ parent.forEach(function (part) {
33
+ if (!originalDefinitionObj) return; // bail out if we've lost sight of our target
34
+
35
+ originalDefinitionObj = originalDefinitionObj[part];
36
+ });
37
+ originalDefinitionObj = _objectSpread({}, originalDefinitionObj);
38
+ delete originalDefinitionObj.allOf;
39
+ var patches = []; // remove existing content
40
+
41
+ patches.push(specmap.replace(parent, {}));
42
+ val.forEach(function (toMerge, i) {
43
+ if (!specmap.isObject(toMerge)) {
44
+ if (alreadyAddError) {
45
+ return null;
46
+ }
47
+
48
+ alreadyAddError = true;
49
+
50
+ var _err = new TypeError('Elements in allOf must be objects');
51
+
52
+ _err.fullPath = fullPath; // This is an array
53
+
54
+ return patches.push(_err);
55
+ } // Deeply merge the member's contents onto the parent location
56
+
57
+
58
+ patches.push(specmap.mergeDeep(parent, toMerge)); // Generate patches that migrate $ref values based on ContextTree information
59
+ // remove ["allOf"], which will not be present when these patches are applied
60
+
61
+ var collapsedFullPath = fullPath.slice(0, -1);
62
+ var absoluteRefPatches = generateAbsoluteRefPatches(toMerge, collapsedFullPath, {
63
+ getBaseUrlForNodePath: function getBaseUrlForNodePath(nodePath) {
64
+ return specmap.getContext([].concat(_toConsumableArray(fullPath), [i], _toConsumableArray(nodePath))).baseDoc;
65
+ },
66
+ specmap: specmap
67
+ });
68
+ patches.push.apply(patches, _toConsumableArray(absoluteRefPatches));
69
+ return undefined;
70
+ }); // Merge back the values from the original definition
71
+
72
+ patches.push(specmap.mergeDeep(parent, originalDefinitionObj)); // If there was not an original $$ref value, make sure to remove
73
+ // any $$ref value that may exist from the result of `allOf` merges
74
+
75
+ if (!originalDefinitionObj.$$ref) {
76
+ patches.push(specmap.remove([].concat(parent, '$$ref')));
77
+ }
78
+
79
+ return patches;
80
+ }
81
+ };