diff --git a/src/composed.ts b/src/composed.ts index 24caaf6..699a156 100644 --- a/src/composed.ts +++ b/src/composed.ts @@ -1,4 +1,4 @@ import {IRouter} from "express-serve-static-core" import {HandleFn} from "./types"; -export type Composer = (handler: HandleFn, router?: IRouter) => HandleFn +export type Composer = (handler: HandleFn) => HandleFn; diff --git a/src/handler.ts b/src/handler.ts index 299b918..b969789 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -1,9 +1,10 @@ -import {HandleFn} from "./types"; +import { MethodName } from "./method"; +import { HandleFn } from "./types"; export interface HandlerParams { path?: string; - handle: HandleFn; + method: MethodName; } export interface Handler extends HandlerParams { diff --git a/src/method.ts b/src/method.ts index 5ec0181..2accfde 100644 --- a/src/method.ts +++ b/src/method.ts @@ -1,46 +1 @@ -import {Component} from "./component"; -import {Composer} from "./composed" -import {Element} from "./element" -import {Middleware} from "./middleware" -import {HandleFn} from "./types" - -declare module "express-serve-static-core" { - interface IRouter { - ecmethod: string - } -} - -export type MethodName = "GET" | "POST" | "DELETE" | "PUT" | "OPTIONS" - -export interface MethodParams { - method: MethodName - child: Element; -} - -export interface Method extends MethodParams, Component { - -} - -export class Method implements Component { - constructor(params: MethodParams) { - Object.assign(this, params) - } - - checkMethod: Composer = (handler: HandleFn, router) => { - router.ecmethod = this.method - return async (req, res, next) => { - if (req.method === this.method) { - handler(req, res, next) - } else { - next() - } - } - } - - render(): any { - return new Middleware({ - child: this.child, - handle: this.checkMethod - }) - } -} +export type MethodName = "get" | "post" | "put" | "delete" diff --git a/src/render.ts b/src/render.ts index a0d2917..c1ecafb 100644 --- a/src/render.ts +++ b/src/render.ts @@ -12,11 +12,12 @@ export type RouterFactory = (...args: any[]) => IRouter export function withRouterFactory(factory: RouterFactory) { return function render(instance: Element, router: IRouter, mw: Composer | null = null, context?: any) { if (instance instanceof Handler) { - const handler = mw != null ? mw(instance.handle, router) : instance.handle + const handler = mw != null ? mw(instance.handle) : instance.handle; if (instance.path) { - router.use(instance.path, handler) - } else { - router.use(handler) + router[instance.method](instance.path, handler); + } + else { + router[instance.method]("", handler); } } else if (isRouter(instance)) { const childRouter = factory() @@ -29,7 +30,7 @@ export function withRouterFactory(factory: RouterFactory) { router.use(childRouter) } } else if (instance instanceof Middleware) { - const composer = mw != null ? (handler: HandleFn) => mw(instance.handle(handler), router) : instance.handle + const composer = mw != null ? (handler: HandleFn) => mw(instance.handle(handler)) : instance.handle render(instance.child, router, composer, context) } else if (isComponent(instance)) { instance.context = context diff --git a/src/tryMethod.ts b/src/tryMethod.ts index 1971b1d..d344abc 100644 --- a/src/tryMethod.ts +++ b/src/tryMethod.ts @@ -1,7 +1,7 @@ import {Element} from "./element" import {ErrorRequestHandler} from "express"; import {Handler} from "./handler" -import {Method, MethodName} from "./method" +import {MethodName} from "./method" import {TryJson} from "./tryJson" import {HandleFn} from "./types" @@ -19,14 +19,15 @@ export class TryMethod { method, child: new Handler({ handle, - path + path, + method }) }) - static post = (path: string, handle: HandleFn) => TryMethod.handler("POST", path, handle) - static get = (path: string, handle: HandleFn) => TryMethod.handler("GET", path, handle) - static delete = (path: string, handle: HandleFn) => TryMethod.handler("DELETE", path, handle) - static put = (path: string, handle: HandleFn) => TryMethod.handler("PUT", path, handle) + static post = (path: string, handle: HandleFn) => TryMethod.handler("post", path, handle) + static get = (path: string, handle: HandleFn) => TryMethod.handler("get", path, handle) + static delete = (path: string, handle: HandleFn) => TryMethod.handler("delete", path, handle) + static put = (path: string, handle: HandleFn) => TryMethod.handler("put", path, handle) constructor(params: TryMethodParams) { Object.assign(this, params) @@ -37,12 +38,9 @@ export class TryMethod { } render() { - return new Method({ - child: new TryJson({ - child: this.child, - onCatch: this.context.onCatch - }), - method: this.method + return new TryJson({ + child: this.child, + onCatch: this.context.onCatch }) } }