@anyhowstep/ts-route-server
Version:
38 lines (31 loc) • 1.32 kB
text/typescript
import {Definition} from "@anyhowstep/ts-route-shared";
import {Handler, VoidHandler} from "./Handler";
import {HandlerMiddleware} from "./HandlerMiddleware";
export type HandlerArray<P, B, Q, D, M> = (Handler<P, B, Q, D, M, any, any>|VoidHandler<P, B, Q, D, M, any>)[];
export class Route<P, B, Q, D, M, L={_dummy:undefined}> {
private definition : Definition<P, B, Q, D, M>;
private handlerArr : HandlerArray<P, B, Q, D, M>;
public constructor (
definition : Definition<P, B, Q, D, M>,
handlerArr : HandlerArray<P, B, Q, D, M> = []
) {
this.definition = definition;
this.handlerArr = handlerArr;
}
public add<N extends {}> (handler : Handler<P, B, Q, D, M, L, N>) : Route<P, B, Q, D, M, L & N> {
const newHandlerArr = this.handlerArr.slice();
newHandlerArr.push(HandlerMiddleware.Create(handler));
return new Route(this.definition, newHandlerArr);
}
public addVoid (handler : VoidHandler<P, B, Q, D, M, L>) : Route<P, B, Q, D, M, L> {
const newHandlerArr = this.handlerArr.slice();
newHandlerArr.push(handler);
return new Route(this.definition, newHandlerArr);
}
public getDefinition () {
return this.definition;
}
public getHandlerArr () {
return this.handlerArr;
}
}