[@types/koa-router]: Parameterize Router

After #31704 it would be useful to specify what is `Context.State` and
`Context` inside the `Router`'s handlers. For that, we could provide
a way to parameterize `Router` (also fallbacking to default `any` and
`{}` in case the generic type params are omitted).

So it would work like that:

```ts
type MyState = {foo: string}
type MyContext = {bar: string}

const app = new Koa<{}, {}>();
const router = new Router<MyState, MyContext>();
router.get('/', (ctx, next) => {
    // ctx here is Koa.ParameterizedContext<MyState, MyContext & IRouterContext<MyState, MyContext>>
    console.log(ctx.state.foo);
    ctx.body = 'Hello World!';
});
app.use<MyState, MyContext>(async (ctx, next) => {
        ctx.state.foo = "foo"
        ctx.bar = "bar";
        await next();
    })
    .use(router.routes())
    .use(router.allowedMethods())
    .use(async (ctx, next) => {
        // ctx here is Koa.ParameterizedContext<MyState, MyContext & IRouterContext<MyState, MyContext>>
        console.log(ctx.router);
        console.log(ctx.bar);
        await next();
    })
    .listen(3000);
```

But if we don't parameterize `Router`, it will work as before:

```ts
const app = new Koa();
const router = new Router();
router.get('/', (ctx, next) => {
    // ctx here is Koa.ParameterizedContext<any, IRouterContext>
    ctx.body = 'Hello World!';
});
app.use(async (ctx, next) => {
        ctx.state.foo = "foo"
        ctx.bar = "bar";
        await next();
    })
    .use(router.routes())
    .use(router.allowedMethods())
    .listen(3000);
```

What do you think?
This commit is contained in:
Anton Astashov
2019-01-06 16:11:24 -06:00
parent 3ad75fac4d
commit 4de11f4fb3
2 changed files with 193 additions and 43 deletions
+126 -37
View File
@@ -43,7 +43,7 @@ declare namespace Router {
strict?: boolean;
}
export interface IRouterContext extends Koa.Context {
export interface IRouterContext<StateT = any, CustomT = {}> {
/**
* url params
*/
@@ -51,11 +51,14 @@ declare namespace Router {
/**
* the router instance
*/
router: Router;
router: Router<StateT, CustomT>;
}
export interface IMiddleware {
(ctx: Koa.ParameterizedContext<{}, IRouterContext>, next: () => Promise<any>): any;
export interface IMiddleware<StateT = any, CustomT = {}> {
(
ctx: Koa.ParameterizedContext<StateT, IRouterContext<StateT, CustomT> & CustomT>,
next: () => Promise<void>
): void;
}
export interface IParamMiddleware {
@@ -148,7 +151,7 @@ declare namespace Router {
}
}
declare class Router {
declare class Router<StateT = any, CustomT = {}> {
params: Object;
stack: Array<Router.Layer>;
@@ -164,109 +167,195 @@ declare class Router {
* sequentially, requests start at the first middleware and work their way
* "down" the middleware stack.
*/
use(...middleware: Array<Router.IMiddleware>): Router;
use(path: string | string[] | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
use(...middleware: Array<Router.IMiddleware<StateT, CustomT>>): Router<StateT, CustomT>;
use(
path: string | string[] | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* HTTP get method
*/
get(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
get(path: string | RegExp | (string | RegExp)[], ...middleware: Array<Router.IMiddleware>): Router;
get(
name: string,
path: string | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
get(
path: string | RegExp | (string | RegExp)[],
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* HTTP post method
*/
post(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
post(path: string | RegExp | (string | RegExp)[], ...middleware: Array<Router.IMiddleware>): Router;
post(
name: string,
path: string | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
post(
path: string | RegExp | (string | RegExp)[],
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* HTTP put method
*/
put(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
put(path: string | RegExp | (string | RegExp)[], ...middleware: Array<Router.IMiddleware>): Router;
put(
name: string,
path: string | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
put(
path: string | RegExp | (string | RegExp)[],
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* HTTP link method
*/
link(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
link(path: string | RegExp | (string | RegExp)[], ...middleware: Array<Router.IMiddleware>): Router;
link(
name: string,
path: string | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
link(
path: string | RegExp | (string | RegExp)[],
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* HTTP unlink method
*/
unlink(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
unlink(path: string | RegExp | (string | RegExp)[], ...middleware: Array<Router.IMiddleware>): Router;
unlink(
name: string,
path: string | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
unlink(
path: string | RegExp | (string | RegExp)[],
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* HTTP delete method
*/
delete(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
delete(path: string | RegExp | (string | RegExp)[], ...middleware: Array<Router.IMiddleware>): Router;
delete(
name: string,
path: string | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
delete(
path: string | RegExp | (string | RegExp)[],
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* Alias for `router.delete()` because delete is a reserved word
*/
del(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
del(path: string | RegExp | (string | RegExp)[], ...middleware: Array<Router.IMiddleware>): Router;
del(
name: string,
path: string | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
del(
path: string | RegExp | (string | RegExp)[],
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* HTTP head method
*/
head(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
head(path: string | RegExp | (string | RegExp)[], ...middleware: Array<Router.IMiddleware>): Router;
head(
name: string,
path: string | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
head(
path: string | RegExp | (string | RegExp)[],
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* HTTP options method
*/
options(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
options(path: string | RegExp | (string | RegExp)[], ...middleware: Array<Router.IMiddleware>): Router;
options(
name: string,
path: string | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
options(
path: string | RegExp | (string | RegExp)[],
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* HTTP path method
*/
patch(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
patch(path: string | RegExp | (string | RegExp)[], ...middleware: Array<Router.IMiddleware>): Router;
patch(
name: string,
path: string | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
patch(
path: string | RegExp | (string | RegExp)[],
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* Register route with all methods.
*/
all(name: string, path: string | RegExp, ...middleware: Array<Router.IMiddleware>): Router;
all(path: string | RegExp | (string | RegExp)[], ...middleware: Array<Router.IMiddleware>): Router;
all(
name: string,
path: string | RegExp,
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
all(
path: string | RegExp | (string | RegExp)[],
...middleware: Array<Router.IMiddleware<StateT, CustomT>>
): Router<StateT, CustomT>;
/**
* Set the path prefix for a Router instance that was already initialized.
*/
prefix(prefix: string): Router;
prefix(prefix: string): Router<StateT, CustomT>;
/**
* Returns router middleware which dispatches a route matching the request.
*/
routes(): Koa.Middleware;
routes(): Koa.Middleware<StateT, CustomT & Router.IRouterContext<StateT, CustomT>>;
/**
* Returns router middleware which dispatches a route matching the request.
*/
middleware(): Koa.Middleware;
middleware(): Koa.Middleware<StateT, CustomT & Router.IRouterContext<StateT, CustomT>>;
/**
* Returns separate middleware for responding to `OPTIONS` requests with
* an `Allow` header containing the allowed methods, as well as responding
* with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.
*/
allowedMethods(options?: Router.IRouterAllowedMethodsOptions): Koa.Middleware;
allowedMethods(
options?: Router.IRouterAllowedMethodsOptions
): Koa.Middleware<StateT, CustomT & Router.IRouterContext<StateT, CustomT>>;
/**
* Redirect `source` to `destination` URL with optional 30x status `code`.
*
* Both `source` and `destination` can be route names.
*/
redirect(source: string, destination: string, code?: number): Router;
redirect(source: string, destination: string, code?: number): Router<StateT, CustomT>;
/**
* Create and register a route.
*/
register(path: string | RegExp, methods: string[], middleware: Router.IMiddleware, opts?: Object): Router.Layer;
register(
path: string | RegExp,
methods: string[],
middleware: Router.IMiddleware<StateT, CustomT>,
opts?: Object
): Router.Layer;
/**
* Lookup route with given `name`.
@@ -304,7 +393,7 @@ declare class Router {
/**
* Run middleware for named route parameters. Useful for auto-loading or validation.
*/
param(param: string, middleware: Router.IParamMiddleware): Router;
param(param: string, middleware: Router.IParamMiddleware): Router<StateT, CustomT>;
/**
* Generate URL from url pattern and given `params`.
+67 -6
View File
@@ -1,9 +1,15 @@
import Koa = require("koa");
import { Context } from "koa";
import compose = require("koa-compose");
import etag = require("koa-etag");
import Router = require("koa-router");
type MyState = {foo: string}
type MyContext = {bar: string}
const app = new Koa<{}, {}>();
const router = new Router({
const router = new Router<MyState, MyContext>({
prefix: "/users"
});
@@ -31,9 +37,10 @@ router
}
})
.post('/users', function (ctx, next) {
// ...
ctx.state.foo = 'foo';
})
.put('/users/:id', function (ctx, next) {
ctx.bar = 'bar';
ctx.body = ctx.params.id;
})
.del('/users/:id', function () {
@@ -49,11 +56,65 @@ const match = router.match('/users/:id', 'GET');
let layer: Router.Layer
let layerOptions: Router.ILayerOptions
const mw: Router.IMiddleware = (ctx: Router.IRouterContext, next: () => Promise<any>) => {
const mw: Router.IMiddleware = (ctx: Koa.ParameterizedContext<any, Router.IRouterContext>, next: () => Promise<any>) => {
ctx.body = "Ok";
};
app.use(router.routes());
app.use(router.allowedMethods());
app.use(async (ctx: Koa.ParameterizedContext<MyState, MyContext>, next) => {
ctx.state.foo = 'foo';
await next();
})
.use(router.routes())
.use(router.allowedMethods())
.use(async (ctx, next) => {
ctx.state.foo = '3';
await next();
})
.listen(3000);
app.listen(3000);
const router3 = new Router();
router3.get('/', (ctx) => {
ctx.foo = "bar";
console.log(ctx.router.params);
ctx.body = "Hello World!";
});
new Koa()
.use(async (ctx, next) => next())
.use(router3.routes())
.use(router.allowedMethods())
.listen(3001);
// It's from https://github.com/DefinitelyTyped/DefinitelyTyped/pull/31704#issuecomment-451075919,
// to make sure we don't break it again
declare module "koa" {
interface Context {
name: string;
}
}
const app2 = new Koa();
const conditional = async (ctx: Context, next: any) => {
await next();
if (ctx.fresh) {
ctx.status = 304;
ctx.body = null;
}
};
const router2 = compose([conditional, etag()]);
app2.use(router2);
app2.use(async (ctx: Context, next: any) => {
ctx.name = "hello world";
await next();
});
app2.use((ctx: Context, next: any) => {
console.log(ctx.name);
ctx.body = ctx.name;
});
app2.listen(8000);