Skip to content

Commit

Permalink
feat(resolver): expose resolve makers (#2855)
Browse files Browse the repository at this point in the history
Resolve makers are functions that preconfigure
resolve and resolveSubTree function with default
options.
  • Loading branch information
char0n authored Feb 24, 2023
1 parent 90d1d3c commit 5b88577
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
28 changes: 18 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import Url from 'url';

import Http, { makeHttp, serializeRes, serializeHeaders } from './http/index.js';
import resolve from './resolver/index.js';
import { makeResolve } from './resolver/index.js';
import { makeResolveSubtree } from './subtree-resolver/index.js';
import genericResolveStrategy from './resolver/strategies/generic/index.js';
import openApi2ResolveStrategy, { clearCache } from './resolver/strategies/openapi-2/index.js';
import openApi30ResolveStrategy from './resolver/strategies/openapi-3-0/index.js';
import openApi31ApiDOMResolveStrategy from './resolver/strategies/openapi-3-1-apidom/index.js';
import resolveSubtree from './subtree-resolver/index.js';
import { makeApisTagOperation } from './interfaces.js';
import { execute, buildRequest, baseUrl } from './execute/index.js';
import { opId } from './helpers/index.js';
Expand All @@ -26,14 +26,22 @@ Swagger.resolveStrategies = {
'openapi-2-0': openApi2ResolveStrategy,
generic: genericResolveStrategy,
};
Swagger.resolve = async (options) => {
const mergedOptions = { strategies: Object.values(Swagger.resolveStrategies), ...options };
return resolve(mergedOptions);
};
Swagger.resolveSubtree = async (obj, path, options = {}) => {
const mergedOptions = { strategies: Object.values(Swagger.resolveStrategies), ...options };
return resolveSubtree(obj, path, mergedOptions);
};
Swagger.resolve = makeResolve({
strategies: [
Swagger.resolveStrategies['openapi-3-1-apidom'],
Swagger.resolveStrategies['openapi-3-0'],
Swagger.resolveStrategies['openapi-2-0'],
Swagger.resolveStrategies.generic,
],
});
Swagger.resolveSubtree = makeResolveSubtree({
strategies: [
Swagger.resolveStrategies['openapi-3-1-apidom'],
Swagger.resolveStrategies['openapi-3-0'],
Swagger.resolveStrategies['openapi-2-0'],
Swagger.resolveStrategies.generic,
],
});
Swagger.execute = execute;
Swagger.serializeRes = serializeRes;
Swagger.serializeHeaders = serializeHeaders;
Expand Down
12 changes: 9 additions & 3 deletions src/resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ const resolve = async (options) => {
spec ||
(await makeFetchJSON(httpClient, { requestInterceptor, responseInterceptor })(retrievalURI));
const strategyOptions = { ...options, spec: retrievedSpec };
const strategies = options.strategies || [openApi30Strategy, openApi2Strategy, genericStrategy];
const strategy = strategies.find((strg) => strg.match(strategyOptions));
const strategy = options.strategies.find((strg) => strg.match(strategyOptions));

return strategy.resolve(strategyOptions);
};

export default resolve;
export const makeResolve = (defaultOptions) => async (options) => {
const mergedOptions = { ...defaultOptions, ...options };
return resolve(mergedOptions);
};

export default makeResolve({
strategies: [openApi30Strategy, openApi2Strategy, genericStrategy],
});
21 changes: 14 additions & 7 deletions src/subtree-resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import genericResolverStrategy from '../resolver/strategies/generic/index.js';
import openApi2ResolverStrategy from '../resolver/strategies/openapi-2/index.js';
import openApi30ResolverStrategy from '../resolver/strategies/openapi-3-0/index.js';

export default async function resolveSubtree(obj, path, options = {}) {
export const resolveSubtree = async (obj, path, options = {}) => {
const {
returnEntireTree,
baseDoc,
Expand All @@ -36,12 +36,8 @@ export default async function resolveSubtree(obj, path, options = {}) {
parameterMacro,
modelPropertyMacro,
useCircularStructures,
strategies,
} = options;
const strategies = options.strategies || [
openApi30ResolverStrategy,
openApi2ResolverStrategy,
genericResolverStrategy,
];
const resolveOptions = {
spec: obj,
pathDiscriminator: path,
Expand All @@ -67,4 +63,15 @@ export default async function resolveSubtree(obj, path, options = {}) {
}

return result;
}
};

export const makeResolveSubtree =
(defaultOptions) =>
async (obj, path, options = {}) => {
const mergedOptions = { ...defaultOptions, ...options };
return resolveSubtree(obj, path, mergedOptions);
};

export default makeResolveSubtree({
strategies: [openApi30ResolverStrategy, openApi2ResolverStrategy, genericResolverStrategy],
});

0 comments on commit 5b88577

Please sign in to comment.