Skip to content

Commit c437868

Browse files
committed
chore: wip
1 parent 6b1106a commit c437868

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

.stacks/core/router/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface Router {
1010
patch(url: Route['url'], callback: Route['callback']): this
1111
put(url: Route['url'], callback: Route['callback']): this
1212
group(options: RouteGroupOptions, callback: () => void): this
13+
name(name: string): this
1314
middleware(middleware: Route['middleware']): this
1415
getRoutes(): Promise<Route[]>
1516
}
@@ -18,6 +19,7 @@ export class Router implements Router {
1819
private routes: Route[] = []
1920

2021
private addRoute(method: Route['method'], uri: string, callback: Route['callback'] | string | object, statusCode: StatusCode): void {
22+
const name = uri.replace(/\//g, '.').replace(/:/g, '') // we can improve this
2123
const pattern = new RegExp(`^${uri.replace(/:[a-zA-Z]+/g, (match) => {
2224
return '([a-zA-Z0-9-]+)'
2325
})}$`)
@@ -32,6 +34,7 @@ export class Router implements Router {
3234
}
3335

3436
this.routes.push({
37+
name,
3538
method,
3639
url: uri,
3740
uri,
@@ -118,6 +121,13 @@ export class Router implements Router {
118121
return this
119122
}
120123

124+
public name(name: string): this {
125+
// @ts-ignore
126+
this.routes[this.routes.length - 1].name = name
127+
128+
return this
129+
}
130+
121131
public middleware(middleware: Route['middleware']): this {
122132
// @ts-ignore
123133
this.routes[this.routes.length - 1].middleware = middleware

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ function executeMiddleware(route: Route): void {
5656
}
5757
}
5858

59-
function execute(route: Route, request: any, { statusCode }: { statusCode?: StatusCode }) {
59+
type Options = {
60+
statusCode?: StatusCode
61+
}
62+
63+
function execute(route: Route, request: Request, { statusCode }: Options) {
6064
if (!statusCode)
6165
statusCode = 200
6266

.stacks/core/types/src/router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'before'
3535
export type RouteCallback = (params?: Record<string, any>) => any | string | object
3636

3737
export interface Route {
38+
name: string
3839
uri: string
3940
url: string // used synonymously with uri
4041
method: HttpMethod

0 commit comments

Comments
 (0)