mirror of
https://github.com/certd/certd.git
synced 2026-07-13 00:37:36 +08:00
93 lines
2.5 KiB
TypeScript
93 lines
2.5 KiB
TypeScript
import { Registrable } from "../registry/index.js";
|
|
import { FormItemProps } from "../dt/index.js";
|
|
import { HttpClient, ILogger, utils } from "@certd/basic";
|
|
import * as _ from "lodash-es";
|
|
import { PluginRequestHandleReq } from "../plugin/index.js";
|
|
import { IRuntimeDepsService, IServiceGetter } from "../service/index.js";
|
|
|
|
// export type AccessRequestHandleReqInput<T = any> = {
|
|
// id?: number;
|
|
// title?: string;
|
|
// access: T;
|
|
// };
|
|
|
|
export type AccessRequestHandleReq<T = any> = PluginRequestHandleReq<T>;
|
|
|
|
export type AccessInputDefine = FormItemProps & {
|
|
title: string;
|
|
required?: boolean;
|
|
encrypt?: boolean;
|
|
};
|
|
export type AccessDefine = Registrable & {
|
|
icon?: string;
|
|
subtype?: string;
|
|
dependPlugins?: Record<string, string>;
|
|
dependPackages?: Record<string, string>;
|
|
input?: {
|
|
[key: string]: AccessInputDefine;
|
|
};
|
|
};
|
|
export interface IAccessService {
|
|
getById<T = any>(id: any): Promise<T>;
|
|
getCommonById<T = any>(id: any): Promise<T>;
|
|
}
|
|
|
|
export interface IAccess {
|
|
ctx: AccessContext;
|
|
[key: string]: any;
|
|
}
|
|
|
|
export type AccessContext = {
|
|
http: HttpClient;
|
|
logger: ILogger;
|
|
utils: typeof utils;
|
|
accessService: IAccessService;
|
|
serviceGetter?: IServiceGetter;
|
|
define?: AccessDefine;
|
|
};
|
|
|
|
export abstract class BaseAccess implements IAccess {
|
|
ctx!: AccessContext;
|
|
runtimeDepsService?: IRuntimeDepsService;
|
|
|
|
async importRuntime(specifier: string) {
|
|
if (!this.runtimeDepsService) {
|
|
throw new Error("runtimeDepsService 未初始化");
|
|
}
|
|
return await this.runtimeDepsService.importRuntime(specifier, this.ctx.logger);
|
|
}
|
|
|
|
async setCtx(ctx: AccessContext) {
|
|
this.ctx = ctx;
|
|
if (!this.runtimeDepsService && this.ctx.serviceGetter) {
|
|
this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService");
|
|
}
|
|
if (this.runtimeDepsService && this.ctx.define?.name) {
|
|
await this.runtimeDepsService.ensureRuntimeDependencies({ pluginKeys: `access:${this.ctx.define.name}`, logger: this.ctx.logger });
|
|
}
|
|
}
|
|
|
|
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`);
|
|
}
|
|
|
|
normalizeEndpoint(endpoint: string) {
|
|
return endpoint.replace(/\/$/, "");
|
|
}
|
|
}
|