This repository has been archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove the router's knowledge of its own context
The router can't know about `RouterContext`, it needs to know its contents instead.
- Loading branch information
1 parent
3ef1f0d
commit 22bb9d1
Showing
3 changed files
with
21 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
import Router, { RouterContext } from '@koa/router'; | ||
import Router, { Middleware, RouterParamContext } from '@koa/router'; | ||
import createHttpError from 'http-errors'; | ||
import { Middleware, Next, DefaultState } from 'koa'; | ||
import { DefaultContextExtends, DefaultStateExtends, Next } from 'koa'; | ||
import compose from 'koa-compose'; | ||
|
||
const notFound = (): Middleware<DefaultState, RouterContext> => ( | ||
async ({ _matchedRoute }: RouterContext, next: Next): Promise<void> => { | ||
await next(); | ||
const notFound = <State extends DefaultStateExtends, Context extends DefaultContextExtends> | ||
(): Middleware<State, Context> => ( | ||
async ({ _matchedRoute }: RouterParamContext<State, Context>, next: Next): Promise<void> => { | ||
await next(); | ||
|
||
if (typeof _matchedRoute === 'undefined') { | ||
throw new createHttpError.NotFound(); | ||
if (typeof _matchedRoute === 'undefined') { | ||
throw new createHttpError.NotFound(); | ||
} | ||
} | ||
} | ||
); | ||
); | ||
|
||
export default (router: Router): Middleware<DefaultState, RouterContext> => ( | ||
export default <State extends DefaultStateExtends, Context extends DefaultContextExtends> | ||
(router: Router<State, Context>): Middleware<State, Context> => ( | ||
compose([ | ||
router.routes(), | ||
notFound(), | ||
notFound<State, Context>(), | ||
router.allowedMethods({ throw: true }), | ||
]) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters