From b18cb17a3de81bce7df64fe7ace9305118383530 Mon Sep 17 00:00:00 2001 From: Nikolay Lukinyh Date: Fri, 18 Dec 2020 14:53:41 +0300 Subject: [PATCH] Generation of the module name: added the ability to pass the url index, which will be taken as the module name. --- index.d.ts | 5 +++++ src/config.js | 3 +++ src/index.js | 4 +++- src/routes.js | 4 ++-- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index a5d17785..cf1fc698 100644 --- a/index.d.ts +++ b/index.d.ts @@ -55,6 +55,11 @@ interface GenerateApiParams { * generate js api module with declaration file (default: false) */ toJS?: boolean; + + /** + * url index from paths used for merging into modules + */ + moduleNameIndex?: number; } export declare function generateApi( diff --git a/src/config.js b/src/config.js index 43cd42fd..41930966 100644 --- a/src/config.js +++ b/src/config.js @@ -22,6 +22,9 @@ const config = { componentsMap: {}, /** flag for catching convertion from swagger 2.0 */ convertedFromSwagger2: false, + + /** url index from paths used for merging into modules */ + moduleNameIndex: 0, }; /** needs to use data everywhere in project */ diff --git a/src/index.js b/src/index.js index ef0b740c..1fa5fe7d 100644 --- a/src/index.js +++ b/src/index.js @@ -44,6 +44,7 @@ module.exports = { generateRouteTypes = config.generateRouteTypes, generateClient = config.generateClient, generateUnionEnums = config.generateUnionEnums, + moduleNameIndex = config.moduleNameIndex, }) => new Promise((resolve, reject) => { addToConfig({ @@ -53,6 +54,7 @@ module.exports = { generateResponses, templates, generateUnionEnums, + moduleNameIndex, }); (spec ? convertSwaggerObject(spec) : getSwaggerObject(input, url)) .then(({ usageSchema, originalSchema }) => { @@ -78,7 +80,7 @@ module.exports = { const schemasMap = filterComponentsMap(componentsMap, "schemas"); const parsedSchemas = parseSchemas(components); - const routes = parseRoutes(usageSchema, parsedSchemas, componentsMap, components); + const routes = parseRoutes(usageSchema, parsedSchemas, componentsMap, components, moduleNameIndex); const hasSecurityRoutes = routes.some((route) => route.security); const hasQueryRoutes = routes.some((route) => route.hasQuery); const hasFormDataRoutes = routes.some((route) => route.hasFormDataParams); diff --git a/src/routes.js b/src/routes.js index a3b2cce4..6ea3de8f 100644 --- a/src/routes.js +++ b/src/routes.js @@ -191,7 +191,7 @@ const createRequestsMap = (requestInfoByMethodsMap) => { ); }; -const parseRoutes = ({ paths, security: globalSecurity }, parsedSchemas) => +const parseRoutes = ({ paths, security: globalSecurity }, parsedSchemas, componentsMap, components, moduleNameIndex) => _.entries(paths).reduce((routes, [route, requestInfoByMethodsMap]) => { if (route.startsWith("x-")) return routes; @@ -224,7 +224,7 @@ const parseRoutes = ({ paths, security: globalSecurity }, parsedSchemas) => let formDataRequestBody = requestBodyType && requestBodyType.dataType === "multipart/form-data"; - const moduleName = _.camelCase(_.compact(_.split(route, "/"))[0]); + const moduleName = _.camelCase(_.compact(_.split(route, "/"))[moduleNameIndex]); const routeName = getRouteName(operationId, method, route, moduleName); const name = _.camelCase(routeName);