2022-07-28 10:56:02 +00:00
|
|
|
|
import compose from 'koa-compose';
|
2020-10-24 07:34:43 +00:00
|
|
|
|
import { ActionName } from './action';
|
|
|
|
|
import { HandlerType } from './resourcer';
|
2022-07-28 10:56:02 +00:00
|
|
|
|
import { requireModule } from './utils';
|
2020-10-24 07:34:43 +00:00
|
|
|
|
|
2021-12-06 13:23:34 +00:00
|
|
|
|
export type MiddlewareType = string | string[] | HandlerType | HandlerType[] | MiddlewareOptions | MiddlewareOptions[];
|
2020-10-24 07:34:43 +00:00
|
|
|
|
|
|
|
|
|
export interface MiddlewareOptions {
|
|
|
|
|
/**
|
|
|
|
|
* actions 白名单,默认有 list、get、create、update、delete
|
|
|
|
|
*/
|
|
|
|
|
only?: Array<ActionName>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* actions 黑名单,默认有 list、get、create、update、delete
|
|
|
|
|
*/
|
|
|
|
|
except?: Array<ActionName>;
|
|
|
|
|
|
|
|
|
|
handler?: HandlerType | Function;
|
|
|
|
|
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Middleware {
|
|
|
|
|
protected options: MiddlewareOptions;
|
|
|
|
|
|
|
|
|
|
constructor(options: MiddlewareOptions | Function) {
|
|
|
|
|
options = requireModule(options);
|
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
|
this.options = { handler: options };
|
|
|
|
|
} else {
|
|
|
|
|
this.options = options;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getHandler() {
|
|
|
|
|
const handler = requireModule(this.options.handler);
|
|
|
|
|
if (typeof handler !== 'function') {
|
|
|
|
|
throw new Error('Handler must be a function!');
|
|
|
|
|
}
|
|
|
|
|
return handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canAccess(name: ActionName) {
|
|
|
|
|
const { only = [], except = [] } = this.options;
|
|
|
|
|
if (only.length > 0) {
|
|
|
|
|
return only.includes(name);
|
|
|
|
|
}
|
|
|
|
|
if (except.length > 0) {
|
|
|
|
|
return !except.includes(name);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static toInstanceArray(middlewares: any): Middleware[] {
|
|
|
|
|
if (!middlewares) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
if (!Array.isArray(middlewares)) {
|
|
|
|
|
middlewares = [middlewares];
|
|
|
|
|
}
|
2021-12-06 13:23:34 +00:00
|
|
|
|
return middlewares.map((middleware) => {
|
2020-10-24 07:34:43 +00:00
|
|
|
|
if (middleware instanceof Middleware) {
|
|
|
|
|
return middleware;
|
|
|
|
|
}
|
|
|
|
|
if (typeof middleware === 'object') {
|
|
|
|
|
return new Middleware(middleware);
|
|
|
|
|
}
|
|
|
|
|
if (typeof middleware === 'function') {
|
2021-03-28 05:34:51 +00:00
|
|
|
|
return new Middleware({ handler: middleware });
|
2020-10-24 07:34:43 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 13:23:34 +00:00
|
|
|
|
export default Middleware;
|
2022-07-25 11:33:23 +00:00
|
|
|
|
|
|
|
|
|
export class MiddlewareManager {
|
|
|
|
|
protected middlewares: HandlerType[] = [];
|
|
|
|
|
|
|
|
|
|
compose() {
|
|
|
|
|
return (ctx, next) => compose(this.middlewares)(ctx, next);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use(middleware: HandlerType) {
|
|
|
|
|
this.middlewares.push(middleware);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unuse(middleware: HandlerType) {
|
|
|
|
|
this.middlewares.splice(this.middlewares.indexOf(middleware), 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-20 10:06:12 +00:00
|
|
|
|
|
|
|
|
|
export function branch(
|
|
|
|
|
map: {
|
|
|
|
|
[key: string]: HandlerType;
|
|
|
|
|
} = {},
|
|
|
|
|
reducer: (ctx) => string,
|
|
|
|
|
options: {
|
|
|
|
|
keyNotFound?(ctx, next): void;
|
|
|
|
|
handlerNotSet?(ctx, next): void;
|
|
|
|
|
} = {}
|
|
|
|
|
): HandlerType {
|
|
|
|
|
return (ctx, next) => {
|
|
|
|
|
const key = reducer(ctx);
|
|
|
|
|
|
|
|
|
|
if (!key) {
|
|
|
|
|
return options.keyNotFound ? options.keyNotFound(ctx, next) : ctx.throw(404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handler = map[key];
|
|
|
|
|
if (!handler) {
|
|
|
|
|
return options.handlerNotSet ? options.handlerNotSet(ctx, next) : ctx.throw(404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handler(ctx, next);
|
|
|
|
|
};
|
|
|
|
|
}
|