Skip to content

Commit

Permalink
types: use Options instead of Config if it is not actual config
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 6, 2024
1 parent b0f6d63 commit 979f2da
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 44 deletions.
4 changes: 2 additions & 2 deletions adapters/dingtalk/src/bot.ts
Expand Up @@ -103,7 +103,7 @@ export class DingtalkBot<C extends Context = Context> extends Bot<C, DingtalkBot
}

export namespace DingtalkBot {
export interface Config extends WsClient.Config {
export interface Config extends WsClient.Options {
secret: string
protocol: string
appkey: string
Expand All @@ -125,6 +125,6 @@ export namespace DingtalkBot {
api: Quester.createConfig('https://api.dingtalk.com/v1.0/'),
oldApi: Quester.createConfig('https://oapi.dingtalk.com/'),
}),
WsClient.Config,
WsClient.Options,
])
}
4 changes: 2 additions & 2 deletions adapters/dingtalk/src/ws.ts
Expand Up @@ -47,9 +47,9 @@ export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, D
}

export namespace WsClient {
export interface Config extends Adapter.WsClientConfig {}
export interface Options extends Adapter.WsClientConfig {}

export const Config: Schema<Config> = Schema.intersect([
export const Options: Schema<Options> = Schema.intersect([
Adapter.WsClientConfig,
] as const)
}
4 changes: 2 additions & 2 deletions adapters/discord/src/bot.ts
Expand Up @@ -200,7 +200,7 @@ export class DiscordBot<C extends Context = Context> extends Bot<C, DiscordBot.C
}

export namespace DiscordBot {
export interface Config extends Quester.Config, DiscordMessageEncoder.Config, WsClient.Config {
export interface Config extends Quester.Config, DiscordMessageEncoder.Config, WsClient.Options {
token: string
slash?: boolean
}
Expand All @@ -212,7 +212,7 @@ export namespace DiscordBot {
Schema.object({
slash: Schema.boolean().description('是否启用斜线指令。').default(true),
}).description('功能设置'),
WsClient.Config,
WsClient.Options,
DiscordMessageEncoder.Config,
Quester.createConfig('https://discord.com/api/v10'),
])
Expand Down
4 changes: 2 additions & 2 deletions adapters/discord/src/ws.ts
Expand Up @@ -104,11 +104,11 @@ export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, D
}

export namespace WsClient {
export interface Config extends Adapter.WsClientConfig {
export interface Options extends Adapter.WsClientConfig {
intents?: number
}

export const Config: Schema<Config> = Schema.intersect([
export const Options: Schema<Options> = Schema.intersect([
Schema.object({
intents: Schema.bitset(Gateway.Intent).description('需要订阅的机器人事件。').default(0
| Gateway.Intent.GUILD_MESSAGES
Expand Down
6 changes: 3 additions & 3 deletions adapters/kook/src/bot.ts
Expand Up @@ -175,7 +175,7 @@ export class KookBot<C extends Context = Context, T extends KookBot.Config = Koo
export namespace KookBot {
export interface BaseConfig extends Quester.Config, KookMessageEncoder.Config {}

export type Config = BaseConfig & (HttpServer.Config | WsClient.Config)
export type Config = BaseConfig & (HttpServer.Options | WsClient.Options)

export const Config: Schema<Config> = Schema.intersect([
Schema.object({
Expand All @@ -184,8 +184,8 @@ export namespace KookBot {
: Schema.union(['http', 'ws']).description('选择要使用的协议。').required(),
}),
Schema.union([
WsClient.Config,
HttpServer.Config,
WsClient.Options,
HttpServer.Options,
]),
KookMessageEncoder.Config,
Quester.createConfig('https://www.kookapp.cn/api/v3'),
Expand Down
8 changes: 4 additions & 4 deletions adapters/kook/src/http.ts
Expand Up @@ -3,15 +3,15 @@ import {} from '@cordisjs/server'
import { KookBot } from './bot'
import { adaptSession } from './utils'

export class HttpServer<C extends Context = Context> extends Adapter<C, KookBot<C, KookBot.BaseConfig & HttpServer.Config>> {
export class HttpServer<C extends Context = Context> extends Adapter<C, KookBot<C, KookBot.BaseConfig & HttpServer.Options>> {
static inject = ['server']

private logger: Logger

constructor(ctx: C, bot: KookBot<C>) {
super(ctx)
this.logger = ctx.logger('kook')
let { path } = bot.config as HttpServer.Config
let { path } = bot.config as HttpServer.Options
path = sanitize(path)
ctx.server.post(path, async (ctx) => {
const { body } = ctx.request
Expand Down Expand Up @@ -40,14 +40,14 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, KookBot<
}

export namespace HttpServer {
export interface Config {
export interface Options {
protocol: 'http'
path?: string
token: string
verifyToken: string
}

export const Config: Schema<Config> = Schema.object({
export const Options: Schema<Options> = Schema.object({
protocol: Schema.const('http').required(),
token: Schema.string().description('机器人令牌。').role('secret').required(),
verifyToken: Schema.string().description('验证令牌。').role('secret').required(),
Expand Down
6 changes: 3 additions & 3 deletions adapters/kook/src/ws.ts
Expand Up @@ -5,7 +5,7 @@ import { Payload, Signal } from './types'

const heartbeatIntervals = [6, 2, 4]

export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, KookBot<C, KookBot.BaseConfig & WsClient.Config>> {
export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, KookBot<C, KookBot.BaseConfig & WsClient.Options>> {
_sn = 0
_ping: NodeJS.Timeout
_heartbeat: NodeJS.Timeout
Expand Down Expand Up @@ -64,12 +64,12 @@ export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, K
}

export namespace WsClient {
export interface Config extends Adapter.WsClientConfig {
export interface Options extends Adapter.WsClientConfig {
protocol: 'ws'
token: string
}

export const Config: Schema<Config> = Schema.intersect([
export const Options: Schema<Options> = Schema.intersect([
Schema.object({
protocol: Schema.const('ws').required(process.env.KOISHI_ENV !== 'browser'),
token: Schema.string().description('机器人的用户令牌。').role('secret').required(),
Expand Down
2 changes: 1 addition & 1 deletion adapters/lark/src/bot.ts
Expand Up @@ -118,7 +118,7 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
}

export namespace LarkBot {
export interface Config extends HttpServer.Config, Quester.Config {
export interface Config extends HttpServer.Options, Quester.Config {
appId: string
appSecret: string
encryptKey?: string
Expand Down
4 changes: 2 additions & 2 deletions adapters/lark/src/http.ts
Expand Up @@ -141,14 +141,14 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, FeishuBo
}

export namespace HttpServer {
export interface Config {
export interface Options {
selfUrl?: string
path?: string
verifyToken?: boolean
verifySignature?: boolean
}

export const createConfig = (path: string): Schema<Config> => Schema.object({
export const createConfig = (path: string): Schema<Options> => Schema.object({
path: Schema.string().role('url').description('要连接的服务器地址。').default(path),
selfUrl: Schema.string().role('link').description('服务器暴露在公网的地址。缺省时将使用全局配置。'),
verifyToken: Schema.boolean().description('是否验证令牌。'),
Expand Down
4 changes: 2 additions & 2 deletions adapters/qq/src/bot/index.ts
Expand Up @@ -115,7 +115,7 @@ export class QQBot<C extends Context = Context> extends Bot<C, QQBot.Config> {
}

export namespace QQBot {
export interface Config extends QQ.Options, WsClient.Config {
export interface Config extends QQ.Options, WsClient.Options {
intents?: number
retryWhen: number[]
}
Expand All @@ -132,6 +132,6 @@ export namespace QQBot {
intents: Schema.bitset(QQ.Intents).description('需要订阅的机器人事件。'),
retryWhen: Schema.array(Number).description('发送消息遇到平台错误码时重试。').default([]),
}),
WsClient.Config,
WsClient.Options,
] as const)
}
4 changes: 2 additions & 2 deletions adapters/qq/src/ws.ts
Expand Up @@ -90,9 +90,9 @@ export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, Q
}

export namespace WsClient {
export interface Config extends Adapter.WsClientConfig { }
export interface Options extends Adapter.WsClientConfig { }

export const Config: Schema<Config> = Schema.intersect([
export const Options: Schema<Options> = Schema.intersect([
Adapter.WsClientConfig,
])
}
6 changes: 3 additions & 3 deletions adapters/slack/src/bot.ts
Expand Up @@ -191,7 +191,7 @@ export namespace SlackBot {
botToken: string
}

export type Config = BaseConfig & (HttpServer.Config | WsClient.Config)
export type Config = BaseConfig & (HttpServer.Options | WsClient.Options)

export const Config: Schema<Config> = Schema.intersect([
Schema.object({
Expand All @@ -200,8 +200,8 @@ export namespace SlackBot {
botToken: Schema.string().description('OAuth Token.').role('secret').required(),
}),
Schema.union([
WsClient.Config,
HttpServer.Config,
WsClient.Options,
HttpServer.Options,
]),
Quester.createConfig('https://slack.com/api/'),
] as const)
Expand Down
6 changes: 3 additions & 3 deletions adapters/slack/src/http.ts
Expand Up @@ -8,7 +8,7 @@ import { adaptSession } from './utils'
export class HttpServer<C extends Context = Context> extends Adapter<C, SlackBot<C>> {
static inject = ['server']

async connect(bot: SlackBot<C, SlackBot.Config & HttpServer.Config>) {
async connect(bot: SlackBot<C, SlackBot.Config & HttpServer.Options>) {
const { signing } = bot.config
await bot.getLogin()
this.ctx.server.post('/slack', async (ctx) => {
Expand Down Expand Up @@ -51,12 +51,12 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, SlackBot
}

export namespace HttpServer {
export interface Config {
export interface Options {
protocol: 'http'
signing: string
}

export const Config: Schema<Config> = Schema.object({
export const Options: Schema<Options> = Schema.object({
protocol: Schema.const('http').required(),
signing: Schema.string().required(),
})
Expand Down
6 changes: 3 additions & 3 deletions adapters/slack/src/ws.ts
Expand Up @@ -3,7 +3,7 @@ import { SlackBot } from './bot'
import { adaptSession } from './utils'
import { SocketEvent } from './types/events'

export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, SlackBot<C, SlackBot.BaseConfig & WsClient.Config>> {
export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, SlackBot<C, SlackBot.BaseConfig & WsClient.Options>> {
async prepare() {
await this.bot.getLogin()
const data = await this.bot.request('POST', '/apps.connections.open', {}, {}, true)
Expand Down Expand Up @@ -38,11 +38,11 @@ export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, S
}

export namespace WsClient {
export interface Config extends Adapter.WsClientConfig {
export interface Options extends Adapter.WsClientConfig {
protocol: 'ws'
}

export const Config: Schema<Config> = Schema.intersect([
export const Options: Schema<Options> = Schema.intersect([
Schema.object({
protocol: Schema.const('ws').required(process.env.KOISHI_ENV !== 'browser'),
}),
Expand Down
6 changes: 3 additions & 3 deletions adapters/telegram/src/bot.ts
Expand Up @@ -237,7 +237,7 @@ export namespace TelegramBot {
slash?: boolean
}

export type Config = BaseConfig & (HttpServer.Config | HttpPolling.Config)
export type Config = BaseConfig & (HttpServer.Options | HttpPolling.Options)

export namespace Config {
export interface Files {
Expand All @@ -255,8 +255,8 @@ export namespace TelegramBot {
: Schema.union(['server', 'polling']).description('选择要使用的协议。').required(),
}),
Schema.union([
HttpServer.Config,
HttpPolling.Config,
HttpServer.Options,
HttpPolling.Options,
]).description('推送设置'),
Schema.object({
slash: Schema.boolean().description('是否启用斜线指令。').default(true),
Expand Down
6 changes: 3 additions & 3 deletions adapters/telegram/src/polling.ts
Expand Up @@ -8,7 +8,7 @@ export class HttpPolling<C extends Context = Context> extends Adapter<C, Telegra
private offset = 0
private timeout: NodeJS.Timeout

async connect(bot: TelegramBot<C, TelegramBot.BaseConfig & HttpPolling.Config>) {
async connect(bot: TelegramBot<C, TelegramBot.BaseConfig & HttpPolling.Options>) {
bot.initialize(async () => {
let _retryCount = 0
let _initial = true
Expand Down Expand Up @@ -75,14 +75,14 @@ export class HttpPolling<C extends Context = Context> extends Adapter<C, Telegra
}

export namespace HttpPolling {
export interface Config {
export interface Options {
protocol: 'polling'
pollingTimeout?: number
retryTimes?: number
retryInterval?: number
}

export const Config: Schema<Config> = Schema.object({
export const Options: Schema<Options> = Schema.object({
protocol: Schema.const('polling').required(process.env.KOISHI_ENV !== 'browser'),
pollingTimeout: Schema.natural().role('ms').default(Time.second * 25).description('通过长轮询获取更新时请求的超时 (单位为毫秒)。'),
retryTimes: Schema.natural().description('初次连接时的最大重试次数。').default(6),
Expand Down
6 changes: 3 additions & 3 deletions adapters/telegram/src/server.ts
Expand Up @@ -7,7 +7,7 @@ import * as Telegram from './types'
export class HttpServer<C extends Context = Context> extends Adapter<C, TelegramBot<C>> {
static inject = ['server']

async connect(bot: TelegramBot<C, TelegramBot.BaseConfig & HttpServer.Config>) {
async connect(bot: TelegramBot<C, TelegramBot.BaseConfig & HttpServer.Options>) {
let { token, path, selfUrl } = bot.config
path = sanitize(path || '/telegram')
if (selfUrl) {
Expand Down Expand Up @@ -38,13 +38,13 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, Telegram
}

export namespace HttpServer {
export interface Config {
export interface Options {
protocol: 'server'
path?: string
selfUrl?: string
}

export const Config: Schema<Config> = Schema.object({
export const Options: Schema<Options> = Schema.object({
protocol: Schema.const('server').required(),
path: Schema.string().description('服务器监听的路径。').default('/telegram'),
selfUrl: Schema.string().role('link').description('服务器暴露在公网的地址。缺省时将使用全局配置。'),
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/adapter.ts
Expand Up @@ -103,7 +103,6 @@ export namespace Adapter {

export abstract class WsClient<C extends Context, B extends Bot<C, WsClientConfig>> extends WsClientBase<C, B> {
static reusable = true
static Config = WsClientConfig

constructor(ctx: C, public bot: B) {
super(ctx, bot.config)
Expand Down

0 comments on commit 979f2da

Please sign in to comment.