Skip to content

Commit

Permalink
fix(core): validate adapter config should not lose props
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 1, 2022
1 parent 34edd3a commit 07a5e53
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
"dependencies": {
"@koishijs/utils": "^5.0.0",
"fastest-levenshtein": "^1.0.12",
"schemastery": "^2.2.0"
"schemastery": "^2.3.0"
}
}
17 changes: 16 additions & 1 deletion packages/core/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,31 @@ export namespace Adapter {
} else {
library[platform] = { [redirect]: args[1] } as Constructor
BotConfig = library[platform].schema = Schema.union([]).description('机器人要使用的协议。')
const FlatConfig = Schema.object({ protocol: Schema.string() })
function flatten(schema: Schema) {
if (schema.type === 'union' || schema.type === 'intersect') {
schema.list.forEach(flatten)
} else if (schema.type === 'object') {
for (const key in schema.dict) {
FlatConfig.dict[key] = new Schema(schema.dict[key])
FlatConfig.dict[key].meta = { ...schema.dict[key].meta, required: false }
}
} else {
throw new Error('cannot flatten bot schema')
}
}

for (const protocol in args[0]) {
library[join(platform, protocol)] = args[0][protocol]
flatten(args[0][protocol].schema)
BotConfig.list.push(Schema.intersect([
Schema.object({
protocol: Schema.const(protocol).required(),
}),
args[0][protocol].schema,
]).description(protocol))
}
BotConfig.list.push(Schema.transform(Schema.dict(Schema.any()), (value) => {
BotConfig.list.push(Schema.transform(FlatConfig, (value) => {
if (value.protocol) throw new Error(`unknown protocol "${value.protocol}"`)
value.protocol = args[1](value) as never
logger.debug('infer type as %s', value.protocol)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export namespace Plugin {
: T extends Object<infer U> ? U
: never

export interface State<T = any> {
export interface State {
id?: string
parent?: State
context?: Context
config?: T
config?: any
using?: readonly (keyof Context.Services)[]
schema?: Schema
plugin?: Plugin
Expand Down
5 changes: 1 addition & 4 deletions plugins/adapter/telegram/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ export class HttpPolling extends TelegramAdapter {
static schema = Schema.intersect([
BotConfig,
Schema.object({
pollingTimeout: Schema.union([
Schema.number(),
Schema.transform(Schema.const(true as const), () => Time.minute),
]).description('通过长轮询获取更新时请求的超时 (单位为秒)。'),
pollingTimeout: Schema.number().default(Time.minute).description('通过长轮询获取更新时请求的超时 (单位为秒)。'),
}),
])

Expand Down

0 comments on commit 07a5e53

Please sign in to comment.