import { APIRoute } from "./route" import { Service } from "./service" export interface RouteDecorator { (this: TThis, obj: any, name: string, descriptor: TypedPropertyDescriptor>): void } export interface RouteInterface { (this: TThis, context: TThis, ...args: any[]): void } export interface RouteDecoratorFactory { (option: TOption): RouteDecorator } export namespace RouteDecoratorFactory { export function from(guard: GuardFunction): RouteDecoratorFactory { return function (option: TOption) { return function (this: TThis, obj: TThis, name: string, descriptor: TypedPropertyDescriptor>) { let orig = descriptor.value descriptor.value = function (this: TThis, context: TThis) { guard.call(this, this, option, () => { orig.call(this, this) }) } } } } export const generator = from export function decorate(solve: SolveFunction): RouteDecorator { return function (this: TThis, obj: TThis, name: string, descriptor: TypedPropertyDescriptor>) { let orig = descriptor.value descriptor.value = function (this: TThis, context: TThis) { solve.call(this, this, () => { orig.call(this, this) }) } } } } export interface GuardFunction { (this: TThis, context: TThis, option: TOption, pass: () => void): void } export interface SolveFunction { (this: TThis, context: TThis, pass: () => void): void } export interface FakeService extends Service { services: TServices } export class RouteDecorators { constructor(public spec: TSpecs) { } private decs: { [K in keyof TSpecs]?: RouteDecoratorFactory } = {} implement(name: K, guard: GuardFunction> & TSpecs[K], TSpecs[K]["option"]>) { if (this.decs[name]) { throw new Error("Duplication implementation of decorator " + name) } this.decs[name] = RouteDecoratorFactory.from(guard) } decorate(name: K, option?: TSpecs[K]["option"]) { let factory = this.decs[name] if (!factory) { throw new Error(`Implementation of decorator ${name} not found`) } return factory(option) } } export abstract class RouteDecoratorSpec { params? services? option? return? } export interface RouteDecoratorSpecs { [name: string]: RouteDecoratorSpec }