chore: 优化access

This commit is contained in:
xiaojunnuo
2024-10-02 00:55:20 +08:00
parent b09acfb4dc
commit 551311d3a0
17 changed files with 73 additions and 56 deletions
+26 -1
View File
@@ -1,6 +1,8 @@
import { Registrable } from "../registry/index.js";
import { FormItemProps } from "../dt/index.js";
import { HttpClient, ILogger, utils } from "../utils";
import { HttpClient, ILogger, utils } from "../utils/index.js";
import _ from "lodash-es";
import { AccessRequestHandleReq } from "../core";
export type AccessInputDefine = FormItemProps & {
title: string;
@@ -26,3 +28,26 @@ export type AccessContext = {
logger: ILogger;
utils: typeof utils;
};
export abstract class BaseAccess implements IAccess {
ctx!: AccessContext;
async onRequest(req: AccessRequestHandleReq) {
if (!req.action) {
throw new Error("action is required");
}
let methodName = req.action;
if (!req.action.startsWith("on")) {
methodName = `on${_.upperFirst(req.action)}`;
}
// @ts-ignore
const method = this[methodName];
if (method) {
// @ts-ignore
return await this[methodName](req.data);
}
throw new Error(`action ${req.action} not found`);
}
}
@@ -3,7 +3,7 @@ import { AccessContext, AccessDefine, AccessInputDefine } from "./api.js";
import { Decorator } from "../decorator/index.js";
import _ from "lodash-es";
import { accessRegistry } from "./registry.js";
import { http, logger, utils } from "../utils";
import { http, logger, utils } from "../utils/index.js";
// 提供一个唯一 key
export const ACCESS_CLASS_KEY = "pipeline:access";
+1 -23
View File
@@ -1,5 +1,4 @@
import _ from "lodash-es";
import { HttpClient, ILogger, utils } from "../utils";
import { HttpClient, ILogger, utils } from "../utils/index.js";
export type PluginRequestHandleReq<T = any> = {
typeName: string;
@@ -20,24 +19,3 @@ export type AccessRequestHandleContext = {
};
export type AccessRequestHandleReq<T = any> = PluginRequestHandleReq<AccessRequestHandleReqInput<T>>;
export class AccessRequestHandler<T = any> {
async onRequest(req: AccessRequestHandleReq<T>, ctx: AccessRequestHandleContext) {
if (!req.action) {
throw new Error("action is required");
}
let methodName = req.action;
if (!req.action.startsWith("on")) {
methodName = `on${_.upperFirst(req.action)}`;
}
// @ts-ignore
const method = this[methodName];
if (method) {
// @ts-ignore
return await this[methodName](req.data, ctx);
}
throw new Error(`action ${req.action} not found`);
}
}
+1 -1
View File
@@ -6,7 +6,7 @@ import { IAccessService } from "../access/index.js";
import { IEmailService } from "../service/index.js";
import { IContext, PluginRequestHandleReq, RunnableCollection } from "../core/index.js";
import { ILogger, logger, utils } from "../utils/index.js";
import { HttpClient } from "../utils/util.request";
import { HttpClient } from "../utils/util.request.js";
import dayjs from "dayjs";
import _ from "lodash-es";
export type UserInfo = {