This commit is contained in:
xiaojunnuo
2024-10-01 23:52:44 +08:00
parent f8f3e8b43f
commit b09acfb4dc
9 changed files with 47 additions and 26 deletions
+11 -2
View File
@@ -1,5 +1,6 @@
import { Registrable } from "../registry/index.js";
import { FormItemProps } from "../dt/index.js";
import { HttpClient, ILogger, utils } from "../utils";
export type AccessInputDefine = FormItemProps & {
title: string;
@@ -15,5 +16,13 @@ export interface IAccessService {
getById<T = any>(id: any): Promise<T>;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IAccess {}
export interface IAccess {
ctx: AccessContext;
[key: string]: any;
}
export type AccessContext = {
http: HttpClient;
logger: ILogger;
utils: typeof utils;
};
+23 -1
View File
@@ -1,8 +1,9 @@
// src/decorator/memoryCache.decorator.ts
import { AccessDefine, AccessInputDefine } from "./api.js";
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";
// 提供一个唯一 key
export const ACCESS_CLASS_KEY = "pipeline:access";
@@ -37,3 +38,24 @@ export function AccessInput(input?: AccessInputDefine): PropertyDecorator {
Reflect.defineMetadata(ACCESS_INPUT_KEY, input, target, propertyKey);
};
}
export function newAccess(type: string, input: any, ctx?: AccessContext) {
const register = accessRegistry.get(type);
if (register == null) {
throw new Error(`access ${type} not found`);
}
// @ts-ignore
const access = new register.target();
for (const key in input) {
access[key] = input[key];
}
if (!ctx) {
ctx = {
http,
logger,
utils,
};
}
access.ctx = ctx;
return access;
}
+2 -2
View File
@@ -13,14 +13,14 @@ export type AccessRequestHandleReqInput<T = any> = {
title?: string;
access: T;
};
export type AccessRequestHandleReq<T = any> = PluginRequestHandleReq<AccessRequestHandleReqInput<T>>;
export type AccessRequestHandleContext = {
http: HttpClient;
logger: ILogger;
utils: typeof utils;
};
export type AccessRequestHandleReq<T = any> = PluginRequestHandleReq<AccessRequestHandleReqInput<T>>;
export class AccessRequestHandler<T = any> {
async onRequest(req: AccessRequestHandleReq<T>, ctx: AccessRequestHandleContext) {
if (!req.action) {