[d24f17c] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
---|
| 4 | exports.__esModule = true;
|
---|
| 5 | exports.makeResolveSubtree = exports.default = void 0;
|
---|
| 6 | var _index = _interopRequireDefault(require("../resolver/index.js"));
|
---|
| 7 | var _index2 = _interopRequireDefault(require("../resolver/strategies/generic/index.js"));
|
---|
| 8 | var _index3 = _interopRequireDefault(require("../resolver/strategies/openapi-2/index.js"));
|
---|
| 9 | var _index4 = _interopRequireDefault(require("../resolver/strategies/openapi-3-0/index.js"));
|
---|
| 10 | // The subtree resolver is a higher-level interface that allows you to
|
---|
| 11 | // get the same result that you would from `Swagger.resolve`, but focuses on
|
---|
| 12 | // a subtree of your object.
|
---|
| 13 | //
|
---|
| 14 | // It makes several assumptions that allow you to think less about what resolve,
|
---|
| 15 | // specmap, and normalizeSwagger are doing: if this is not suitable for you,
|
---|
| 16 | // you can emulate `resolveSubtree`'s behavior by talking to the traditional
|
---|
| 17 | // resolver directly.
|
---|
| 18 | //
|
---|
| 19 | // By providing a top-level `obj` and a `path` to resolve within, the subtree
|
---|
| 20 | // at `path` will be resolved and normalized in the context of your top-level
|
---|
| 21 | // `obj`. You'll get the resolved subtree you're interest in as a return value
|
---|
| 22 | // (or, you can use `returnEntireTree` to get everything back).
|
---|
| 23 | //
|
---|
| 24 | // This is useful for cases where resolving your entire object is unnecessary
|
---|
| 25 | // and/or non-performant; we use this interface for lazily resolving operations
|
---|
| 26 | // and models in Swagger-UI, which allows us to handle larger definitions.
|
---|
| 27 | //
|
---|
| 28 | // It's likely that Swagger-Client will rely entirely on lazy resolving in
|
---|
| 29 | // future versions.
|
---|
| 30 | //
|
---|
| 31 | // TODO: move the remarks above into project documentation
|
---|
| 32 |
|
---|
| 33 | const resolveSubtree = async (obj, path, options = {}) => {
|
---|
| 34 | const {
|
---|
| 35 | returnEntireTree,
|
---|
| 36 | baseDoc,
|
---|
| 37 | requestInterceptor,
|
---|
| 38 | responseInterceptor,
|
---|
| 39 | parameterMacro,
|
---|
| 40 | modelPropertyMacro,
|
---|
| 41 | useCircularStructures,
|
---|
| 42 | strategies
|
---|
| 43 | } = options;
|
---|
| 44 | const resolveOptions = {
|
---|
| 45 | spec: obj,
|
---|
| 46 | pathDiscriminator: path,
|
---|
| 47 | baseDoc,
|
---|
| 48 | requestInterceptor,
|
---|
| 49 | responseInterceptor,
|
---|
| 50 | parameterMacro,
|
---|
| 51 | modelPropertyMacro,
|
---|
| 52 | useCircularStructures,
|
---|
| 53 | strategies
|
---|
| 54 | };
|
---|
| 55 | const strategy = strategies.find(strg => strg.match(resolveOptions));
|
---|
| 56 | const normalized = strategy.normalize(resolveOptions);
|
---|
| 57 | const result = await (0, _index.default)({
|
---|
| 58 | ...resolveOptions,
|
---|
| 59 | spec: normalized,
|
---|
| 60 | allowMetaPatches: true,
|
---|
| 61 | skipNormalization: true
|
---|
| 62 | });
|
---|
| 63 | if (!returnEntireTree && Array.isArray(path) && path.length) {
|
---|
| 64 | result.spec = path.reduce((acc, pathSegment) => acc == null ? void 0 : acc[pathSegment], result.spec) || null;
|
---|
| 65 | }
|
---|
| 66 | return result;
|
---|
| 67 | };
|
---|
| 68 | const makeResolveSubtree = defaultOptions => async (obj, path, options = {}) => {
|
---|
| 69 | const mergedOptions = {
|
---|
| 70 | ...defaultOptions,
|
---|
| 71 | ...options
|
---|
| 72 | };
|
---|
| 73 | return resolveSubtree(obj, path, mergedOptions);
|
---|
| 74 | };
|
---|
| 75 | exports.makeResolveSubtree = makeResolveSubtree;
|
---|
| 76 | var _default = exports.default = makeResolveSubtree({
|
---|
| 77 | strategies: [_index4.default, _index3.default, _index2.default]
|
---|
| 78 | }); |
---|