Skip to content

Commit 9596c3c

Browse files
authored
Generation of the module name: added the ability to pass the url index, which will be taken as the module name. (#97)
1 parent 2a54d4a commit 9596c3c

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ interface GenerateApiParams {
5555
* generate js api module with declaration file (default: false)
5656
*/
5757
toJS?: boolean;
58+
59+
/**
60+
* url index from paths used for merging into modules
61+
*/
62+
moduleNameIndex?: number;
5863
}
5964

6065
export declare function generateApi(

src/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ const config = {
2222
componentsMap: {},
2323
/** flag for catching convertion from swagger 2.0 */
2424
convertedFromSwagger2: false,
25+
26+
/** url index from paths used for merging into modules */
27+
moduleNameIndex: 0,
2528
};
2629

2730
/** needs to use data everywhere in project */

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = {
4444
generateRouteTypes = config.generateRouteTypes,
4545
generateClient = config.generateClient,
4646
generateUnionEnums = config.generateUnionEnums,
47+
moduleNameIndex = config.moduleNameIndex,
4748
}) =>
4849
new Promise((resolve, reject) => {
4950
addToConfig({
@@ -53,6 +54,7 @@ module.exports = {
5354
generateResponses,
5455
templates,
5556
generateUnionEnums,
57+
moduleNameIndex,
5658
});
5759
(spec ? convertSwaggerObject(spec) : getSwaggerObject(input, url))
5860
.then(({ usageSchema, originalSchema }) => {
@@ -78,7 +80,7 @@ module.exports = {
7880
const schemasMap = filterComponentsMap(componentsMap, "schemas");
7981

8082
const parsedSchemas = parseSchemas(components);
81-
const routes = parseRoutes(usageSchema, parsedSchemas, componentsMap, components);
83+
const routes = parseRoutes(usageSchema, parsedSchemas, componentsMap, components, moduleNameIndex);
8284
const hasSecurityRoutes = routes.some((route) => route.security);
8385
const hasQueryRoutes = routes.some((route) => route.hasQuery);
8486
const hasFormDataRoutes = routes.some((route) => route.hasFormDataParams);

src/routes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ const createRequestsMap = (requestInfoByMethodsMap) => {
191191
);
192192
};
193193

194-
const parseRoutes = ({ paths, security: globalSecurity }, parsedSchemas) =>
194+
const parseRoutes = ({ paths, security: globalSecurity }, parsedSchemas, componentsMap, components, moduleNameIndex) =>
195195
_.entries(paths).reduce((routes, [route, requestInfoByMethodsMap]) => {
196196
if (route.startsWith("x-")) return routes;
197197

@@ -224,7 +224,7 @@ const parseRoutes = ({ paths, security: globalSecurity }, parsedSchemas) =>
224224
let formDataRequestBody =
225225
requestBodyType && requestBodyType.dataType === "multipart/form-data";
226226

227-
const moduleName = _.camelCase(_.compact(_.split(route, "/"))[0]);
227+
const moduleName = _.camelCase(_.compact(_.split(route, "/"))[moduleNameIndex]);
228228

229229
const routeName = getRouteName(operationId, method, route, moduleName);
230230
const name = _.camelCase(routeName);

0 commit comments

Comments
 (0)