11const _ = require ( "lodash" ) ;
22const { parseSchema } = require ( "./schema" ) ;
3+ const { checkAndRenameModelName } = require ( "./modelNames" ) ;
34const { inlineExtraFormatters } = require ( "./typeFormatters" ) ;
45
6+ const methodAliases = {
7+ get : ( pathName , hasPathInserts ) => _ . camelCase ( `${ pathName } _${ hasPathInserts ? 'detail' : 'list' } ` ) ,
8+ post : ( pathName , hasPathInserts ) => _ . camelCase ( `${ pathName } _create` ) ,
9+ put : ( pathName , hasPathInserts ) => _ . camelCase ( `${ pathName } _update` ) ,
10+ patch : ( pathName , hasPathInserts ) => _ . camelCase ( `${ pathName } _partial_update` ) ,
11+ delete : ( pathName , hasPathInserts ) => _ . camelCase ( `${ pathName } _delete` )
12+ }
13+
514const getTypeFromRequestInfo = ( requestInfo , parsedSchemas , contentType ) => {
615 // TODO: make more flexible pick schema without content type
716 const schema = _ . get ( requestInfo , `content["${ contentType } "].schema` ) ;
@@ -10,7 +19,8 @@ const getTypeFromRequestInfo = (requestInfo, parsedSchemas, contentType) => {
1019 const extractedSchema = _ . get ( schema , 'additionalProperties' , schema ) ;
1120 const { content } = parseSchema ( extractedSchema , 'none' , inlineExtraFormatters ) ;
1221 const foundSchema = _ . find ( parsedSchemas , parsedSchema => _ . isEqual ( parsedSchema . content , content ) )
13- return foundSchema ? foundSchema . name : content ;
22+
23+ return foundSchema ? foundSchema . name : checkAndRenameModelName ( content ) ;
1424 }
1525
1626 return 'any' ;
@@ -20,14 +30,17 @@ const findSuccessResponse = (responses) => {
2030 return _ . find ( responses , ( v , status ) => status === 'default' || ( + status >= 200 && + status < 300 ) )
2131}
2232
33+ const createCustomOperationId = ( method , route , moduleName ) => {
34+ const hasPathInserts = / \{ ( \w ) { 1 , } \} / g. test ( route ) ;
35+ const splitedRouteBySlash = _ . replace ( route , / \{ ( \w ) { 1 , } \} / g, '' ) . split ( '/' ) . filter ( Boolean ) ;
36+ const routeParts = ( splitedRouteBySlash . length > 1 ? splitedRouteBySlash . splice ( 1 ) : splitedRouteBySlash ) . join ( '_' ) ;
37+ return routeParts . length > 3 && methodAliases [ method ] ? methodAliases [ method ] ( routeParts , hasPathInserts ) : _ . camelCase ( _ . lowerCase ( method ) + '_' + ( [ moduleName ] ) . join ( '_' ) ) || 'index'
38+ }
39+
2340const getRouteName = ( operationId , method , route , moduleName ) => {
2441 if ( operationId ) return operationId ;
2542 if ( route === '/' ) return `${ _ . lowerCase ( method ) } Root` ;
26-
27- const routeParts = _ . replace ( route , / \{ ( \w ) { 1 , } \} / g, '' ) . split ( '/' ) . filter ( Boolean ) ;
28-
29- // create route name via method and route
30- return _ . camelCase ( _ . lowerCase ( method ) + '_' + ( moduleName ? routeParts . splice ( 1 ) : routeParts ) . join ( '_' ) ) || 'index'
43+ return createCustomOperationId ( method , route , moduleName ) ;
3144}
3245
3346const parseRoutes = ( routes , parsedSchemas ) =>
@@ -159,7 +172,7 @@ const groupRoutes = routes => {
159172 `🥵 This method has been renamed to "${ route . name + ( duplicates [ route . moduleName ] [ route . name ] + 1 ) } ()" to solve conflict names.`
160173 )
161174 route . comments . push ( `@originalName ${ route . name } ` )
162- route . comments . push ( `@duplicate true ` )
175+ route . comments . push ( `@duplicate` )
163176 route . name += ++ duplicates [ route . moduleName ] [ route . name ] ;
164177 }
165178
@@ -171,21 +184,37 @@ const groupRoutes = routes => {
171184 return modules
172185 } , {
173186 $outOfModule : [ ]
174- } ) , ( shuffle , packRoutes , moduleName ) => {
175-
187+ } ) , ( acc , packRoutes , moduleName ) => {
188+
189+ // if (moduleName === "$outOfModule") {
190+ // acc.outOfModule.push(...routes)
191+ // } else {
192+ // if (routes.length === 1) {
193+ // const route = routes[0]
194+ // acc.outOfModule.push({
195+ // ...route,
196+ // name: route.name === _.lowerCase(route.name) ? moduleName : route.name,
197+ // })
198+ // } else {
199+ // acc.combined.push({
200+ // moduleName,
201+ // routes: routes,
202+ // })
203+ // }
204+ // }
176205
177206 if ( moduleName === "$outOfModule" ) {
178- shuffle [ ' outOfModule' ] = packRoutes
207+ acc . outOfModule = packRoutes
179208 } else {
180- if ( ! shuffle . combined ) shuffle . combined = [ ]
209+ if ( ! acc . combined ) acc . combined = [ ]
181210
182- shuffle . combined . push ( {
211+ acc . combined . push ( {
183212 moduleName,
184213 routes : packRoutes ,
185214 } )
186215 }
187216
188- return shuffle ;
217+ return acc ;
189218 } , { } )
190219}
191220
0 commit comments