mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 14:57:10 +00:00
Merge pull request #32671 from astashov/typesafe-compose
[@types/koa-compose]: Provide way for typesafe compose
This commit is contained in:
Vendored
+7
@@ -2,6 +2,13 @@
|
||||
// Project: https://github.com/koajs/compose
|
||||
// Definitions by: jKey Lu <https://github.com/jkeylu>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
import * as Koa from "koa";
|
||||
|
||||
declare function compose<T, U, V, W>(
|
||||
middleware: [Koa.Middleware<T, U>, Koa.Middleware<V, W>]
|
||||
): Koa.Middleware<T & V, U & W>;
|
||||
|
||||
declare function compose<T>(middleware: Array<compose.Middleware<T>>): compose.ComposedMiddleware<T>;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import compose = require('koa-compose');
|
||||
import * as Koa from "koa";
|
||||
|
||||
const fn1: compose.Middleware<any> = (context: any, next: () => Promise<void>): Promise<any> =>
|
||||
Promise
|
||||
@@ -11,3 +12,40 @@ const fn2: compose.Middleware<any> = (context: any, next: () => Promise<void>):
|
||||
.then(next);
|
||||
|
||||
const fn = compose([fn1, fn2]);
|
||||
|
||||
interface FooCtx {
|
||||
foo: string;
|
||||
}
|
||||
|
||||
const fooMiddleware: Koa.Middleware<FooCtx> = async (ctx, next) => {
|
||||
ctx.state.foo = "foo";
|
||||
await next();
|
||||
};
|
||||
|
||||
interface BarCtx {
|
||||
bar: string;
|
||||
}
|
||||
|
||||
const barMiddleware: Koa.Middleware<BarCtx> = async (ctx, next) => {
|
||||
ctx.state.bar = "bar";
|
||||
await next();
|
||||
};
|
||||
|
||||
interface WooCtx {
|
||||
woo: string;
|
||||
}
|
||||
|
||||
const wooMiddleware: Koa.Middleware<WooCtx> = async (ctx, next) => {
|
||||
ctx.state.woo = "woo";
|
||||
await next();
|
||||
};
|
||||
|
||||
new Koa<{}, {}>()
|
||||
.use(compose([compose([fooMiddleware, barMiddleware]), wooMiddleware]))
|
||||
.use(async (ctx, next) => {
|
||||
ctx.state.foo;
|
||||
ctx.state.bar;
|
||||
ctx.state.woo;
|
||||
ctx.body = "Something";
|
||||
await next();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user