add use method

This commit is contained in:
Aleksandr 2024-07-06 21:39:28 +03:00
parent 59dafba982
commit bb00e1b207

View file

@ -2,6 +2,7 @@ import { ControlFlow, Break, Continue } from "./control_flow"
// Typescript lacks shadowing, so code sometimes can be stupid to avoid it
type Mapper<B, C, Bn, Cn> = (inp: Handler<B, C>) => Handler<Bn, Cn>
export type HandlerFn<B, C> = (req: C, next: Endpoint<B, C>) => Promise<ControlFlow<B, C>>
export type InputHandler<B, C> = HandlerFn<B, C> | Handler<B, C>
@ -35,6 +36,10 @@ export class Handler<B, C> {
this.fn = fn
}
use<Bn, Cn>(f: Mapper<B, C, Bn, Cn>): Handler<Bn, Cn> {
return f(this)
}
then(next: InputHandler<B, C>): Handler<B, C> {
return new Handler(then(this, next))
}