Skip to content

Commit

Permalink
feat(core): enhance typings
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 8, 2024
1 parent e5825f1 commit 8f96625
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"@satorijs/element": "^3.1.4",
"@satorijs/protocol": "^1.2.1",
"cordis": "^3.9.0",
"cordis": "^3.9.1",
"cordis-axios": "^4.1.0",
"cosmokit": "^1.5.2",
"ws": "^8.14.2"
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Awaitable, remove, Time } from 'cosmokit'
import { Status, WebSocket } from '@satorijs/protocol'
import { Context, Schema } from '.'
import { z } from 'cordis'
import { Context } from '.'
import { Bot } from './bot'

export abstract class Adapter<C extends Context = Context, B extends Bot<C> = Bot<C>> {
Expand Down Expand Up @@ -28,10 +29,10 @@ export namespace Adapter {
retryInterval?: number
}

export const WsClientConfig: Schema<WsClientConfig> = Schema.object({
retryTimes: Schema.natural().description('初次连接时的最大重试次数。').default(6),
retryInterval: Schema.natural().role('ms').description('初次连接时的重试时间间隔。').default(5 * Time.second),
retryLazy: Schema.natural().role('ms').description('连接关闭后的重试时间间隔。').default(Time.minute),
export const WsClientConfig: z<WsClientConfig> = z.object({
retryTimes: z.natural().description('初次连接时的最大重试次数。').default(6),
retryInterval: z.natural().role('ms').description('初次连接时的重试时间间隔。').default(5 * Time.second),
retryLazy: z.natural().role('ms').description('连接关闭后的重试时间间隔。').default(Time.minute),
}).description('连接设置')

export abstract class WsClientBase<C extends Context, B extends Bot<C>> extends Adapter<C, B> {
Expand Down
43 changes: 22 additions & 21 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cordis from 'cordis'
import { Awaitable, defineProperty, Dict } from 'cosmokit'
import { Bot } from './bot'
import { Session } from './session'
import { Schema } from 'cordis'
import { z } from 'cordis'
import { Quester } from 'cordis-axios'
import { Event, SendOptions } from '@satorijs/protocol'
import h from '@satorijs/element'
Expand All @@ -13,10 +13,9 @@ h.warn = new cordis.Logger('element').warn
// because `esModuleInterop` is not respected by esbuild
export type { Fragment, Render } from '@satorijs/element'

export { Logger, Schema, Schema as z } from 'cordis'
export { h, h as Element, h as segment, Quester }

export { h, h as Element, h as segment }
export { Quester }
export * from 'cordis'

export * as Satori from '@satorijs/protocol'
export * as Universal from '@satorijs/protocol'
Expand All @@ -28,23 +27,23 @@ export * from './session'

declare module 'cordis-axios' {
namespace Quester {
export const Config: Schema<Config>
export function createConfig(this: typeof Quester, endpoint?: string | boolean): Schema<Config>
export const Config: z<Config>
export function createConfig(this: typeof Quester, endpoint?: string | boolean): z<Config>
}
}

defineProperty(Quester, 'Config', Schema.object({
timeout: Schema.natural().role('ms').description('等待连接建立的最长时间。'),
proxyAgent: Schema.string().description('使用的代理服务器地址。'),
keepAlive: Schema.boolean().description('是否保持连接。'),
defineProperty(Quester, 'Config', z.object({
timeout: z.natural().role('ms').description('等待连接建立的最长时间。'),
proxyAgent: z.string().description('使用的代理服务器地址。'),
keepAlive: z.boolean().description('是否保持连接。'),
}).description('请求设置'))

Quester.createConfig = function createConfig(this, endpoint) {
return Schema.object({
endpoint: Schema.string().role('link').description('要连接的服务器地址。')
return z.object({
endpoint: z.string().role('link').description('要连接的服务器地址。')
.default(typeof endpoint === 'string' ? endpoint : null)
.required(typeof endpoint === 'boolean' ? endpoint : false),
headers: Schema.dict(String).role('table').description('要附加的额外请求头。'),
headers: z.dict(String).role('table').description('要附加的额外请求头。'),
...this.Config.dict,
}).description('请求设置')
}
Expand Down Expand Up @@ -98,14 +97,12 @@ export interface Events<C extends Context = Context> extends cordis.Events<C> {
'bot-disconnect'(client: Bot<C>): Awaitable<void>
}

export type EffectScope<C extends Context = Context> = cordis.EffectScope<C>
export type ForkScope<C extends Context = Context> = cordis.ForkScope<C>
export type MainScope<C extends Context = Context> = cordis.MainScope<C>
export interface EffectScope<C extends Context = Context> extends cordis.EffectScope<C> {}
export interface ForkScope<C extends Context = Context> extends cordis.ForkScope<C> {}
export interface MainScope<C extends Context = Context> extends cordis.MainScope<C> {}

export interface Events<C extends Context = Context> extends cordis.Events<C> {}

export class Service<C extends Context = Context> extends cordis.Service<C> {}

export interface Context {
[Context.events]: Events<this>
[Context.session]: Session<this>
Expand Down Expand Up @@ -163,13 +160,17 @@ export namespace Context {
request?: Quester.Config
}

export const Config: Config.Static = Schema.intersect([
Schema.object({}),
export const Config: Config.Static = z.intersect([
z.object({}),
])

namespace Config {
export interface Static extends Schema<Config> {}
export interface Static extends z<Config> {}
}

export type Associate<P extends string, C extends Context = Context> = cordis.Context.Associate<P, C>
}

export class Service<C extends Context = Context> extends cordis.Service<C> {
static Context = Context
}

0 comments on commit 8f96625

Please sign in to comment.