Files
noco-auto/agents/query/query-config.ts
2025-12-03 16:41:01 +08:00

108 lines
2.3 KiB
TypeScript

import { Query } from '@kevisual/query';
import type { Result } from '@kevisual/query/query';
type QueryConfigOpts = {
query?: Query;
};
export type Config<T = any> = {
id?: string;
title?: string;
key?: string;
description?: string;
data?: T;
createdAt?: string;
updatedAt?: string;
};
export type UploadConfig = {
key?: string;
version?: string;
};
type PostOpts = {
token?: string;
};
export const defaultConfigKeys = ['upload.json', 'workspace.json', 'ai.json', 'user.json', 'life.json'] as const;
type DefaultConfigKey = (typeof defaultConfigKeys)[number];
export class QueryConfig {
query: Query;
constructor(opts?: QueryConfigOpts) {
this.query = opts?.query || new Query();
}
async post<T = Config>(data: any) {
return this.query.post<T>({ path: 'config', ...data });
}
async getConfig({ id, key }: { id?: string; key?: string }, opts?: PostOpts) {
return this.post({
key: 'get',
data: {
id,
key,
},
...opts,
});
}
async updateConfig(data: Config, opts?: PostOpts) {
return this.post({
key: 'update',
data,
...opts,
});
}
async deleteConfig(id: string, opts?: PostOpts) {
return this.post({
key: 'delete',
data: { id },
});
}
async listConfig(opts?: PostOpts) {
return this.post<{ list: Config[] }>({
key: 'list',
...opts,
});
}
/**
* 获取上传配置
* @returns
*/
async getUploadConfig(opts?: PostOpts) {
return this.post<Result<Config<UploadConfig>>>({
key: 'getUploadConfig',
...opts,
});
}
/**
* 更新上传配置
* @param data
* @returns
*/
async updateUploadConfig(data: Config, opts?: PostOpts) {
return this.post<Result<Config<UploadConfig>>>({
key: 'updateUploadConfig',
data,
...opts,
});
}
/**
* 检测配置是否存在
* @param id
* @returns
*/
async detectConfig(opts?: PostOpts) {
return this.post<{ updateList: Config[] }>({
key: 'detect',
...opts,
});
}
/**
* 获取配置, 获取默认的配置项
* @param key
* @returns
*/
async getConfigByKey(key: DefaultConfigKey, opts?: PostOpts) {
return this.post<Result<Config>>({
key: 'defaultConfig',
configKey: key,
...opts,
});
}
}