Skip to content

Commit d4feb6a

Browse files
committed
remove create router method
Koa router is exported directly so it could be instantiated as is.
1 parent 53cd315 commit d4feb6a

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

lib/koa-api/src/__tests__/index.test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,5 @@ describe('Koa Api', () => {
7575

7676
expect(result.status).toBe(501)
7777
})
78-
79-
test('can create new router', () => {
80-
const api = new KoaApi()
81-
82-
const router = api.createNewRouter()
83-
expect(router).toBeInstanceOf(router.constructor)
84-
})
8578
})
8679
})

lib/koa-api/src/koa-api.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Router, { RouterOptions } from '@koa/router'
1+
import Router from '@koa/router'
22
import Koa from 'koa'
33
import type { NextApiRequest, NextApiResponse } from 'next'
44
import onFinished from 'on-finished'
@@ -11,7 +11,8 @@ declare module 'koa' {
1111
}
1212
}
1313

14-
export type { Router, RouterOptions }
14+
export { Router }
15+
export { Koa }
1516

1617
export type KoaOptions = {
1718
env?: string
@@ -29,8 +30,11 @@ export type KoaApiOptions = {
2930
routerAllowedMethods?: Router.RouterAllowedMethodsOptions
3031
}
3132

32-
export class KoaApi extends Koa {
33-
router: Router
33+
export class KoaApi<
34+
TState extends Koa.DefaultState = Koa.DefaultState,
35+
TContext extends Koa.DefaultContext = Koa.DefaultContext
36+
> extends Koa<TState, TContext> {
37+
router: Router<TState, TContext>
3438

3539
protected firstRun = true
3640

@@ -47,9 +51,9 @@ export class KoaApi extends Koa {
4751
this.router = new Router(this.options.router)
4852

4953
if (this.options.attachBody) {
50-
this.use((ctx, next) => {
54+
this.use(async (ctx, next) => {
5155
ctx.request.body = ctx.req.body
52-
next()
56+
await next()
5357
})
5458
}
5559
}
@@ -68,12 +72,8 @@ export class KoaApi extends Koa {
6872

6973
return p
7074
}
71-
72-
createNewRouter(opts?: RouterOptions) {
73-
return new Router(opts)
74-
}
7575
}
7676

77-
export function withKoaApi(koa: KoaApi) {
77+
export function withKoaApi(koa: KoaApi<any, any>) {
7878
return (req: NextApiRequest, res: NextApiResponse) => koa.run(req, res)
7979
}

0 commit comments

Comments
 (0)