2020-10-24 15:34:43 +08:00
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
import Action, { ActionName, ActionType } from './action';
|
2022-06-14 15:01:53 +08:00
|
|
|
|
import Middleware, { MiddlewareType } from './middleware';
|
|
|
|
|
import Resourcer from './resourcer';
|
2020-10-24 15:34:43 +08:00
|
|
|
|
|
|
|
|
|
export type ResourceType = 'single' | 'hasOne' | 'hasMany' | 'belongsTo' | 'belongsToMany';
|
|
|
|
|
|
|
|
|
|
export interface ResourceOptions {
|
|
|
|
|
/**
|
|
|
|
|
* 资源名称
|
|
|
|
|
*/
|
|
|
|
|
name: string;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 资源类型,默认为 single
|
|
|
|
|
*/
|
|
|
|
|
type?: ResourceType;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 资源的行为
|
|
|
|
|
*/
|
|
|
|
|
actions?: {
|
|
|
|
|
[key: string]: ActionType;
|
2021-12-06 21:23:34 +08:00
|
|
|
|
};
|
2020-10-24 15:34:43 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* actions 白名单,默认有 list、get、create、update、delete
|
|
|
|
|
*/
|
|
|
|
|
only?: Array<ActionName>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* actions 黑名单,默认有 list、get、create、update、delete
|
|
|
|
|
*/
|
|
|
|
|
except?: Array<ActionName>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 中间件
|
|
|
|
|
*/
|
|
|
|
|
middleware?: MiddlewareType;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 中间件
|
|
|
|
|
*/
|
|
|
|
|
middlewares?: MiddlewareType;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 额外的一些参数
|
|
|
|
|
*/
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Resource {
|
|
|
|
|
public readonly resourcer: Resourcer;
|
|
|
|
|
|
|
|
|
|
public readonly middlewares: Middleware[];
|
|
|
|
|
|
|
|
|
|
public readonly actions = new Map<ActionName, Action>();
|
|
|
|
|
|
|
|
|
|
public readonly options: ResourceOptions;
|
|
|
|
|
|
|
|
|
|
public readonly except: Array<ActionName>;
|
|
|
|
|
|
|
|
|
|
constructor(options: ResourceOptions, resourcer: Resourcer) {
|
2022-10-12 15:22:00 +08:00
|
|
|
|
const { middleware, middlewares, actions = {}, only = [], except = [] } = options;
|
2020-10-24 15:34:43 +08:00
|
|
|
|
this.options = options;
|
|
|
|
|
this.resourcer = resourcer;
|
|
|
|
|
this.middlewares = Middleware.toInstanceArray(middleware || middlewares);
|
|
|
|
|
let excludes = [];
|
|
|
|
|
for (const [name, handler] of resourcer.getRegisteredHandlers()) {
|
|
|
|
|
if (!actions[name as string]) {
|
|
|
|
|
actions[name as string] = handler;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (except.length > 0) {
|
|
|
|
|
excludes = except;
|
|
|
|
|
} else if (only.length > 0) {
|
2021-12-06 21:23:34 +08:00
|
|
|
|
excludes = Object.keys(actions).filter((name) => !only.includes(name));
|
2020-10-24 15:34:43 +08:00
|
|
|
|
}
|
|
|
|
|
this.except = excludes;
|
|
|
|
|
this.actions = Action.toInstanceMap(_.omit(actions, excludes), this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getName() {
|
|
|
|
|
return this.options.name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getExcept() {
|
|
|
|
|
return this.except;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAction(action: ActionName) {
|
|
|
|
|
if (this.except.includes(action)) {
|
|
|
|
|
throw new Error(`${action} action is not allowed`);
|
|
|
|
|
}
|
|
|
|
|
if (!this.actions.has(action)) {
|
|
|
|
|
throw new Error(`${action} action does not exist`);
|
|
|
|
|
}
|
|
|
|
|
return this.actions.get(action);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
|
export default Resource;
|