Skip to content
This repository has been archived by the owner on May 14, 2020. It is now read-only.

Commit

Permalink
fix: remove the router's knowledge of its own context
Browse files Browse the repository at this point in the history
The router can't know about `RouterContext`, it needs to know its contents instead.
  • Loading branch information
thewilkybarkid authored Jan 10, 2020
1 parent 3ef1f0d commit 22bb9d1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
8 changes: 5 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ import namespaces from './namespaces';

export type AppState = DefaultState;

export type AppContext = RouterContext<AppState, DatasetContext<{
export type AppServiceContext = DatasetContext<{
articles: Articles;
}>>;
}>;

export type AppContext = RouterContext<AppState, AppServiceContext>;

export type AppMiddleware = Middleware<AppState, AppContext>;

export type App = Koa<AppState, AppContext>

export default (
articles: Articles,
router: Router<AppState, AppContext>,
router: Router<AppState, AppServiceContext>,
apiDocumentationPath: string,
dataFactory: ExtendedDataFactory,
): App => {
Expand Down
24 changes: 13 additions & 11 deletions src/middleware/routing.ts
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 }),
])
);
6 changes: 3 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Router from '@koa/router';
import { AppContext, AppState } from './app';
import { AppServiceContext, AppState } from './app';
import Routes from './routes';
import addArticle from './routes/add-article';
import apiDocumentation from './routes/api-documentation';
import articleList from './routes/article-list';
import entryPoint from './routes/entry-point';

export default (): Router<AppState, AppContext> => {
const router = new Router<AppState, AppContext>();
export default (): Router<AppState, AppServiceContext> => {
const router = new Router<AppState, AppServiceContext>();

router.get(Routes.ApiDocumentation, '/doc', apiDocumentation());
router.get(Routes.ArticleList, '/articles', articleList());
Expand Down

0 comments on commit 22bb9d1

Please sign in to comment.