@@ -20,33 +20,32 @@ export const ALLOWED_METHODS = [
2020
2121export type AllowedMethods = ( typeof ALLOWED_METHODS ) [ number ] ;
2222
23- const toOpenAPIPath = ( path : string ) =>
24- path
25- . split ( "/" )
26- . map ( ( x ) => {
27- let tmp = x ;
28-
29- // Example - ":id"
30- if ( tmp . startsWith ( ":" ) ) {
31- const match = tmp . match ( / ^ : ( [ ^ { ? ] + ) (?: { ( .+ ) } ) ? ( \? ) ? $ / ) ;
32- if ( match ) {
33- const paramName = match [ 1 ] ;
34- tmp = `{${ paramName } }` ;
35- } else {
36- // Remove the leading colon ":"
37- tmp = tmp . slice ( 1 , tmp . length ) ;
23+ const toOpenAPIPathSegment = ( segment : string ) => {
24+ let tmp = segment ;
25+
26+ // Example - ":id"
27+ if ( tmp . startsWith ( ":" ) ) {
28+ const match = tmp . match ( / ^ : ( [ ^ { ? ] + ) (?: { ( .+ ) } ) ? ( \? ) ? $ / ) ;
29+ if ( match ) {
30+ const paramName = match [ 1 ] ;
31+ tmp = `{${ paramName } }` ;
32+ } else {
33+ // Remove the leading colon ":"
34+ tmp = tmp . slice ( 1 , tmp . length ) ;
3835
39- // If it ends with "?", remove it
40- // This is for optional parameters
41- if ( tmp . endsWith ( "?" ) ) tmp = tmp . slice ( 0 , - 1 ) ;
36+ // If it ends with "?", remove it
37+ // This is for optional parameters
38+ if ( tmp . endsWith ( "?" ) ) tmp = tmp . slice ( 0 , - 1 ) ;
4239
43- tmp = `{${ tmp } }` ;
44- }
45- }
40+ tmp = `{${ tmp } }` ;
41+ }
42+ }
4643
47- return tmp ;
48- } )
49- . join ( "/" ) ;
44+ return tmp ;
45+ } ;
46+
47+ const toOpenAPIPath = ( path : string ) =>
48+ path . split ( "/" ) . map ( toOpenAPIPathSegment ) . join ( "/" ) ;
5049
5150const capitalize = ( word : string ) =>
5251 word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) ;
@@ -57,10 +56,11 @@ const generateOperationId = (route: RouterRoute) => {
5756 if ( route . path === "/" ) return `${ operationId } Index` ;
5857
5958 for ( const segment of route . path . split ( "/" ) ) {
60- if ( segment . charCodeAt ( 0 ) === 123 ) {
61- operationId += `By${ capitalize ( segment . slice ( 1 , - 1 ) ) } ` ;
59+ const openApiPathSegment = toOpenAPIPathSegment ( segment ) ;
60+ if ( openApiPathSegment . charCodeAt ( 0 ) === 123 ) {
61+ operationId += `By${ capitalize ( openApiPathSegment . slice ( 1 , - 1 ) ) } ` ;
6262 } else {
63- operationId += capitalize ( segment ) ;
63+ operationId += capitalize ( openApiPathSegment ) ;
6464 }
6565 }
6666
0 commit comments