Skip to content

Commit efc1b1e

Browse files
committed
chore: wip
chore: wip
1 parent 077bc11 commit efc1b1e

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

routes/api.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,27 @@ import { route } from '@stacksjs/router'
88
* @see https://stacksjs.org/docs/routing
99
*/
1010

11-
route.get('/', () => 'hello world') // stacksjs.org/api/
12-
route.get('/welcome', () => 'hello world 2') // stacksjs.org/api/welcome
13-
route.get('/welcome/', () => 'hello world 3') // stacksjs.org/api/welcome/
14-
route.get('/buddy/commands', 'Buddy/CommandsAction')
15-
route.get('/buddy/versions', 'Buddy/VersionsAction')
16-
// route.get('/buddy/test-1', '../app/Actions/BuddyAction') // todo: support this
17-
// route.get('/buddy/test-2', import('../app/Actions/BuddyAction')) // todo: support this
11+
route.get('/', () => 'hello world') // $APP_URL/api
12+
13+
route.get('/welcome', () => { // stacksjs.org/api/welcome
14+
return { // you may return an object as well
15+
data: 'hello world, friend',
16+
}
17+
})
18+
19+
route.get('/hello/world', () => 'hello world, buddy') // stacksjs.org/api/hello/world
20+
21+
route.group('/buddy', () => { // you may group your routes in a few different ways
22+
route.get('/commands', 'Actions/Buddy/CommandsAction')
23+
24+
route.group({ prefix: 'commands' }, () => {
25+
route.get('/example-two', import('Actions/Buddy/CommandsAction')) // or import the action directly
26+
})
27+
28+
route.get('/versions', '../app/Actions/Buddy/VersionsAction') // a relative path is accepted as well
29+
})
1830

1931
route.action('/example') // the equivalent of route.get('/example', 'ExampleAction')
2032
route.job('/example-two') // the equivalent of route.get('/example-two', 'ExampleTwoJob')
2133

22-
route.health() // /api/health
34+
route.health() // adds an `/api/health` route

routes/users.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { route } from '@stacksjs/router'
1+
// import { route } from '@stacksjs/router'
22

33
// based on the route's file name, it wraps the contents into a
44
// `route.group({ prefix: '/users' }, () => { ... })` block
55

6-
route.get('/:id', ({ id }) => {
7-
// Retrieve user from database
8-
return `User with ID ${id}`
9-
})
6+
// route.get('/:id', ({ id }) => {
7+
// // Retrieve user from database
8+
// return `User with ID ${id}`
9+
// })
1010

11-
route.post('/', ({ name, email }) => {
12-
// Save user to database
13-
return `User ${name} (${email}) created`
14-
})
11+
// route.post('/', ({ name, email }) => {
12+
// // Save user to database
13+
// return `User ${name} (${email}) created`
14+
// })

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export class Router implements RouterInterface {
4949
}
5050

5151
public async get(path: Route['url'], callback: Route['callback']): Promise<this> {
52+
// if the route ends with a /, then remove it
53+
if (path.endsWith('/'))
54+
path = path.slice(0, -1)
55+
5256
// check if callback is a string and if it is, then import that module path and use the default.handle function as the callback
5357
if (callback instanceof Promise) {
5458
const actionModule = await callback
@@ -159,7 +163,7 @@ export class Router implements RouterInterface {
159163
return this
160164
}
161165

162-
public group(options: RouteGroupOptions | (() => void), callback?: () => void): this {
166+
public group(options: RouteGroupOptions | (() => void) | string, callback?: () => void): this {
163167
let cb: () => void
164168

165169
if (typeof options === 'function') {

storage/framework/core/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"stacks/*": ["./*/src"],
1616
"buddy": ["./buddy/src/index.ts"],
1717
"buddy/*": ["./buddy/src/*"],
18+
"Actions/*": ["../../../app/Actions/*"],
19+
"Jobs/*": ["../../../app/Jobs/*"],
1820
"~/*": ["../../../*"]
1921
},
2022
"resolveJsonModule": true,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface Route {
4343
url: string // used synonymously with uri
4444
method: HttpMethod
4545
pattern: RegExp
46-
callback: RouteCallback | ActionName | Action
46+
callback: RouteCallback | ActionName | Action | Promise<any> // we may be able to improve the `Promise<any>` if we could narrow this type `import('../app/Actions/BuddyAction')`
4747
paramNames: string[]
4848
middleware?: string | string[]
4949
statusCode?: StatusCode

0 commit comments

Comments
 (0)