Skip to content

Commit fb0b870

Browse files
committed
chore: wip
1 parent 41a3455 commit fb0b870

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

.stacks/core/router/src/server.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ Bun.serve({
1111
async fetch(req) {
1212
const url = new URL(req.url)
1313

14-
const foundRoute: Route = routesList.find((route: Route) => {
14+
const foundRoute: Route | undefined = routesList.find((route: Route) => {
1515
const pattern = new RegExp(`^${route.uri.replace(/:\w+/g, '\\w+')}$`)
1616

1717
return pattern.test(url.pathname)
18-
}) as Route
18+
})
1919

2020
if (url.pathname === '/favicon.ico')
2121
return new Response('')
2222

2323
if (!foundRoute)
2424
return new Response('Not found', { status: 404 })
2525

26-
addRouteParamsandQuery(url, foundRoute)
26+
addRouteParamsAndQuery(url, foundRoute)
2727
executeMiddleware(foundRoute)
2828

29-
return execute(foundRoute, req, { statusCode: foundRoute.statusCode })
29+
return execute(foundRoute, req, { statusCode: foundRoute?.statusCode })
3030
},
3131
})
3232

33-
function addRouteParamsandQuery(url: URL, route: Route): void {
33+
function addRouteParamsAndQuery(url: URL, route: Route): void {
3434
if (!isObjectNotEmpty(url.searchParams))
3535
request.addQuery(url)
3636

@@ -56,7 +56,10 @@ function executeMiddleware(route: Route): void {
5656
}
5757
}
5858

59-
function execute(route: Route, request: any, { statusCode }: { statusCode: StatusCode }) {
59+
function execute(route: Route, request: any, { statusCode }: { statusCode?: StatusCode }) {
60+
if (!statusCode)
61+
statusCode = 200
62+
6063
if (route?.method === 'GET' && (statusCode === 301 || statusCode === 302)) {
6164
const callback = String(route.callback)
6265

0 commit comments

Comments
 (0)