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,446 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = mapSpec;
5
+ exports.plugins = exports.SpecMap = void 0;
6
+
7
+ var _find = _interopRequireDefault(require("lodash/find"));
8
+
9
+ var _noop = _interopRequireDefault(require("lodash/noop"));
10
+
11
+ var _lib = _interopRequireDefault(require("./lib"));
12
+
13
+ var _refs = _interopRequireDefault(require("./lib/refs"));
14
+
15
+ var _allOf = _interopRequireDefault(require("./lib/all-of"));
16
+
17
+ var _parameters = _interopRequireDefault(require("./lib/parameters"));
18
+
19
+ var _properties = _interopRequireDefault(require("./lib/properties"));
20
+
21
+ var _contextTree = _interopRequireDefault(require("./lib/context-tree"));
22
+
23
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
+
25
+ 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; }
26
+
27
+ 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; }
28
+
29
+ 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; }
30
+
31
+ const HARD_LIMIT = 100;
32
+
33
+ class SpecMap {
34
+ static getPluginName(plugin) {
35
+ return plugin.pluginName;
36
+ }
37
+
38
+ static getPatchesOfType(patches, fn) {
39
+ return patches.filter(fn);
40
+ }
41
+
42
+ constructor(opts) {
43
+ Object.assign(this, {
44
+ spec: '',
45
+ debugLevel: 'info',
46
+ plugins: [],
47
+ pluginHistory: {},
48
+ errors: [],
49
+ mutations: [],
50
+ promisedPatches: [],
51
+ state: {},
52
+ patches: [],
53
+ context: {},
54
+ contextTree: new _contextTree.default(),
55
+ showDebug: false,
56
+ allPatches: [],
57
+ // only populated if showDebug is true
58
+ pluginProp: 'specMap',
59
+ libMethods: Object.assign(Object.create(this), _lib.default, {
60
+ getInstance: () => this
61
+ }),
62
+ allowMetaPatches: false
63
+ }, opts); // Lib methods bound
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.default.isFunction); // Initial patch(s)
72
+
73
+ this.patches.push(_lib.default.add([], this.spec));
74
+ this.patches.push(_lib.default.context([], this.context));
75
+ this.updatePatches(this.patches);
76
+ }
77
+
78
+ debug(level, ...args) {
79
+ if (this.debugLevel === level) {
80
+ console.log(...args); // eslint-disable-line no-console
81
+ }
82
+ }
83
+
84
+ verbose(header, ...args) {
85
+ if (this.debugLevel === 'verbose') {
86
+ console.log(`[${header}] `, ...args); // eslint-disable-line no-console
87
+ }
88
+ }
89
+
90
+ wrapPlugin(plugin, name) {
91
+ const {
92
+ pathDiscriminator
93
+ } = this;
94
+ let ctx = null;
95
+ let fn;
96
+
97
+ if (plugin[this.pluginProp]) {
98
+ ctx = plugin;
99
+ fn = plugin[this.pluginProp];
100
+ } else if (_lib.default.isFunction(plugin)) {
101
+ fn = plugin;
102
+ } else if (_lib.default.isObject(plugin)) {
103
+ fn = createKeyBasedPlugin(plugin);
104
+ }
105
+
106
+ return Object.assign(fn.bind(ctx), {
107
+ pluginName: plugin.name || name,
108
+ isGenerator: _lib.default.isGenerator(fn)
109
+ }); // Expected plugin interface: {key: string, plugin: fn*}
110
+ // This traverses depth-first and immediately applies yielded patches.
111
+ // This strategy should work well for most plugins (including the built-ins).
112
+ // We might consider making this (traversing & application) configurable later.
113
+
114
+ function createKeyBasedPlugin(pluginObj) {
115
+ const isSubPath = (path, tested) => {
116
+ if (!Array.isArray(path)) {
117
+ return true;
118
+ }
119
+
120
+ return path.every((val, i) => {
121
+ return val === tested[i];
122
+ });
123
+ };
124
+
125
+ return function* generator(patches, specmap) {
126
+ const refCache = {}; // eslint-disable-next-line no-restricted-syntax
127
+
128
+ for (const patch of patches.filter(_lib.default.isAdditiveMutation)) {
129
+ yield* traverse(patch.value, patch.path, patch);
130
+ }
131
+
132
+ function* traverse(obj, path, patch) {
133
+ if (!_lib.default.isObject(obj)) {
134
+ if (pluginObj.key === path[path.length - 1]) {
135
+ yield pluginObj.plugin(obj, pluginObj.key, path, specmap);
136
+ }
137
+ } else {
138
+ const parentIndex = path.length - 1;
139
+ const parent = path[parentIndex];
140
+ const indexOfFirstProperties = path.indexOf('properties');
141
+ const isRootProperties = parent === 'properties' && parentIndex === indexOfFirstProperties;
142
+ const traversed = specmap.allowMetaPatches && refCache[obj.$$ref]; // eslint-disable-next-line no-restricted-syntax
143
+
144
+ for (const key of Object.keys(obj)) {
145
+ const val = obj[key];
146
+ const updatedPath = path.concat(key);
147
+
148
+ const isObj = _lib.default.isObject(val);
149
+
150
+ const objRef = obj.$$ref;
151
+
152
+ if (!traversed) {
153
+ if (isObj) {
154
+ // Only store the ref if it exists
155
+ if (specmap.allowMetaPatches && objRef) {
156
+ refCache[objRef] = true;
157
+ }
158
+
159
+ yield* traverse(val, updatedPath, patch);
160
+ }
161
+ }
162
+
163
+ if (!isRootProperties && key === pluginObj.key) {
164
+ const isWithinPathDiscriminator = isSubPath(pathDiscriminator, path);
165
+
166
+ if (!pathDiscriminator || isWithinPathDiscriminator) {
167
+ yield pluginObj.plugin(val, key, updatedPath, specmap, patch);
168
+ }
169
+ }
170
+ }
171
+ }
172
+ }
173
+ };
174
+ }
175
+ }
176
+
177
+ nextPlugin() {
178
+ // Array.prototype.find doesn't work in IE 11 :(
179
+ return (0, _find.default)(this.wrappedPlugins, plugin => {
180
+ const mutations = this.getMutationsForPlugin(plugin);
181
+ return mutations.length > 0;
182
+ });
183
+ }
184
+
185
+ nextPromisedPatch() {
186
+ if (this.promisedPatches.length > 0) {
187
+ return Promise.race(this.promisedPatches.map(patch => patch.value));
188
+ }
189
+
190
+ return undefined;
191
+ }
192
+
193
+ getPluginHistory(plugin) {
194
+ const name = this.constructor.getPluginName(plugin);
195
+ return this.pluginHistory[name] || [];
196
+ }
197
+
198
+ getPluginRunCount(plugin) {
199
+ return this.getPluginHistory(plugin).length;
200
+ }
201
+
202
+ getPluginHistoryTip(plugin) {
203
+ const history = this.getPluginHistory(plugin);
204
+ const val = history && history[history.length - 1];
205
+ return val || {};
206
+ }
207
+
208
+ getPluginMutationIndex(plugin) {
209
+ const mi = this.getPluginHistoryTip(plugin).mutationIndex;
210
+ return typeof mi !== 'number' ? -1 : mi;
211
+ }
212
+
213
+ updatePluginHistory(plugin, val) {
214
+ const name = this.constructor.getPluginName(plugin);
215
+ this.pluginHistory[name] = this.pluginHistory[name] || [];
216
+ this.pluginHistory[name].push(val);
217
+ }
218
+
219
+ updatePatches(patches) {
220
+ _lib.default.normalizeArray(patches).forEach(patch => {
221
+ if (patch instanceof Error) {
222
+ this.errors.push(patch);
223
+ return;
224
+ }
225
+
226
+ try {
227
+ if (!_lib.default.isObject(patch)) {
228
+ this.debug('updatePatches', 'Got a non-object patch', patch);
229
+ return;
230
+ }
231
+
232
+ if (this.showDebug) {
233
+ this.allPatches.push(patch);
234
+ }
235
+
236
+ if (_lib.default.isPromise(patch.value)) {
237
+ this.promisedPatches.push(patch);
238
+ this.promisedPatchThen(patch);
239
+ return;
240
+ }
241
+
242
+ if (_lib.default.isContextPatch(patch)) {
243
+ this.setContext(patch.path, patch.value);
244
+ return;
245
+ }
246
+
247
+ if (_lib.default.isMutation(patch)) {
248
+ this.updateMutations(patch);
249
+ return;
250
+ }
251
+ } catch (e) {
252
+ console.error(e); // eslint-disable-line no-console
253
+
254
+ this.errors.push(e);
255
+ }
256
+ });
257
+ }
258
+
259
+ updateMutations(patch) {
260
+ if (typeof patch.value === 'object' && !Array.isArray(patch.value) && this.allowMetaPatches) {
261
+ patch.value = _objectSpread({}, patch.value);
262
+ }
263
+
264
+ const result = _lib.default.applyPatch(this.state, patch, {
265
+ allowMetaPatches: this.allowMetaPatches
266
+ });
267
+
268
+ if (result) {
269
+ this.mutations.push(patch);
270
+ this.state = result;
271
+ }
272
+ }
273
+
274
+ removePromisedPatch(patch) {
275
+ const index = this.promisedPatches.indexOf(patch);
276
+
277
+ if (index < 0) {
278
+ this.debug("Tried to remove a promisedPatch that isn't there!");
279
+ return;
280
+ }
281
+
282
+ this.promisedPatches.splice(index, 1);
283
+ }
284
+
285
+ promisedPatchThen(patch) {
286
+ patch.value = patch.value.then(val => {
287
+ const promisedPatch = _objectSpread(_objectSpread({}, patch), {}, {
288
+ value: val
289
+ });
290
+
291
+ this.removePromisedPatch(patch);
292
+ this.updatePatches(promisedPatch);
293
+ }).catch(e => {
294
+ this.removePromisedPatch(patch);
295
+ this.updatePatches(e);
296
+ });
297
+ return patch.value;
298
+ }
299
+
300
+ getMutations(from, to) {
301
+ from = from || 0;
302
+
303
+ if (typeof to !== 'number') {
304
+ to = this.mutations.length;
305
+ }
306
+
307
+ return this.mutations.slice(from, to);
308
+ }
309
+
310
+ getCurrentMutations() {
311
+ return this.getMutationsForPlugin(this.getCurrentPlugin());
312
+ }
313
+
314
+ getMutationsForPlugin(plugin) {
315
+ const tip = this.getPluginMutationIndex(plugin);
316
+ return this.getMutations(tip + 1);
317
+ }
318
+
319
+ getCurrentPlugin() {
320
+ return this.currentPlugin;
321
+ }
322
+
323
+ getLib() {
324
+ return this.libMethods;
325
+ } // eslint-disable-next-line no-underscore-dangle
326
+
327
+
328
+ _get(path) {
329
+ return _lib.default.getIn(this.state, path);
330
+ } // eslint-disable-next-line no-underscore-dangle
331
+
332
+
333
+ _getContext(path) {
334
+ return this.contextTree.get(path);
335
+ }
336
+
337
+ setContext(path, value) {
338
+ return this.contextTree.set(path, value);
339
+ } // eslint-disable-next-line no-underscore-dangle
340
+
341
+
342
+ _hasRun(count) {
343
+ const times = this.getPluginRunCount(this.getCurrentPlugin());
344
+ return times > (count || 0);
345
+ }
346
+
347
+ dispatch() {
348
+ const that = this;
349
+ const plugin = this.nextPlugin();
350
+
351
+ if (!plugin) {
352
+ const nextPromise = this.nextPromisedPatch();
353
+
354
+ if (nextPromise) {
355
+ return nextPromise.then(() => this.dispatch()).catch(() => this.dispatch());
356
+ } // We're done!
357
+
358
+
359
+ const result = {
360
+ spec: this.state,
361
+ errors: this.errors
362
+ };
363
+
364
+ if (this.showDebug) {
365
+ result.patches = this.allPatches;
366
+ }
367
+
368
+ return Promise.resolve(result);
369
+ } // Makes sure plugin isn't running an endless loop
370
+
371
+
372
+ that.pluginCount = that.pluginCount || {};
373
+ that.pluginCount[plugin] = (that.pluginCount[plugin] || 0) + 1;
374
+
375
+ if (that.pluginCount[plugin] > HARD_LIMIT) {
376
+ return Promise.resolve({
377
+ spec: that.state,
378
+ errors: that.errors.concat(new Error(`We've reached a hard limit of ${HARD_LIMIT} plugin runs`))
379
+ });
380
+ } // A different plugin runs, wait for all promises to resolve, then retry
381
+
382
+
383
+ if (plugin !== this.currentPlugin && this.promisedPatches.length) {
384
+ const promises = this.promisedPatches.map(p => p.value); // Waits for all to settle instead of Promise.all which stops on rejection
385
+
386
+ return Promise.all(promises.map(promise => {
387
+ return promise.then(_noop.default, _noop.default);
388
+ })).then(() => this.dispatch());
389
+ } // Ok, run the plugin
390
+
391
+
392
+ return executePlugin();
393
+
394
+ function executePlugin() {
395
+ that.currentPlugin = plugin;
396
+ const mutations = that.getCurrentMutations();
397
+ const lastMutationIndex = that.mutations.length - 1;
398
+
399
+ try {
400
+ if (plugin.isGenerator) {
401
+ // eslint-disable-next-line no-restricted-syntax
402
+ for (const yieldedPatches of plugin(mutations, that.getLib())) {
403
+ updatePatches(yieldedPatches);
404
+ }
405
+ } else {
406
+ const newPatches = plugin(mutations, that.getLib());
407
+ updatePatches(newPatches);
408
+ }
409
+ } catch (e) {
410
+ console.error(e); // eslint-disable-line no-console
411
+
412
+ updatePatches([Object.assign(Object.create(e), {
413
+ plugin
414
+ })]);
415
+ } finally {
416
+ that.updatePluginHistory(plugin, {
417
+ mutationIndex: lastMutationIndex
418
+ });
419
+ }
420
+
421
+ return that.dispatch();
422
+ }
423
+
424
+ function updatePatches(patches) {
425
+ if (patches) {
426
+ patches = _lib.default.fullyNormalizeArray(patches);
427
+ that.updatePatches(patches, plugin);
428
+ }
429
+ }
430
+ }
431
+
432
+ }
433
+
434
+ exports.SpecMap = SpecMap;
435
+
436
+ function mapSpec(opts) {
437
+ return new SpecMap(opts).dispatch();
438
+ }
439
+
440
+ const plugins = {
441
+ refs: _refs.default,
442
+ allOf: _allOf.default,
443
+ parameters: _parameters.default,
444
+ properties: _properties.default
445
+ };
446
+ exports.plugins = plugins;
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+
6
+ var _helpers = require("../helpers");
7
+
8
+ 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; }
9
+
10
+ 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; }
11
+
12
+ 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; }
13
+
14
+ var _default = {
15
+ key: 'allOf',
16
+ plugin: (val, key, fullPath, specmap, patch) => {
17
+ // Ignore replace patches created by $ref because the changes will
18
+ // occur in the original "add" patch and we don't want this plugin
19
+ // to redundantly processes those "relace" patches.
20
+ if (patch.meta && patch.meta.$$ref) {
21
+ return undefined;
22
+ }
23
+
24
+ const parent = fullPath.slice(0, -1);
25
+
26
+ if ((0, _helpers.isFreelyNamed)(parent)) {
27
+ return undefined;
28
+ }
29
+
30
+ if (!Array.isArray(val)) {
31
+ const err = new TypeError('allOf must be an array');
32
+ err.fullPath = fullPath; // This is an array
33
+
34
+ return err;
35
+ }
36
+
37
+ let alreadyAddError = false; // Find the original definition from the `patch.value` object
38
+ // Remove the `allOf` property so it doesn't get added to the result of the `allOf` plugin
39
+
40
+ let originalDefinitionObj = patch.value;
41
+ parent.forEach(part => {
42
+ if (!originalDefinitionObj) return; // bail out if we've lost sight of our target
43
+
44
+ originalDefinitionObj = originalDefinitionObj[part];
45
+ });
46
+ originalDefinitionObj = _objectSpread({}, originalDefinitionObj);
47
+ delete originalDefinitionObj.allOf;
48
+ const patches = []; // remove existing content
49
+
50
+ patches.push(specmap.replace(parent, {}));
51
+ val.forEach((toMerge, i) => {
52
+ if (!specmap.isObject(toMerge)) {
53
+ if (alreadyAddError) {
54
+ return null;
55
+ }
56
+
57
+ alreadyAddError = true;
58
+ const err = new TypeError('Elements in allOf must be objects');
59
+ err.fullPath = fullPath; // This is an array
60
+
61
+ return patches.push(err);
62
+ } // Deeply merge the member's contents onto the parent location
63
+
64
+
65
+ patches.push(specmap.mergeDeep(parent, toMerge)); // Generate patches that migrate $ref values based on ContextTree information
66
+ // remove ["allOf"], which will not be present when these patches are applied
67
+
68
+ const collapsedFullPath = fullPath.slice(0, -1);
69
+ const absoluteRefPatches = (0, _helpers.generateAbsoluteRefPatches)(toMerge, collapsedFullPath, {
70
+ getBaseUrlForNodePath: nodePath => {
71
+ return specmap.getContext([...fullPath, i, ...nodePath]).baseDoc;
72
+ },
73
+ specmap
74
+ });
75
+ patches.push(...absoluteRefPatches);
76
+ return undefined;
77
+ }); // Merge back the values from the original definition
78
+
79
+ patches.push(specmap.mergeDeep(parent, originalDefinitionObj)); // If there was not an original $$ref value, make sure to remove
80
+ // any $$ref value that may exist from the result of `allOf` merges
81
+
82
+ if (!originalDefinitionObj.$$ref) {
83
+ patches.push(specmap.remove([].concat(parent, '$$ref')));
84
+ }
85
+
86
+ return patches;
87
+ }
88
+ };
89
+ exports.default = _default;
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+
6
+ 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; }
7
+
8
+ 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; }
9
+
10
+ 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; }
11
+
12
+ class ContextTree {
13
+ constructor(value) {
14
+ this.root = createNode(value || {});
15
+ }
16
+
17
+ set(path, value) {
18
+ const parent = this.getParent(path, true);
19
+
20
+ if (!parent) {
21
+ updateNode(this.root, value, null);
22
+ return;
23
+ }
24
+
25
+ const key = path[path.length - 1];
26
+ const {
27
+ children
28
+ } = parent;
29
+
30
+ if (children[key]) {
31
+ updateNode(children[key], value, parent);
32
+ return;
33
+ }
34
+
35
+ children[key] = createNode(value, parent);
36
+ } // Get the "best" node (node or nearest parent) and return its value.
37
+
38
+
39
+ get(path) {
40
+ path = path || [];
41
+
42
+ if (path.length < 1) {
43
+ return this.root.value;
44
+ }
45
+
46
+ let branch = this.root;
47
+ let child;
48
+ let token;
49
+
50
+ for (let i = 0; i < path.length; i += 1) {
51
+ token = path[i];
52
+ child = branch.children;
53
+
54
+ if (!child[token]) {
55
+ break;
56
+ }
57
+
58
+ branch = child[token];
59
+ }
60
+
61
+ return branch && branch.protoValue;
62
+ }
63
+
64
+ getParent(path, ensureExists) {
65
+ if (!path || path.length < 1) {
66
+ return null;
67
+ }
68
+
69
+ if (path.length < 2) {
70
+ return this.root;
71
+ }
72
+
73
+ return path.slice(0, -1).reduce((branch, token) => {
74
+ if (!branch) {
75
+ return branch;
76
+ }
77
+
78
+ const {
79
+ children
80
+ } = branch;
81
+
82
+ if (!children[token] && ensureExists) {
83
+ children[token] = createNode(null, branch);
84
+ }
85
+
86
+ return children[token];
87
+ }, this.root);
88
+ }
89
+
90
+ } // =========================
91
+ // Utilities
92
+ // =========================
93
+
94
+
95
+ exports.default = ContextTree;
96
+
97
+ function createNode(value, parent) {
98
+ return updateNode({
99
+ children: {}
100
+ }, value, parent);
101
+ }
102
+
103
+ function updateNode(node, value, parent) {
104
+ node.value = value || {};
105
+ node.protoValue = parent ? _objectSpread(_objectSpread({}, parent.protoValue), node.value) : node.value;
106
+ Object.keys(node.children).forEach(prop => {
107
+ const child = node.children[prop];
108
+ node.children[prop] = updateNode(child, child.value, node);
109
+ });
110
+ return node;
111
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = createErrorType;
5
+
6
+ function createErrorType(name, init) {
7
+ function E(...args) {
8
+ if (!Error.captureStackTrace) {
9
+ this.stack = new Error().stack;
10
+ } else {
11
+ Error.captureStackTrace(this, this.constructor);
12
+ }
13
+
14
+ [this.message] = args;
15
+
16
+ if (init) {
17
+ init.apply(this, args);
18
+ }
19
+ }
20
+
21
+ E.prototype = new Error();
22
+ E.prototype.name = name;
23
+ E.prototype.constructor = E;
24
+ return E;
25
+ }