Skip to content

Commit 53cd315

Browse files
committed
example: add state and context types to route api
1 parent 4dc9373 commit 53cd315

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

example/src/pages/api/level_one/[[...hello]].ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
import { KoaApi, withKoaApi } from 'nextjs-koa-api'
1+
import { Koa, KoaApi, Router, withKoaApi } from 'nextjs-koa-api'
22

3-
const api = new KoaApi({
3+
interface ApiState extends Koa.DefaultState {
4+
seesion: boolean
5+
}
6+
interface ApiContext extends Koa.Context {
7+
user: { name: string }
8+
}
9+
const api = new KoaApi<ApiState, ApiContext>({
410
router: {
511
prefix: '/api/level_one'
612
}
713
})
14+
api.context.user = { name: 'Lionel' }
815

916
api.use(async (ctx, next) => {
17+
// ctx.user.name
18+
// ctx.state.seesion
1019
try {
1120
await next()
1221
} catch (err: any) {
@@ -35,7 +44,7 @@ api.router
3544
})
3645

3746
// create nested router
38-
const subRouter = api.createNewRouter().get('/', (ctx) => {
47+
const subRouter = new Router<ApiState, ApiContext>().get('/', (ctx) => {
3948
ctx.body = `nested params: ${JSON.stringify(ctx.params)}`
4049
})
4150

0 commit comments

Comments
 (0)