mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-11 23:40:24 +00:00
迁移类型校验到zod
This commit is contained in:
@@ -1,108 +1,107 @@
|
||||
import { Type, Static } from '@sinclair/typebox';
|
||||
import Ajv from 'ajv';
|
||||
import { z } from 'zod';
|
||||
|
||||
const HttpServerConfigSchema = Type.Object({
|
||||
name: Type.String({ default: 'http-server' }),
|
||||
enable: Type.Boolean({ default: false }),
|
||||
port: Type.Number({ default: 3000 }),
|
||||
host: Type.String({ default: '0.0.0.0' }),
|
||||
enableCors: Type.Boolean({ default: true }),
|
||||
enableWebsocket: Type.Boolean({ default: true }),
|
||||
messagePostFormat: Type.String({ default: 'array' }),
|
||||
token: Type.String({ default: '' }),
|
||||
debug: Type.Boolean({ default: false })
|
||||
const HttpServerConfigSchema = z.object({
|
||||
name: z.string().default('http-server'),
|
||||
enable: z.boolean().default(false),
|
||||
port: z.number().default(3000),
|
||||
host: z.string().default('0.0.0.0'),
|
||||
enableCors: z.boolean().default(true),
|
||||
enableWebsocket: z.boolean().default(true),
|
||||
messagePostFormat: z.string().default('array'),
|
||||
token: z.string().default(''),
|
||||
debug: z.boolean().default(false)
|
||||
});
|
||||
|
||||
const HttpSseServerConfigSchema = Type.Object({
|
||||
name: Type.String({ default: 'http-sse-server' }),
|
||||
enable: Type.Boolean({ default: false }),
|
||||
port: Type.Number({ default: 3000 }),
|
||||
host: Type.String({ default: '0.0.0.0' }),
|
||||
enableCors: Type.Boolean({ default: true }),
|
||||
enableWebsocket: Type.Boolean({ default: true }),
|
||||
messagePostFormat: Type.String({ default: 'array' }),
|
||||
token: Type.String({ default: '' }),
|
||||
debug: Type.Boolean({ default: false }),
|
||||
reportSelfMessage: Type.Boolean({ default: false })
|
||||
const HttpSseServerConfigSchema = z.object({
|
||||
name: z.string().default('http-sse-server'),
|
||||
enable: z.boolean().default(false),
|
||||
port: z.number().default(3000),
|
||||
host: z.string().default('0.0.0.0'),
|
||||
enableCors: z.boolean().default(true),
|
||||
enableWebsocket: z.boolean().default(true),
|
||||
messagePostFormat: z.string().default('array'),
|
||||
token: z.string().default(''),
|
||||
debug: z.boolean().default(false),
|
||||
reportSelfMessage: z.boolean().default(false)
|
||||
});
|
||||
|
||||
const HttpClientConfigSchema = Type.Object({
|
||||
name: Type.String({ default: 'http-client' }),
|
||||
enable: Type.Boolean({ default: false }),
|
||||
url: Type.String({ default: 'http://localhost:8080' }),
|
||||
messagePostFormat: Type.String({ default: 'array' }),
|
||||
reportSelfMessage: Type.Boolean({ default: false }),
|
||||
token: Type.String({ default: '' }),
|
||||
debug: Type.Boolean({ default: false })
|
||||
const HttpClientConfigSchema = z.object({
|
||||
name: z.string().default('http-client'),
|
||||
enable: z.boolean().default(false),
|
||||
url: z.string().default('http://localhost:8080'),
|
||||
messagePostFormat: z.string().default('array'),
|
||||
reportSelfMessage: z.boolean().default(false),
|
||||
token: z.string().default(''),
|
||||
debug: z.boolean().default(false)
|
||||
});
|
||||
|
||||
const WebsocketServerConfigSchema = Type.Object({
|
||||
name: Type.String({ default: 'websocket-server' }),
|
||||
enable: Type.Boolean({ default: false }),
|
||||
host: Type.String({ default: '0.0.0.0' }),
|
||||
port: Type.Number({ default: 3001 }),
|
||||
messagePostFormat: Type.String({ default: 'array' }),
|
||||
reportSelfMessage: Type.Boolean({ default: false }),
|
||||
token: Type.String({ default: '' }),
|
||||
enableForcePushEvent: Type.Boolean({ default: true }),
|
||||
debug: Type.Boolean({ default: false }),
|
||||
heartInterval: Type.Number({ default: 30000 })
|
||||
const WebsocketServerConfigSchema = z.object({
|
||||
name: z.string().default('websocket-server'),
|
||||
enable: z.boolean().default(false),
|
||||
host: z.string().default('0.0.0.0'),
|
||||
port: z.number().default(3001),
|
||||
messagePostFormat: z.string().default('array'),
|
||||
reportSelfMessage: z.boolean().default(false),
|
||||
token: z.string().default(''),
|
||||
enableForcePushEvent: z.boolean().default(true),
|
||||
debug: z.boolean().default(false),
|
||||
heartInterval: z.number().default(30000)
|
||||
});
|
||||
|
||||
const WebsocketClientConfigSchema = Type.Object({
|
||||
name: Type.String({ default: 'websocket-client' }),
|
||||
enable: Type.Boolean({ default: false }),
|
||||
url: Type.String({ default: 'ws://localhost:8082' }),
|
||||
messagePostFormat: Type.String({ default: 'array' }),
|
||||
reportSelfMessage: Type.Boolean({ default: false }),
|
||||
reconnectInterval: Type.Number({ default: 5000 }),
|
||||
token: Type.String({ default: '' }),
|
||||
debug: Type.Boolean({ default: false }),
|
||||
heartInterval: Type.Number({ default: 30000 })
|
||||
const WebsocketClientConfigSchema = z.object({
|
||||
name: z.string().default('websocket-client'),
|
||||
enable: z.boolean().default(false),
|
||||
url: z.string().default('ws://localhost:8082'),
|
||||
messagePostFormat: z.string().default('array'),
|
||||
reportSelfMessage: z.boolean().default(false),
|
||||
reconnectInterval: z.number().default(5000),
|
||||
token: z.string().default(''),
|
||||
debug: z.boolean().default(false),
|
||||
heartInterval: z.number().default(30000)
|
||||
});
|
||||
|
||||
const PluginConfigSchema = Type.Object({
|
||||
name: Type.String({ default: 'plugin' }),
|
||||
enable: Type.Boolean({ default: false }),
|
||||
messagePostFormat: Type.String({ default: 'array' }),
|
||||
reportSelfMessage: Type.Boolean({ default: false }),
|
||||
debug: Type.Boolean({ default: false }),
|
||||
const PluginConfigSchema = z.object({
|
||||
name: z.string().default('plugin'),
|
||||
enable: z.boolean().default(false),
|
||||
messagePostFormat: z.string().default('array'),
|
||||
reportSelfMessage: z.boolean().default(false),
|
||||
debug: z.boolean().default(false),
|
||||
});
|
||||
|
||||
const NetworkConfigSchema = Type.Object({
|
||||
httpServers: Type.Array(HttpServerConfigSchema, { default: [] }),
|
||||
httpSseServers: Type.Array(HttpSseServerConfigSchema, { default: [] }),
|
||||
httpClients: Type.Array(HttpClientConfigSchema, { default: [] }),
|
||||
websocketServers: Type.Array(WebsocketServerConfigSchema, { default: [] }),
|
||||
websocketClients: Type.Array(WebsocketClientConfigSchema, { default: [] }),
|
||||
plugins: Type.Array(PluginConfigSchema, { default: [] })
|
||||
}, { default: {} });
|
||||
const NetworkConfigSchema = z.object({
|
||||
httpServers: z.array(HttpServerConfigSchema).default([]),
|
||||
httpSseServers: z.array(HttpSseServerConfigSchema).default([]),
|
||||
httpClients: z.array(HttpClientConfigSchema).default([]),
|
||||
websocketServers: z.array(WebsocketServerConfigSchema).default([]),
|
||||
websocketClients: z.array(WebsocketClientConfigSchema).default([]),
|
||||
plugins: z.array(PluginConfigSchema).default([])
|
||||
}).default({});
|
||||
|
||||
export const OneBotConfigSchema = Type.Object({
|
||||
export const OneBotConfigSchema = z.object({
|
||||
network: NetworkConfigSchema,
|
||||
musicSignUrl: Type.String({ default: '' }),
|
||||
enableLocalFile2Url: Type.Boolean({ default: false }),
|
||||
parseMultMsg: Type.Boolean({ default: false })
|
||||
musicSignUrl: z.string().default(''),
|
||||
enableLocalFile2Url: z.boolean().default(false),
|
||||
parseMultMsg: z.boolean().default(false)
|
||||
});
|
||||
|
||||
export type OneBotConfig = Static<typeof OneBotConfigSchema>;
|
||||
export type HttpServerConfig = Static<typeof HttpServerConfigSchema>;
|
||||
export type HttpSseServerConfig = Static<typeof HttpSseServerConfigSchema>;
|
||||
export type HttpClientConfig = Static<typeof HttpClientConfigSchema>;
|
||||
export type WebsocketServerConfig = Static<typeof WebsocketServerConfigSchema>;
|
||||
export type WebsocketClientConfig = Static<typeof WebsocketClientConfigSchema>;
|
||||
export type PluginConfig = Static<typeof PluginConfigSchema>;
|
||||
export type OneBotConfig = z.infer<typeof OneBotConfigSchema>;
|
||||
export type HttpServerConfig = z.infer<typeof HttpServerConfigSchema>;
|
||||
export type HttpSseServerConfig = z.infer<typeof HttpSseServerConfigSchema>;
|
||||
export type HttpClientConfig = z.infer<typeof HttpClientConfigSchema>;
|
||||
export type WebsocketServerConfig = z.infer<typeof WebsocketServerConfigSchema>;
|
||||
export type WebsocketClientConfig = z.infer<typeof WebsocketClientConfigSchema>;
|
||||
export type PluginConfig = z.infer<typeof PluginConfigSchema>;
|
||||
|
||||
export type NetworkAdapterConfig = HttpServerConfig | HttpSseServerConfig | HttpClientConfig | WebsocketServerConfig | WebsocketClientConfig | PluginConfig;
|
||||
export type NetworkConfigKey = keyof OneBotConfig['network'];
|
||||
|
||||
|
||||
export function loadConfig(config: Partial<OneBotConfig>): OneBotConfig {
|
||||
const ajv = new Ajv({ useDefaults: true, coerceTypes: true });
|
||||
const validate = ajv.compile(OneBotConfigSchema);
|
||||
const valid = validate(config);
|
||||
if (!valid) {
|
||||
throw new Error(ajv.errorsText(validate.errors));
|
||||
try {
|
||||
return OneBotConfigSchema.parse(config);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
throw new Error(error.errors.map(e => `${e.path.join('.')}: ${e.message}`).join(', '));
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
return config as OneBotConfig;
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import { ConfigBase } from '@/common/config-base';
|
||||
import type { NapCatCore } from '@/core';
|
||||
import { OneBotConfig } from './config';
|
||||
import { AnySchema } from 'ajv';
|
||||
import { z } from 'zod';
|
||||
|
||||
|
||||
export class OB11ConfigLoader extends ConfigBase<OneBotConfig> {
|
||||
constructor(core: NapCatCore, configPath: string, schema: AnySchema) {
|
||||
constructor(core: NapCatCore, configPath: string, schema: z.ZodType<OneBotConfig>) {
|
||||
super('onebot11', core, configPath, schema);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user