Skip to content

Commit ab6cecf

Browse files
chore: wip
1 parent 8323fbd commit ab6cecf

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import { path as p } from '@stacksjs/path'
77
import { fs } from '@stacksjs/storage'
88
import { kebabCase, pascalCase } from '@stacksjs/strings'
99
import { customValidate, isObjectNotEmpty } from '@stacksjs/validation'
10-
import { StaticRouteManager } from './static'
10+
import { staticRoute } from './'
1111
import { extractDefaultRequest, findRequestInstance } from './utils'
1212

1313
type ActionPath = string
1414

1515
export class Router implements RouterInterface {
1616
private routes: Route[] = []
1717
private path = ''
18-
private staticManager: StaticRouteManager = new StaticRouteManager()
1918

2019
private addRoute(
2120
method: Route['method'],
@@ -142,15 +141,15 @@ export class Router implements RouterInterface {
142141
const uri = this.prepareUri(this.path)
143142

144143
// Add to static manager
145-
this.staticManager.addHtmlFile(uri, htmlFile)
144+
staticRoute.addHtmlFile(uri, htmlFile)
146145

147146
// Register as a route for consistency
148147
return this.addRoute('GET', uri, async () => htmlFile, 200)
149148
}
150149

151150
// New method to get static configuration
152151
public getStaticConfig(): Record<string, any> {
153-
return this.staticManager.getStaticConfig()
152+
return staticRoute.getStaticConfig()
154153
}
155154

156155
public redirect(path: Route['url'], callback: Route['callback'], _status?: RedirectCode): this {
@@ -255,12 +254,16 @@ export class Router implements RouterInterface {
255254
}
256255

257256
public async getRoutes(): Promise<Route[]> {
258-
await import('../../../../../routes/api') // user routes
259-
await import('../../../orm/routes') // auto-generated routes
257+
await this.importRoutes()
260258

261259
return this.routes
262260
}
263261

262+
public async importRoutes(): Promise<void> {
263+
await import('../../../../../routes/api') // user routes
264+
await import('../../../orm/routes') // auto-generated routes
265+
}
266+
264267
public async resolveCallback(callback: Route['callback']): Promise<Route['callback']> {
265268
if (callback instanceof Promise) {
266269
const actionModule = await callback

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getModelName } from '@stacksjs/orm'
77
import { extname, path } from '@stacksjs/path'
88
import { fs, globSync } from '@stacksjs/storage'
99
import { isNumber } from '@stacksjs/validation'
10-
import { route, StaticRouteManager } from '.'
10+
import { route, staticRoute } from '.'
1111

1212
import { middlewares } from './middleware'
1313

@@ -28,8 +28,7 @@ export async function serve(options: ServeOptions = {}): Promise<void> {
2828
const hostname = options.host || 'localhost'
2929
const port = options.port || 3000
3030
const development = options.debug ? true : process.env.APP_ENV !== 'production' && process.env.APP_ENV !== 'prod'
31-
const staticManager: StaticRouteManager = new StaticRouteManager()
32-
const staticFiles = staticManager.getStaticConfig()
31+
const staticFiles = await staticRoute.getStaticConfig()
3332

3433
if (options.timezone)
3534
process.env.TZ = options.timezone
@@ -63,6 +62,7 @@ export async function serverResponse(req: Request, body: string): Promise<Respon
6362
const url: URL = new URL(trimmedUrl) as URL
6463
const routesList: Route[] = await route.getRoutes()
6564

65+
6666
log.info(`Routes List: ${JSON.stringify(routesList)}`)
6767
log.info(`URL: ${JSON.stringify(url)}`)
6868

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { route } from './'
2+
13
export class StaticRouteManager {
24
private staticRoutes: Record<string, any> = {}
35

@@ -8,7 +10,11 @@ export class StaticRouteManager {
810
this.staticRoutes[normalizedUri] = htmlFile
911
}
1012

11-
public getStaticConfig(): Record<string, any> {
13+
public async getStaticConfig(): Promise<Record<string, any>> {
14+
await route.importRoutes()
15+
1216
return this.staticRoutes
1317
}
1418
}
19+
20+
export const staticRoute: StaticRouteManager = new StaticRouteManager()

0 commit comments

Comments
 (0)