2022-07-28 18:56:02 +08:00
|
|
|
|
import compose from 'koa-compose';
|
2022-09-13 23:27:19 +08:00
|
|
|
|
import { requireModule } from '@nocobase/utils';
|
2020-10-24 15:34:43 +08:00
|
|
|
|
import { ActionName } from './action';
|
|
|
|
|
import { HandlerType } from './resourcer';
|
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
|
export type MiddlewareType = string | string[] | HandlerType | HandlerType[] | MiddlewareOptions | MiddlewareOptions[];
|
2020-10-24 15:34:43 +08: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;
|
2022-09-13 23:27:19 +08:00
|
|
|
|
private middlewares: HandlerType[] = [];
|
2020-10-24 15:34:43 +08:00
|
|
|
|
|
|
|
|
|
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!');
|
|
|
|
|
}
|
2022-09-13 23:27:19 +08:00
|
|
|
|
return (ctx, next) => compose([handler, ...this.middlewares])(ctx, next);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use(middleware: HandlerType) {
|
|
|
|
|
this.middlewares.push(middleware);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disuse(middleware: HandlerType) {
|
|
|
|
|
this.middlewares.splice(this.middlewares.indexOf(middleware), 1);
|
2020-10-24 15:34:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 21:23:34 +08:00
|
|
|
|
return middlewares.map((middleware) => {
|
2020-10-24 15:34:43 +08:00
|
|
|
|
if (middleware instanceof Middleware) {
|
|
|
|
|
return middleware;
|
|
|
|
|
}
|
|
|
|
|
if (typeof middleware === 'object') {
|
|
|
|
|
return new Middleware(middleware);
|
|
|
|
|
}
|
|
|
|
|
if (typeof middleware === 'function') {
|
2021-03-28 13:34:51 +08:00
|
|
|
|
return new Middleware({ handler: middleware });
|
2020-10-24 15:34:43 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
|
export default Middleware;
|
2022-07-25 19:33:23 +08:00
|
|
|
|
|
2022-08-20 18:06:12 +08:00
|
|
|
|
export function branch(
|
|
|
|
|
map: {
|
|
|
|
|
[key: string]: HandlerType;
|
|
|
|
|
} = {},
|
|
|
|
|
reducer: (ctx) => string,
|
|
|
|
|
options: {
|
|
|
|
|
keyNotFound?(ctx, next): void;
|
|
|
|
|
handlerNotSet?(ctx, next): void;
|
2023-04-25 13:12:14 +08:00
|
|
|
|
} = {},
|
2022-08-20 18:06:12 +08:00
|
|
|
|
): 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);
|
|
|
|
|
};
|
|
|
|
|
}
|