Skip to content

Commit

Permalink
refa(satori): introduce strict typings
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 22, 2025
1 parent 3d336e2 commit 9617ec2
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@satorijs/element": "^3.1.7",
"@satorijs/protocol": "^1.6.0",
"cordis": "^3.18.1",
"cosmokit": "^1.6.3",
"cosmokit": "^1.7.0",
"path-to-regexp": "^8.2.0"
}
}
10 changes: 5 additions & 5 deletions packages/core/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clone, Dict, pick } from 'cosmokit'
import { clone, Dict, isNonNullable, pick } from 'cosmokit'
import { Context, Logger, Service } from 'cordis'
import h from '@satorijs/element'
import { Adapter } from './adapter'
Expand Down Expand Up @@ -34,12 +34,12 @@ export abstract class Bot<C extends Context = Context, T = any> {
public user = {} as User
public isBot = true
public hidden = false
public platform: string
public platform!: string
public features: string[]
public adapter?: Adapter<C, this>
public error: any
public callbacks: Dict<Function> = {}
public logger: Logger
public logger!: Logger

public _internalRouter: InternalRouter<C>

Expand Down Expand Up @@ -72,7 +72,7 @@ export abstract class Bot<C extends Context = Context, T = any> {
ctx.on('dispose', () => this.dispose())

ctx.on('interaction/button', (session) => {
const cb = this.callbacks[session.event.button.id]
const cb = this.callbacks[session.event.button!.id]
if (cb) cb(session)
})
}
Expand Down Expand Up @@ -198,7 +198,7 @@ export abstract class Bot<C extends Context = Context, T = any> {

async sendMessage(channelId: string, content: h.Fragment, referrer?: any, options?: SendOptions) {
const messages = await this.createMessage(channelId, content, referrer, options)
return messages.map(message => message.id)
return messages.map(message => message.id).filter(isNonNullable)
}

async sendPrivateMessage(userId: string, content: h.Fragment, guildId?: string, options?: SendOptions) {
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,15 @@ export namespace JsonForm {

export async function decode(body: Body) {
const type = body.headers.get('content-type')
if (type.startsWith('multipart/form-data')) {
if (type?.startsWith('multipart/form-data')) {
const response = new globalThis.Response(body.body, { headers: body.headers })
const form = await response.formData()
const json = form.get('$') as string
return load(JSON.parse(json), '$', form)
} else if (type.startsWith('application/json')) {
} else if (type?.startsWith('application/json')) {
return JSON.parse(new TextDecoder().decode(body.body))
} else {
throw new Error(`Unsupported content type: ${type}`)
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from 'cordis'
import { Bot } from './bot'
import { Message, SendOptions } from '@satorijs/protocol'
import { Channel, Message, SendOptions } from '@satorijs/protocol'
import h from '@satorijs/element'

class AggregateError extends Error {
Expand Down Expand Up @@ -33,7 +33,7 @@ export abstract class MessageEncoder<C extends Context = Context, B extends Bot<
async send(content: h.Fragment) {
this.session = this.bot.session({
type: 'send',
channel: { id: this.channelId, ...this.options.session?.event.channel },
channel: { id: this.channelId, ...this.options.session?.event.channel } as Channel,
guild: this.options.session?.event.guild,
})
for (const key in this.options.session || {}) {
Expand Down
16 changes: 9 additions & 7 deletions packages/core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ declare module '@satorijs/protocol' {
// Accessors
export interface Session {
type: string
/** @deprecated */
subtype: string
/** @deprecated */
subsubtype: string
selfId: string
platform: string
timestamp: number
userId: string
channelId: string
guildId: string
messageId: string
operatorId: string
roleId: string
quote: Message
userId?: string
channelId?: string
guildId?: string
messageId?: string
operatorId?: string
roleId?: string
quote?: Message
referrer: any
}

Expand Down
2 changes: 2 additions & 0 deletions packages/create/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"outDir": "lib",
"module": "esnext",
"moduleResolution": "bundler",
"strict": true,
"noImplicitAny": false,
},
"include": [
"src",
Expand Down
2 changes: 1 addition & 1 deletion packages/element/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
"chai-shape": "^1.1.0"
},
"dependencies": {
"cosmokit": "^1.6.3"
"cosmokit": "^1.7.0"
}
}
4 changes: 1 addition & 3 deletions packages/element/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Awaitable, Binary, camelize, defineProperty, Dict, hyphenate, is, isNullable, makeArray } from 'cosmokit'
import { Awaitable, Binary, camelize, defineProperty, Dict, hyphenate, is, isNonNullable, isNullable, makeArray } from 'cosmokit'

declare global {
namespace JSX {
Expand Down Expand Up @@ -167,8 +167,6 @@ namespace Element {
}
}

const isNonNullable = <T, >(x: T): x is NonNullable<T> => !isNullable(x)

export function toElementArray(content?: Element.Fragment) {
if (Array.isArray(content)) {
return content.map(toElement).filter(isNonNullable)
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
],
"dependencies": {
"@satorijs/element": "^3.1.7",
"cosmokit": "^1.6.3"
"cosmokit": "^1.7.0"
}
}
2 changes: 1 addition & 1 deletion packages/protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,13 @@ export type EventName =
export interface Event {
sn: number
type: string
login: Login
selfId: string
platform: string
timestamp: number
argv?: Argv
channel?: Channel
guild?: Guild
login?: Login
member?: GuildMember
message?: Message
operator?: User
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
"@satorijs/core": "^4.5.0"
},
"dependencies": {
"cosmokit": "^1.6.3"
"cosmokit": "^1.7.0"
}
}

0 comments on commit 9617ec2

Please sign in to comment.