Skip to content

Commit 6b1106a

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

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@ export class Router implements Router {
2222
return '([a-zA-Z0-9-]+)'
2323
})}$`)
2424

25-
this.routes.push({ method, uri, callback, pattern, statusCode })
25+
let routeCallback: Route['callback']
26+
27+
if (typeof callback === 'string' || typeof callback === 'object') {
28+
// Convert string or object to RouteCallback
29+
routeCallback = () => callback
30+
} else {
31+
routeCallback = callback
32+
}
33+
34+
this.routes.push({
35+
method,
36+
url: uri,
37+
uri,
38+
callback: routeCallback,
39+
pattern,
40+
statusCode,
41+
paramNames: []
42+
})
2643
}
2744

2845
public get(url: Route['url'], callback: Route['callback']): this {
@@ -61,10 +78,16 @@ export class Router implements Router {
6178
}
6279

6380
public group(options: RouteGroupOptions | (() => void), callback?: () => void): this {
81+
let cb: () => void
82+
6483
if (typeof options === 'function') {
65-
callback = options
84+
cb = options
6685
options = {}
6786
}
87+
else {
88+
if (!callback) throw new Error('Missing callback function for route group.')
89+
cb = callback
90+
}
6891

6992
const { prefix = '', middleware = [] } = options
7093

@@ -75,7 +98,7 @@ export class Router implements Router {
7598
this.routes = []
7699

77100
// Execute the callback. This will add routes to the new this.routes array.
78-
callback()
101+
cb()
79102

80103
// For each route added by the callback, adjust the URI and add to the original routes array.
81104
this.routes.forEach((r) => {

0 commit comments

Comments
 (0)