Files
certd/packages/core/pipeline/src/access/api.ts
T

68 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-07-15 00:30:33 +08:00
import { Registrable } from "../registry/index.js";
import { FormItemProps } from "../dt/index.js";
2024-11-04 15:14:56 +08:00
import { HttpClient, ILogger, utils } from "@certd/basic";
2024-11-08 23:43:19 +08:00
import * as _ from "lodash-es";
2024-11-22 17:12:39 +08:00
import { PluginRequestHandleReq } from "../plugin/index.js";
export type AccessRequestHandleReqInput<T = any> = {
id?: number;
title?: string;
access: T;
};
export type AccessRequestHandleReq<T = any> = PluginRequestHandleReq<AccessRequestHandleReqInput<T>>;
2022-10-26 09:02:47 +08:00
2022-12-27 12:32:09 +08:00
export type AccessInputDefine = FormItemProps & {
2022-10-31 21:27:32 +08:00
title: string;
required?: boolean;
2024-08-27 13:46:19 +08:00
encrypt?: boolean;
2022-10-31 21:27:32 +08:00
};
2022-10-26 09:02:47 +08:00
export type AccessDefine = Registrable & {
2024-11-30 01:57:09 +08:00
icon?: string;
2023-01-11 20:39:48 +08:00
input?: {
2022-12-27 12:32:09 +08:00
[key: string]: AccessInputDefine;
2022-10-26 09:02:47 +08:00
};
};
2022-11-07 23:31:20 +08:00
export interface IAccessService {
2024-09-24 13:50:06 +08:00
getById<T = any>(id: any): Promise<T>;
getCommonById<T = any>(id: any): Promise<T>;
2022-11-07 23:31:20 +08:00
}
2023-01-07 23:22:02 +08:00
2024-10-01 23:52:44 +08:00
export interface IAccess {
ctx: AccessContext;
[key: string]: any;
}
export type AccessContext = {
http: HttpClient;
logger: ILogger;
utils: typeof utils;
};
2024-10-02 00:55:20 +08:00
export abstract class BaseAccess implements IAccess {
ctx!: AccessContext;
2024-11-25 11:35:16 +08:00
setCtx(ctx: AccessContext) {
this.ctx = ctx;
}
2024-10-02 00:55:20 +08:00
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`);
}
}