From 5b8857771ef1cc31ba3abc9b7ccf059cdc6237c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Gorej?= Date: Fri, 24 Feb 2023 09:29:13 +0100 Subject: [PATCH] feat(resolver): expose resolve makers (#2855) Resolve makers are functions that preconfigure resolve and resolveSubTree function with default options. --- src/index.js | 28 ++++++++++++++++++---------- src/resolver/index.js | 12 +++++++++--- src/subtree-resolver/index.js | 21 ++++++++++++++------- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/index.js b/src/index.js index f42920e5f..c060c2e7a 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -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; diff --git a/src/resolver/index.js b/src/resolver/index.js index 3cbf7ed06..0714ee6d5 100644 --- a/src/resolver/index.js +++ b/src/resolver/index.js @@ -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], +}); diff --git a/src/subtree-resolver/index.js b/src/subtree-resolver/index.js index 84e9463f8..6bfb7aac5 100644 --- a/src/subtree-resolver/index.js +++ b/src/subtree-resolver/index.js @@ -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, @@ -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, @@ -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], +});