Skip to content

Commit cf901b9

Browse files
committed
chore: wip
1 parent fcbf342 commit cf901b9

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

storage/framework/core/router/src/router.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { log } from '@stacksjs/logging'
22
import { path as p, projectStoragePath, routesPath } from '@stacksjs/path'
3+
import { Action, Job } from '@stacksjs/types'
34
import { pascalCase } from '@stacksjs/strings'
45
import type { RedirectCode, Route, RouteGroupOptions, RouterInterface, StatusCode } from '@stacksjs/types'
56

@@ -62,20 +63,18 @@ export class Router implements RouterInterface {
6263
public async email(path: Route['url']): Promise<this> {
6364
path = pascalCase(path)
6465

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
6868
const uri = this.prepareUri(path)
6969
this.addRoute('GET', uri, callback, 200)
7070

7171
return this
7272
}
7373

7474
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`
7978

8079
this.addRoute('GET', path, callback, 200)
8180

@@ -86,8 +85,8 @@ export class Router implements RouterInterface {
8685
path = pascalCase(path)
8786

8887
// 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(/JobJob/, 'Job')))
90-
const callback = jobModule.default.handle
88+
const jobModule = (await import(p.userJobsPath(`${path}Job.ts`.replace(/JobJob/, 'Job')))).default as Job
89+
const callback = jobModule.handle
9190

9291
path = this.prepareUri(path)
9392
this.addRoute('GET', path, callback, 200)
@@ -101,12 +100,12 @@ export class Router implements RouterInterface {
101100
let callback: Route['callback']
102101
try {
103102
// 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(/ActionAction/, 'Action')))
105-
callback = actionModule.default.handle
103+
const actionModule = (await import(p.userActionsPath(`${path}Action.ts`.replace(/ActionAction/, 'Action')))).default as Action
104+
callback = actionModule.handle
106105
} catch (error) {
107106
try {
108-
const actionModule = await import(p.userActionsPath(`${path}.ts`.replace(/ActionAction/, 'Action')))
109-
callback = actionModule.default.handle
107+
const actionModule = (await import(p.userActionsPath(`${path}.ts`.replace(/ActionAction/, 'Action')))).default as Action
108+
callback = actionModule.handle
110109
} catch (error) {
111110
log.error(`Could not find action module for path: ${path}`)
112111
return this
@@ -140,16 +139,19 @@ export class Router implements RouterInterface {
140139

141140
public delete(path: Route['url'], callback: Route['callback']): this {
142141
this.addRoute('DELETE', path, callback, 204)
142+
143143
return this
144144
}
145145

146146
public patch(path: Route['url'], callback: Route['callback']): this {
147147
this.addRoute('PATCH', path, callback, 202)
148+
148149
return this
149150
}
150151

151152
public put(path: Route['url'], callback: Route['callback']): this {
152153
this.addRoute('PUT', path, callback, 202)
154+
153155
return this
154156
}
155157

@@ -261,7 +263,7 @@ export class Router implements RouterInterface {
261263
if (typeof callback === 'string') return await this.importCallbackFromPath(callback, this.path)
262264

263265
// in this case, the callback ends up being a function
264-
return await callback
266+
return callback
265267
}
266268

267269
private async importCallbackFromPath(callbackPath: string, originalPath: string): Promise<Route['callback']> {

0 commit comments

Comments
 (0)