Skip to content

Commit

Permalink
refa: declare events for cordis
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 3, 2024
1 parent 1835b5e commit 37aa596
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 62 deletions.
9 changes: 6 additions & 3 deletions adapters/discord/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Context } from '@satorijs/core'
import { DiscordBot } from './bot'
import * as Discord from './utils'

Expand All @@ -14,14 +15,16 @@ type ParamCase<S extends string> =
? `${L extends '_' ? '-' : Lowercase<L>}${ParamCase<R>}`
: S

type DiscordEvents = {
[T in keyof Discord.GatewayEvents as `discord/${ParamCase<T>}`]: (input: Discord.GatewayEvents[T], bot: DiscordBot) => void
type DiscordEvents<C extends Context = Context> = {
[T in keyof Discord.GatewayEvents as `discord/${ParamCase<T>}`]: (input: Discord.GatewayEvents[T], bot: DiscordBot<C>) => void
}

declare module '@satorijs/core' {
interface Session {
discord?: Discord.Gateway.Payload & Discord.Internal
}
}

interface Events extends DiscordEvents {}
declare module 'cordis' {
interface Events<C> extends DiscordEvents<C> {}
}
6 changes: 4 additions & 2 deletions adapters/kook/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ export * from './utils'
export default KookBot

declare module '@satorijs/core' {
interface Events extends Kook.Events {}

interface Session {
kook?: Kook.Data & Kook.Internal
}
}

declare module 'cordis' {
interface Events<C> extends Kook.Events<C> {}
}
68 changes: 34 additions & 34 deletions adapters/kook/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-len */
import { HTTP } from '@satorijs/core'
import { Context, HTTP } from '@satorijs/core'
import { KookBot } from './bot'

export enum Signal {
Expand Down Expand Up @@ -625,39 +625,39 @@ export interface Internal {
hasPermission(permissions: number, permission: Permissions): boolean
}

export interface Events {
'kook/updated-message'(input: any, bot: KookBot): void
'kook/updated-private-message'(input: any, bot: KookBot): void
'kook/deleted-message'(input: any, bot: KookBot): void
'kook/deleted-private-message'(input: any, bot: KookBot): void
'kook/added-reaction'(input: any, bot: KookBot): void
'kook/private-added-reaction'(input: any, bot: KookBot): void
'kook/deleted-reaction'(input: any, bot: KookBot): void
'kook/private-deleted-reaction'(input: any, bot: KookBot): void
'kook/updated-channel'(input: any, bot: KookBot): void
'kook/deleted-channel'(input: any, bot: KookBot): void
'kook/pinned-message'(input: any, bot: KookBot): void
'kook/unpinned-message'(input: any, bot: KookBot): void
'kook/joined-guild'(input: any, bot: KookBot): void
'kook/exited-guild'(input: any, bot: KookBot): void
'kook/updated-guild'(input: any, bot: KookBot): void
'kook/deleted-guild'(input: any, bot: KookBot): void
'kook/self-joined-guild'(input: any, bot: KookBot): void
'kook/self-exited-guild'(input: any, bot: KookBot): void
'kook/update-guild-member'(input: any, bot: KookBot): void
'kook/guild-member-online'(input: any, bot: KookBot): void
'kook/guild-member-offline'(input: any, bot: KookBot): void
'kook/added-role'(input: any, bot: KookBot): void
'kook/deleted-role'(input: any, bot: KookBot): void
'kook/updated-role'(input: any, bot: KookBot): void
'kook/added-block-list'(input: any, bot: KookBot): void
'kook/deleted-block-list'(input: any, bot: KookBot): void
'kook/added-emoji'(input: any, bot: KookBot): void
'kook/updated-emoji'(input: any, bot: KookBot): void
'kook/joined-channel'(input: any, bot: KookBot): void
'kook/exited-channel'(input: any, bot: KookBot): void
'kook/user-updated'(input: any, bot: KookBot): void
'kook/message-btn-click'(input: any, bot: KookBot): void
export interface Events<C extends Context = Context> {
'kook/updated-message'(input: any, bot: KookBot<C>): void
'kook/updated-private-message'(input: any, bot: KookBot<C>): void
'kook/deleted-message'(input: any, bot: KookBot<C>): void
'kook/deleted-private-message'(input: any, bot: KookBot<C>): void
'kook/added-reaction'(input: any, bot: KookBot<C>): void
'kook/private-added-reaction'(input: any, bot: KookBot<C>): void
'kook/deleted-reaction'(input: any, bot: KookBot<C>): void
'kook/private-deleted-reaction'(input: any, bot: KookBot<C>): void
'kook/updated-channel'(input: any, bot: KookBot<C>): void
'kook/deleted-channel'(input: any, bot: KookBot<C>): void
'kook/pinned-message'(input: any, bot: KookBot<C>): void
'kook/unpinned-message'(input: any, bot: KookBot<C>): void
'kook/joined-guild'(input: any, bot: KookBot<C>): void
'kook/exited-guild'(input: any, bot: KookBot<C>): void
'kook/updated-guild'(input: any, bot: KookBot<C>): void
'kook/deleted-guild'(input: any, bot: KookBot<C>): void
'kook/self-joined-guild'(input: any, bot: KookBot<C>): void
'kook/self-exited-guild'(input: any, bot: KookBot<C>): void
'kook/update-guild-member'(input: any, bot: KookBot<C>): void
'kook/guild-member-online'(input: any, bot: KookBot<C>): void
'kook/guild-member-offline'(input: any, bot: KookBot<C>): void
'kook/added-role'(input: any, bot: KookBot<C>): void
'kook/deleted-role'(input: any, bot: KookBot<C>): void
'kook/updated-role'(input: any, bot: KookBot<C>): void
'kook/added-block-list'(input: any, bot: KookBot<C>): void
'kook/deleted-block-list'(input: any, bot: KookBot<C>): void
'kook/added-emoji'(input: any, bot: KookBot<C>): void
'kook/updated-emoji'(input: any, bot: KookBot<C>): void
'kook/joined-channel'(input: any, bot: KookBot<C>): void
'kook/exited-channel'(input: any, bot: KookBot<C>): void
'kook/user-updated'(input: any, bot: KookBot<C>): void
'kook/message-btn-click'(input: any, bot: KookBot<C>): void
}

export class Internal {
Expand Down
10 changes: 6 additions & 4 deletions adapters/line/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hyphenate } from '@satorijs/core'
import { Context, hyphenate } from '@satorijs/core'
import { LineBot } from './bot'
import * as Line from './types'

Expand All @@ -9,16 +9,18 @@ export * from './utils'
export * from './http'
export * from './message'

type LineEvents = {
[P in Line.WebhookEvent['type'] as `line/${hyphenate<P>}`]: (data: Line.WebhookEvent & { type: P }, bot: LineBot) => void
type LineEvents<C extends Context = Context> = {
[P in Line.WebhookEvent['type'] as `line/${hyphenate<P>}`]: (data: Line.WebhookEvent & { type: P }, bot: LineBot<C>) => void
}

declare module '@satorijs/core' {
interface Session {
line?: Line.WebhookEvent & Line.Internal
}
}

interface Events extends LineEvents {}
declare module 'cordis' {
interface Events<C> extends LineEvents<C> {}
}

export default LineBot
9 changes: 6 additions & 3 deletions adapters/telegram/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Context } from '@satorijs/core'
import { TelegramBot } from './bot'
import * as Telegram from './types'

Expand All @@ -16,14 +17,16 @@ type ParamCase<S extends string> =
? `${L extends '_' ? '-' : Lowercase<L>}${ParamCase<R>}`
: S

type TelegramEvents = {
[T in Exclude<keyof Telegram.Update, 'update_id'> as `telegram/${ParamCase<T>}`]: (input: Telegram.Update[T], bot: TelegramBot) => void
type TelegramEvents<C extends Context = Context> = {
[T in Exclude<keyof Telegram.Update, 'update_id'> as `telegram/${ParamCase<T>}`]: (input: Telegram.Update[T], bot: TelegramBot<C>) => void
}

declare module '@satorijs/core' {
interface Session {
telegram?: Telegram.Update & Telegram.Internal
}
}

interface Events extends TelegramEvents {}
declare module 'cordis' {
interface Events<C> extends TelegramEvents<C> {}
}
2 changes: 2 additions & 0 deletions adapters/whatsapp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ declare module '@satorijs/core' {
interface Session {
whatsapp?: Change
}
}

declare module 'cordis' {
interface Events {
'whatsapp/messages'(messages: MessageValue): void
}
Expand Down
14 changes: 1 addition & 13 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context, Logger, Service, z } from 'cordis'
import { Awaitable, defineProperty, Dict, makeArray, remove } from 'cosmokit'
import { Awaitable, Dict, makeArray, remove } from 'cosmokit'
import { Bot } from './bot'
import { Session } from './session'
import { HTTP } from '@cordisjs/plugin-http'
Expand Down Expand Up @@ -78,17 +78,10 @@ declare module 'cordis' {

declare module '@cordisjs/plugin-http' {
namespace HTTP {
export const Config: z<Config>
export function createConfig(this: typeof HTTP, endpoint?: string | boolean): z<Config>
}
}

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

HTTP.createConfig = function createConfig(this, endpoint) {
return z.object({
endpoint: z.string().role('link').description('要连接的服务器地址。')
Expand All @@ -109,11 +102,6 @@ export namespace Component {

export type GetSession<C extends Context> = C[typeof Context.session]

type CordisEvents<C extends Context> = import('cordis').Events<C>

// FIXME remove in the future
export interface Events<C extends Context = Context> extends CordisEvents<C> {}

class SatoriContext extends Context {
constructor(config?: any) {
super(config)
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class SatoriServer extends Service<SatoriServer.Config> {
return
}

const proxyUrls = ctx.bots.flatMap(bot => bot.proxyUrls, 1)
const proxyUrls = ctx.bots.flatMap(bot => bot.proxyUrls)
if (!proxyUrls.some(proxyUrl => url.startsWith(proxyUrl))) {
koa.body = 'forbidden'
koa.status = 403
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"compilerOptions": {
"target": "es2022",
"module": "commonjs",
"module": "esnext",
"sourceMap": true,
"declaration": true,
"emitDeclarationOnly": true,
"composite": true,
"incremental": true,
"skipLibCheck": true,
"esModuleInterop": true,
"moduleResolution": "node",
"moduleResolution": "bundler",
"strictBindCallApply": true,
},
}

0 comments on commit 37aa596

Please sign in to comment.