Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(resolver): expose resolve makers #2855

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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],
});