Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/composed.ts
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 3 additions & 2 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
47 changes: 1 addition & 46 deletions src/method.ts
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 6 additions & 5 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
22 changes: 10 additions & 12 deletions src/tryMethod.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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)
Expand All @@ -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
})
}
}