@@ -393,7 +393,7 @@ export async function writeModelRequest() {
393
393
requestWrite . write ( typeString )
394
394
}
395
395
396
- export async function writeOrmActions ( apiRoute : string , modelName : String ) : Promise < void > {
396
+ export async function writeOrmActions ( apiRoute : string , modelName : String , actionPath ?: string ) : Promise < void > {
397
397
const formattedApiRoute = apiRoute . charAt ( 0 ) . toUpperCase ( ) + apiRoute . slice ( 1 )
398
398
399
399
let method = 'GET'
@@ -466,7 +466,9 @@ export async function writeOrmActions(apiRoute: string, modelName: String): Prom
466
466
})
467
467
`
468
468
469
- const actionFile = path . builtUserActionsPath ( `src/${ modelName } ${ formattedApiRoute } OrmAction.ts` )
469
+ const actionName = actionPath || `${ modelName } ${ formattedApiRoute } OrmAction.ts`
470
+
471
+ const actionFile = path . builtUserActionsPath ( `src/${ actionName } ` )
470
472
471
473
if ( fs . existsSync ( actionFile ) ) return
472
474
@@ -540,27 +542,6 @@ function parseRule(rule: string): FieldArrayElement | null {
540
542
)
541
543
}
542
544
543
- export async function lookupFile ( fileName : string ) : Promise < string | null > {
544
- const ormDirectory = path . builtUserActionsPath ( 'src' , { relative : true } )
545
- const filePath = path . join ( ormDirectory , fileName )
546
- const pathExists = fs . existsSync ( filePath )
547
-
548
- // Check if the directory exists
549
- if ( pathExists ) {
550
- return filePath
551
- }
552
-
553
- const actionDirectory = path . userActionsPath ( )
554
- const actionFilePath = path . join ( actionDirectory , fileName )
555
- const fileExists = fs . existsSync ( actionFilePath )
556
-
557
- if ( fileExists ) {
558
- return actionFilePath
559
- }
560
-
561
- return null
562
- }
563
-
564
545
export async function generateApiRoutes ( modelFiles : string [ ] ) {
565
546
const file = Bun . file ( path . frameworkPath ( `orm/routes.ts` ) )
566
547
const writer = file . writer ( )
@@ -595,36 +576,45 @@ export async function generateApiRoutes(modelFiles: string[]) {
595
576
596
577
if ( model . traits . useApi . routes && Object . keys ( model . traits . useApi . routes ) . length > 0 ) {
597
578
const apiRoutes = model . traits . useApi . routes
579
+
598
580
for ( const apiRoute in apiRoutes ) {
599
581
if ( Object . prototype . hasOwnProperty . call ( apiRoutes , apiRoute ) ) {
600
- let path : string | null = ''
582
+ const routePath = apiRoutes [ apiRoute as keyof typeof apiRoutes ]
601
583
602
- await writeOrmActions ( apiRoute as string , modelName )
584
+ await writeOrmActions ( apiRoute as string , modelName , routePath )
603
585
604
- const routePath = apiRoutes [ apiRoute as keyof typeof apiRoutes ]
605
586
if ( typeof routePath !== 'string' ) {
606
587
throw new Error ( `Invalid route path for ${ apiRoute } ` )
607
588
}
608
589
609
- path = `${ routePath } .ts`
610
-
611
- if ( ! path . includes ( '/' ) ) {
612
- path = await lookupFile ( path )
613
- }
614
-
615
- if ( ! path ) {
616
- throw { message : 'Action Not Found!' }
617
- }
590
+ const path = `${ routePath } .ts`
618
591
619
- if ( apiRoute === 'index' ) routeString += `route.get('${ uri } ', '${ path } ')\n\n`
620
- if ( apiRoute === 'show' ) routeString += `route.get('${ uri } /{id}', '${ path } ')\n\n`
621
- if ( apiRoute === 'store' ) routeString += `route.post('${ uri } ', '${ path } ')\n\n`
622
- if ( apiRoute === 'update' ) routeString += `route.patch('${ uri } /{id}', '${ path } ')\n\n`
623
- if ( apiRoute === 'destroy' ) routeString += `route.delete('${ uri } /{id}', '${ path } ')\n\n`
592
+ if ( apiRoute === 'index' ) routeString += `route.get('${ uri } ', '${ path } ').${ middlewareString } \n\n`
593
+ if ( apiRoute === 'show' ) routeString += `route.get('${ uri } /{id}', '${ path } ').${ middlewareString } \n\n`
594
+ if ( apiRoute === 'store' ) routeString += `route.post('${ uri } ', '${ path } ').${ middlewareString } \n\n`
595
+ if ( apiRoute === 'update' ) routeString += `route.patch('${ uri } /{id}', '${ path } ').${ middlewareString } \n\n`
596
+ if ( apiRoute === 'destroy' )
597
+ routeString += `route.delete('${ uri } /{id}', '${ path } ').${ middlewareString } \n\n`
624
598
}
625
599
}
626
600
}
627
601
}
602
+
603
+ if ( typeof model . traits . useApi === 'boolean' && model . traits ?. useApi ) {
604
+ const uri = tableName
605
+
606
+ const apiRoutes = [ 'index' , 'show' , 'store' , 'update' , 'destroy' ]
607
+
608
+ for ( const apiRoute of apiRoutes ) {
609
+ await writeOrmActions ( apiRoute as string , modelName )
610
+
611
+ if ( apiRoute === 'index' ) routeString += `route.get('${ uri } ', '${ path } ').middleware(['Api'])\n\n`
612
+ if ( apiRoute === 'show' ) routeString += `route.get('${ uri } /{id}', '${ path } ').middleware(['Api'])\n\n`
613
+ if ( apiRoute === 'store' ) routeString += `route.post('${ uri } ', '${ path } ').middleware(['Api'])\n\n`
614
+ if ( apiRoute === 'update' ) routeString += `route.patch('${ uri } /{id}', '${ path } ').middleware(['Api'])\n\n`
615
+ if ( apiRoute === 'destroy' ) routeString += `route.delete('${ uri } /{id}', '${ path } ').middleware(['Api'])\n\n`
616
+ }
617
+ }
628
618
}
629
619
}
630
620
0 commit comments