import * as path from "path" ; import * as Koa from "koa" ; import { paths } from "../path" ; import { getOutput } from "../utils" ; export interface Middleware$ { config : any ; pipe : Koa . Middleware ; } export interface MiddlewareFactory$ { new ( ) : Middleware$ ; } export default class Middleware implements Middleware$ { public config : any = { } ; constructor ( options ? ) { this . config = options || { } ; } param ctx param next async pipe ( ctx , next ) { } } param middlewareName param cwd export function resolveMiddleware ( middlewareName ) { if ( typeof middlewareName === "function" ) { return class KoaMiddleware extends Middleware { async pipe ( ctx , next ) { return middlewareName ( ctx , next ) ; } } ; } let MiddlewareFactory ; try { const localMiddlewarePath = path . join ( paths . middleware , middlewareName + ".middleware" ) ; MiddlewareFactory = getOutput ( require ( localMiddlewarePath ) ) ; } catch ( err ) { try { MiddlewareFactory = getOutput ( require ( middlewareName ) ) ; } catch ( err ) { throw new Error ( `Can not found the middleware "${ middlewareName }"` ) ; } } return MiddlewareFactory ; } const defaultMiddleware = new Middleware ( ) ; param m export function isValidMiddleware ( m ) { return m instanceof Middleware && m . pipe !== defaultMiddleware . pipe ; }