1
1
import { log } from '@stacksjs/logging'
2
2
import { path as p , projectStoragePath , routesPath } from '@stacksjs/path'
3
+ import { Action , Job } from '@stacksjs/types'
3
4
import { pascalCase } from '@stacksjs/strings'
4
5
import type { RedirectCode , Route , RouteGroupOptions , RouterInterface , StatusCode } from '@stacksjs/types'
5
6
@@ -62,20 +63,18 @@ export class Router implements RouterInterface {
62
63
public async email ( path : Route [ 'url' ] ) : Promise < this> {
63
64
path = pascalCase ( path )
64
65
65
- const emailModule = await import ( p . userNotificationsPath ( `${ path } .ts` ) )
66
- const callback = emailModule . default . handle
67
-
66
+ const emailModule = ( await import ( p . userNotificationsPath ( `${ path } .ts` ) ) ) . default as Action
67
+ const callback = emailModule . handle
68
68
const uri = this . prepareUri ( path )
69
69
this . addRoute ( 'GET' , uri , callback , 200 )
70
70
71
71
return this
72
72
}
73
73
74
74
public async health ( ) : Promise < this> {
75
- const healthModule = await import ( p . userActionsPath ( 'HealthAction.ts' ) )
76
- const callback = healthModule . default . handle
77
-
78
- const path = healthModule . default . path ?? `${ this . apiPrefix } /health`
75
+ const healthModule = ( await import ( p . userActionsPath ( 'HealthAction.ts' ) ) ) . default as Action
76
+ const callback = healthModule . handle
77
+ const path = healthModule . path ?? `${ this . apiPrefix } /health`
79
78
80
79
this . addRoute ( 'GET' , path , callback , 200 )
81
80
@@ -86,8 +85,8 @@ export class Router implements RouterInterface {
86
85
path = pascalCase ( path )
87
86
88
87
// removes the potential `JobJob` suffix in case the user does not choose to use the Job suffix in their file name
89
- const jobModule = await import ( p . userJobsPath ( `${ path } Job.ts` . replace ( / J o b J o b / , 'Job' ) ) )
90
- const callback = jobModule . default . handle
88
+ const jobModule = ( await import ( p . userJobsPath ( `${ path } Job.ts` . replace ( / J o b J o b / , 'Job' ) ) ) ) . default as Job
89
+ const callback = jobModule . handle
91
90
92
91
path = this . prepareUri ( path )
93
92
this . addRoute ( 'GET' , path , callback , 200 )
@@ -101,12 +100,12 @@ export class Router implements RouterInterface {
101
100
let callback : Route [ 'callback' ]
102
101
try {
103
102
// removes the potential `ActionAction` suffix in case the user does not choose to use the Job suffix in their file name
104
- const actionModule = await import ( p . userActionsPath ( `${ path } Action.ts` . replace ( / A c t i o n A c t i o n / , 'Action' ) ) )
105
- callback = actionModule . default . handle
103
+ const actionModule = ( await import ( p . userActionsPath ( `${ path } Action.ts` . replace ( / A c t i o n A c t i o n / , 'Action' ) ) ) ) . default as Action
104
+ callback = actionModule . handle
106
105
} catch ( error ) {
107
106
try {
108
- const actionModule = await import ( p . userActionsPath ( `${ path } .ts` . replace ( / A c t i o n A c t i o n / , 'Action' ) ) )
109
- callback = actionModule . default . handle
107
+ const actionModule = ( await import ( p . userActionsPath ( `${ path } .ts` . replace ( / A c t i o n A c t i o n / , 'Action' ) ) ) ) . default as Action
108
+ callback = actionModule . handle
110
109
} catch ( error ) {
111
110
log . error ( `Could not find action module for path: ${ path } ` )
112
111
return this
@@ -140,16 +139,19 @@ export class Router implements RouterInterface {
140
139
141
140
public delete ( path : Route [ 'url' ] , callback : Route [ 'callback' ] ) : this {
142
141
this . addRoute ( 'DELETE' , path , callback , 204 )
142
+
143
143
return this
144
144
}
145
145
146
146
public patch ( path : Route [ 'url' ] , callback : Route [ 'callback' ] ) : this {
147
147
this . addRoute ( 'PATCH' , path , callback , 202 )
148
+
148
149
return this
149
150
}
150
151
151
152
public put ( path : Route [ 'url' ] , callback : Route [ 'callback' ] ) : this {
152
153
this . addRoute ( 'PUT' , path , callback , 202 )
154
+
153
155
return this
154
156
}
155
157
@@ -261,7 +263,7 @@ export class Router implements RouterInterface {
261
263
if ( typeof callback === 'string' ) return await this . importCallbackFromPath ( callback , this . path )
262
264
263
265
// in this case, the callback ends up being a function
264
- return await callback
266
+ return callback
265
267
}
266
268
267
269
private async importCallbackFromPath ( callbackPath : string , originalPath : string ) : Promise < Route [ 'callback' ] > {
0 commit comments