source: node_modules/swagger-client/es/subtree-resolver/index.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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