@saleandwin/config
Version:
Configuration center with KV capabilities for framework-ts projects
570 lines (562 loc) • 15.4 kB
TypeScript
import { D1Database, D1PreparedStatement, PaginationResult } from '@saleandwin/common';
import { z } from 'zod';
interface ConfigItem {
/**
* id
*/
id: string;
key: string;
value: string;
type: 'string' | 'number' | 'boolean' | 'json' | 'array';
namespace: string;
description?: string;
is_active: boolean;
is_encrypted: boolean;
created_at: string;
updated_at: string;
created_by?: string;
updated_by?: string;
}
interface CreateConfigRequest$1 {
/**
* 配置键
*/
key: string;
value: string;
type: 'string' | 'number' | 'boolean' | 'json' | 'array';
namespace: string;
description?: string;
is_encrypted?: boolean;
created_by?: string;
}
interface UpdateConfigRequest$1 {
value?: string;
type?: 'string' | 'number' | 'boolean' | 'json' | 'array';
namespace?: string;
description?: string;
is_active?: boolean;
is_encrypted?: boolean;
updated_by?: string;
}
interface ConfigQueryParams$1 {
namespace?: string;
key?: string;
is_active?: boolean;
search?: string;
}
interface ConfigKVPair {
key: string;
value: any;
}
interface ConfigNamespace {
namespace: string;
count: number;
description?: string;
}
interface ConfigValidationRule {
key: string;
required?: boolean;
type?: 'string' | 'number' | 'boolean' | 'json' | 'array';
pattern?: string;
minLength?: number;
maxLength?: number;
min?: number;
max?: number;
enum?: string[];
validator?: (value: any) => boolean | string;
}
interface ConfigTemplate {
name: string;
description?: string;
category: string;
configs: Array<{
key: string;
defaultValue: string;
type: 'string' | 'number' | 'boolean' | 'json' | 'array';
description?: string;
required?: boolean;
}>;
}
interface ConfigHistory {
id: string;
config_id: string;
config_key: string;
old_value?: string;
new_value?: string;
action: 'create' | 'update' | 'delete';
changed_at: string;
changed_by?: string;
}
declare class ConfigTable {
private db;
/**
* 创建ConfigTable实例的静态工厂方法
* @param env Cloudflare Workers 环境对象
*/
static createInstance(env: {
DB?: D1Database;
}): ConfigTable;
constructor(db: D1Database);
/**
* 获取Cloudflare环境中的数据库连接
* @param env Cloudflare Workers 环境对象
*/
protected static getDatabase(env: {
DB?: D1Database;
}): D1Database;
/**
* 根据ID查询配置
*/
prepareGetById(id: string): D1PreparedStatement;
/**
* 根据key查询配置
*/
prepareGetByKey(key: string): D1PreparedStatement;
/**
* 检查配置key是否存在
*/
prepareCheckKeyExists(key: string, excludeId?: string): D1PreparedStatement;
/**
* 查询配置列表
*/
prepareGetList(params: ConfigQueryParams$1, limit: number, offset: number): D1PreparedStatement;
/**
* 统计配置总数
*/
prepareGetCount(params: ConfigQueryParams$1): D1PreparedStatement;
/**
* 创建配置
*/
prepareCreate(data: CreateConfigRequest$1): D1PreparedStatement;
/**
* 更新配置
*/
prepareUpdate(id: string, data: UpdateConfigRequest$1): D1PreparedStatement;
/**
* 删除配置(软删除)
*/
prepareDelete(id: string, deletedBy?: string): D1PreparedStatement;
/**
* 获取所有命名空间
*/
prepareGetNamespaces(): D1PreparedStatement;
/**
* 根据命名空间获取配置
*/
prepareGetByNamespace(namespace: string): D1PreparedStatement;
/**
* 批量获取配置
*/
prepareGetByKeys(keys: string[]): D1PreparedStatement;
/**
* 获取配置历史记录
*/
prepareGetHistory(configKey: string, limit?: number): D1PreparedStatement;
}
declare class ConfigService {
private configTable;
private env;
private validationRules;
constructor(configTable: ConfigTable, env: {
DB?: any;
});
/**
* 创建ConfigService实例的静态工厂方法
* @param env Cloudflare Workers 环境对象
*/
static createInstance(env: {
DB?: any;
}): ConfigService;
/**
* 添加验证规则
*/
addValidationRule(rule: ConfigValidationRule): void;
/**
* 验证配置值
*/
private validateConfigValue;
/**
* 解析配置值
*/
private parseConfigValue;
/**
* 创建配置
*/
createConfig(data: CreateConfigRequest$1): Promise<ConfigItem>;
/**
* 更新配置
*/
updateConfig(id: string, data: UpdateConfigRequest$1): Promise<ConfigItem>;
/**
* 删除配置
*/
deleteConfig(id: string, deletedBy?: string): Promise<void>;
/**
* 获取配置(移除缓存功能)
*/
getConfig(key: string): Promise<any>;
/**
* 批量获取配置
*/
getConfigs(keys: string[]): Promise<ConfigKVPair[]>;
/**
* 获取配置列表
*/
getConfigList(params: ConfigQueryParams$1, page: number, limit: number): Promise<{
configs: ConfigItem[];
pagination: PaginationResult;
}>;
/**
* 获取命名空间列表
*/
getNamespaces(): Promise<ConfigNamespace[]>;
/**
* 根据命名空间获取配置
*/
getConfigsByNamespace(namespace: string): Promise<ConfigKVPair[]>;
/**
* 获取配置历史记录
*/
getConfigHistory(configKey: string, limit?: number): Promise<ConfigHistory[]>;
}
declare const CreateConfigRequestSchema: z.ZodObject<{
/**
* 配置键
*/
key: z.ZodString;
value: z.ZodString;
type: z.ZodEnum<["string", "number", "boolean", "json", "array"]>;
namespace: z.ZodString;
description: z.ZodOptional<z.ZodString>;
is_encrypted: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
created_by: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
namespace: string;
type: "string" | "number" | "boolean" | "json" | "array";
key: string;
value: string;
is_encrypted: boolean;
description?: string | undefined;
created_by?: string | undefined;
}, {
namespace: string;
type: "string" | "number" | "boolean" | "json" | "array";
key: string;
value: string;
description?: string | undefined;
is_encrypted?: boolean | undefined;
created_by?: string | undefined;
}>;
declare const UpdateConfigRequestSchema: z.ZodObject<{
value: z.ZodOptional<z.ZodString>;
type: z.ZodOptional<z.ZodEnum<["string", "number", "boolean", "json", "array"]>>;
namespace: z.ZodOptional<z.ZodString>;
description: z.ZodOptional<z.ZodString>;
is_active: z.ZodOptional<z.ZodBoolean>;
is_encrypted: z.ZodOptional<z.ZodBoolean>;
updated_by: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
namespace?: string | undefined;
type?: "string" | "number" | "boolean" | "json" | "array" | undefined;
value?: string | undefined;
description?: string | undefined;
is_encrypted?: boolean | undefined;
is_active?: boolean | undefined;
updated_by?: string | undefined;
}, {
namespace?: string | undefined;
type?: "string" | "number" | "boolean" | "json" | "array" | undefined;
value?: string | undefined;
description?: string | undefined;
is_encrypted?: boolean | undefined;
is_active?: boolean | undefined;
updated_by?: string | undefined;
}>;
declare const ConfigQueryParamsSchema: z.ZodObject<{
namespace: z.ZodOptional<z.ZodString>;
key: z.ZodOptional<z.ZodString>;
is_active: z.ZodOptional<z.ZodBoolean>;
search: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
search?: string | undefined;
namespace?: string | undefined;
key?: string | undefined;
is_active?: boolean | undefined;
}, {
search?: string | undefined;
namespace?: string | undefined;
key?: string | undefined;
is_active?: boolean | undefined;
}>;
declare const PaginationParamsSchema: z.ZodObject<{
page: z.ZodNumber;
limit: z.ZodNumber;
}, "strip", z.ZodTypeAny, {
page: number;
limit: number;
}, {
page: number;
limit: number;
}>;
declare const BatchGetConfigsSchema: z.ZodObject<{
keys: z.ZodArray<z.ZodString, "many">;
}, "strip", z.ZodTypeAny, {
keys: string[];
}, {
keys: string[];
}>;
declare const ConfigHistoryParamsSchema: z.ZodObject<{
configKey: z.ZodString;
limit: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
}, "strip", z.ZodTypeAny, {
limit: number;
configKey: string;
}, {
configKey: string;
limit?: number | undefined;
}>;
type CreateConfigRequest = z.infer<typeof CreateConfigRequestSchema>;
type UpdateConfigRequest = z.infer<typeof UpdateConfigRequestSchema>;
type ConfigQueryParams = z.infer<typeof ConfigQueryParamsSchema>;
type PaginationParams = z.infer<typeof PaginationParamsSchema>;
type BatchGetConfigs = z.infer<typeof BatchGetConfigsSchema>;
type ConfigHistoryParams = z.infer<typeof ConfigHistoryParamsSchema>;
declare class ConfigValidator {
/**
* 验证创建配置请求
*/
static validateCreateRequest(data: unknown): {
success: true;
data: CreateConfigRequest;
} | {
success: false;
error: string;
};
/**
* 验证更新配置请求
*/
static validateUpdateRequest(data: unknown): {
success: true;
data: UpdateConfigRequest;
} | {
success: false;
error: string;
};
/**
* 验证查询参数
*/
static validateQueryParams(data: unknown): {
success: true;
data: ConfigQueryParams;
} | {
success: false;
error: string;
};
/**
* 验证分页参数
*/
static validatePaginationParams(data: unknown): {
success: true;
data: PaginationParams;
} | {
success: false;
error: string;
};
/**
* 验证批量获取请求
*/
static validateBatchGetRequest(data: unknown): {
success: true;
data: BatchGetConfigs;
} | {
success: false;
error: string;
};
/**
* 验证配置ID
*/
static validateConfigId(id: unknown): {
success: true;
data: string;
} | {
success: false;
error: string;
};
}
declare class ConfigHandler {
private configService;
constructor(configService: ConfigService);
/**
* 创建ConfigHandler实例的静态工厂方法
* @param env Cloudflare Workers 环境对象
*/
static createInstance(env: {
DB?: any;
}): ConfigHandler;
/**
* 创建配置
*/
handleCreate(data: CreateConfigRequest): Promise<ConfigItem>;
/**
* 更新配置
*/
handleUpdate(id: string, data: UpdateConfigRequest): Promise<ConfigItem>;
/**
* 删除配置
*/
handleDelete(id: string, deletedBy?: string): Promise<void>;
/**
* 获取配置值(移除缓存参数)
*/
handleGet(key: string): Promise<{
key: string;
value: any;
}>;
/**
* 批量获取配置
*/
handleBatchGet(data: BatchGetConfigs): Promise<ConfigKVPair[]>;
/**
* 获取配置列表
*/
handleList(queryParams: ConfigQueryParams, paginationParams: PaginationParams): Promise<{
configs: ConfigItem[];
pagination: {
page: number;
limit: number;
total: number;
totalPages: number;
};
}>;
/**
* 获取命名空间列表
*/
handleGetNamespaces(): Promise<ConfigNamespace[]>;
/**
* 根据命名空间获取配置
*/
handleGetByNamespace(namespace: string): Promise<ConfigKVPair[]>;
/**
* 获取配置历史记录
*/
handleGetHistory(configKey: string, limit?: number): Promise<ConfigHistory[]>;
/**
* 健康检查
*/
handleHealthCheck(): Promise<{
status: string;
timestamp: string;
service: string;
}>;
}
declare class ConfigUtils {
/**
* 验证配置key格式
*/
static isValidKey(key: string): boolean;
/**
* 验证配置类型
*/
static isValidType(type: string): boolean;
/**
* 解析配置值
*/
static parseValue(value: string, type: string): any;
/**
* 序列化配置值
*/
static serializeValue(value: any, type: string): string;
/**
* 生成配置key的建议
*/
static suggestKey(category: string, name: string): string;
/**
* 验证JSON格式
*/
static isValidJson(value: string): boolean;
/**
* 从环境变量创建配置
* @param env Cloudflare Workers 环境对象
* @param envKey 环境变量键名
* @param configKey 配置键名
* @param type 配置类型
* @param defaultValue 默认值
*/
static fromEnv(env: any, envKey: string, configKey: string, type?: string, defaultValue?: string): {
key: string;
value: string;
type: any;
category: string;
description: string;
created_by: string;
};
/**
* 批量从环境变量创建配置
* @param env Cloudflare Workers 环境对象
* @param mappings 环境变量映射配置
*/
static batchFromEnv(env: any, mappings: Array<{
envKey: string;
configKey: string;
type?: string;
defaultValue?: string;
category?: string;
}>): {
key: string;
value: string;
type: any;
category: string;
description: string;
created_by: string;
}[];
}
declare const CONFIG_CONSTANTS: {
DEFAULT_CATEGORIES: string[];
CONFIG_TYPES: readonly ["string", "number", "boolean", "json", "array"];
CACHE_TTL: {
MEMORY: number;
KV: number;
DATABASE: number;
};
VALIDATION: {
KEY_MAX_LENGTH: number;
VALUE_MAX_LENGTH: number;
DESCRIPTION_MAX_LENGTH: number;
CATEGORY_MAX_LENGTH: number;
};
SYSTEM_PREFIX: string;
RESERVED_KEYS: string[];
};
declare const CONFIG_TEMPLATES: {
DATABASE: {
name: string;
category: string;
configs: {
key: string;
defaultValue: string;
type: string;
description: string;
}[];
};
CACHE: {
name: string;
category: string;
configs: {
key: string;
defaultValue: string;
type: string;
description: string;
}[];
};
API: {
name: string;
category: string;
configs: {
key: string;
defaultValue: string;
type: string;
description: string;
}[];
};
};
export { type BatchGetConfigs, CONFIG_CONSTANTS, CONFIG_TEMPLATES, ConfigHandler, type ConfigHistory, type ConfigHistoryParams, type ConfigItem, type ConfigKVPair, type ConfigNamespace, type ConfigQueryParams, ConfigService, ConfigTable, type ConfigTemplate, ConfigUtils, type ConfigValidationRule, ConfigValidator, type CreateConfigRequest, type PaginationParams, type UpdateConfigRequest };