Skip to content

Commit

Permalink
refa: migrate framework API
Browse files Browse the repository at this point in the history
1. HTTP.Error.is -> ctx.http.isError
2. @satorijs/satori -> @satorijs/core
  • Loading branch information
shigma committed May 11, 2024
1 parent a3a6fd0 commit 5172e2e
Show file tree
Hide file tree
Showing 128 changed files with 320 additions and 288 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
### Basic usage

```ts
import { Context } from '@satorijs/satori'
import { Context } from '@satorijs/core'
import discord from '@satorijs/adapter-discord'

// create a new context
Expand All @@ -54,7 +54,7 @@ await ctx.start()
### Specifying protocol

```ts
import { Context } from '@satorijs/satori'
import { Context } from '@satorijs/core'
import router from '@cordisjs/plugin-server'
import telegram from '@satorijs/adapter-telegram'

Expand Down
2 changes: 0 additions & 2 deletions adapters/dingtalk/.npmignore

This file was deleted.

7 changes: 4 additions & 3 deletions adapters/dingtalk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "@satorijs/adapter-dingtalk",
"description": "DingTalk (钉钉) Adapter for Satorijs",
"version": "2.3.1",
"main": "lib/index.js",
"type": "module",
"main": "lib/index.cjs",
"typings": "lib/index.d.ts",
"files": [
"lib",
Expand Down Expand Up @@ -30,10 +31,10 @@
"chat"
],
"devDependencies": {
"@cordisjs/plugin-server": "^0.2.0",
"@cordisjs/plugin-server": "^0.2.1",
"@cordisjs/plugin-server-temp": "^0.4.0"
},
"peerDependencies": {
"@satorijs/satori": "^3.7.0"
"@satorijs/core": "^3.7.0"
}
}
14 changes: 7 additions & 7 deletions adapters/dingtalk/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bot, Context, Quester, Schema } from '@satorijs/satori'
import { Bot, Context, HTTP, Schema } from '@satorijs/core'
import { HttpServer } from './http'
import { DingtalkMessageEncoder } from './message'
import { WsClient } from './ws'
Expand All @@ -9,8 +9,8 @@ export class DingtalkBot<C extends Context = Context> extends Bot<C, DingtalkBot
static MessageEncoder = DingtalkMessageEncoder
static inject = ['http']

public oldHttp: Quester
public http: Quester
public oldHttp: HTTP
public http: HTTP
public internal: Internal
private refreshTokenTimer: NodeJS.Timeout

Expand Down Expand Up @@ -108,8 +108,8 @@ export namespace DingtalkBot {
protocol: string
appkey: string
agentId?: number
api: Quester.Config
oldApi: Quester.Config
api: HTTP.Config
oldApi: HTTP.Config
}

export const Config: Schema<Config> = Schema.intersect([
Expand All @@ -122,8 +122,8 @@ export namespace DingtalkBot {
secret: Schema.string().required().description('机器人密钥。'),
agentId: Schema.number().description('AgentId'),
appkey: Schema.string().required(),
api: Quester.createConfig('https://api.dingtalk.com/v1.0/'),
oldApi: Quester.createConfig('https://oapi.dingtalk.com/'),
api: HTTP.createConfig('https://api.dingtalk.com/v1.0/'),
oldApi: HTTP.createConfig('https://oapi.dingtalk.com/'),
}),
WsClient.Options,
])
Expand Down
2 changes: 1 addition & 1 deletion adapters/dingtalk/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Adapter, Context, Logger } from '@satorijs/satori'
import { Adapter, Context, Logger } from '@satorijs/core'
import {} from '@cordisjs/plugin-server'
import { DingtalkBot } from './bot'
import crypto from 'node:crypto'
Expand Down
10 changes: 5 additions & 5 deletions adapters/dingtalk/src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Dict, Quester } from '@satorijs/satori'
import { Dict, HTTP } from '@satorijs/core'
import { DingtalkBot } from './bot'

export class Internal {
constructor(private bot: DingtalkBot) { }

static define(routes: Dict<Partial<Record<Quester.Method, Record<string, boolean>>>>) {
static define(routes: Dict<Partial<Record<HTTP.Method, Record<string, boolean>>>>) {
for (const path in routes) {
for (const key in routes[path]) {
const method = key as Quester.Method
const method = key as HTTP.Method
for (const name of Object.keys(routes[path][method])) {
const isOldApi = routes[path][method][name]
Internal.prototype[name] = async function (this: Internal, ...args: any[]) {
Expand All @@ -16,7 +16,7 @@ export class Internal {
if (!args.length) throw new Error(`too few arguments for ${path}, received ${raw}`)
return args.shift()
})
const config: Quester.RequestConfig = {}
const config: HTTP.RequestConfig = {}
if (args.length === 1) {
if (method === 'GET' || method === 'DELETE') {
config.params = args[0]
Expand All @@ -36,7 +36,7 @@ export class Internal {
try {
return (await quester(method, url, config)).data
} catch (error) {
if (!Quester.Error.is(error) || !error.response) throw error
if (!this.bot.http.isError(error) || !error.response) throw error
throw new Error(`[${error.response.status}] ${JSON.stringify(error.response.data)}`)
}
}
Expand Down
2 changes: 1 addition & 1 deletion adapters/dingtalk/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Dict, h, MessageEncoder } from '@satorijs/satori'
import { Context, Dict, h, MessageEncoder } from '@satorijs/core'
import { DingtalkBot } from './bot'
import { SendMessageData } from './types'
import { Entry } from '@cordisjs/plugin-server-temp'
Expand Down
2 changes: 1 addition & 1 deletion adapters/dingtalk/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, h } from '@satorijs/satori'
import { Context, h } from '@satorijs/core'
import { Message } from './types'
import { DingtalkBot } from './bot'

Expand Down
2 changes: 1 addition & 1 deletion adapters/dingtalk/src/ws.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Adapter, Context, Schema } from '@satorijs/satori'
import { Adapter, Context, Schema } from '@satorijs/core'
import { DingtalkBot } from './bot'
import { decodeMessage } from './utils'

Expand Down
2 changes: 0 additions & 2 deletions adapters/discord/.npmignore

This file was deleted.

7 changes: 4 additions & 3 deletions adapters/discord/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "@satorijs/adapter-discord",
"description": "Discord Adapter for Satorijs",
"version": "4.3.1",
"main": "lib/index.js",
"version": "4.4.0",
"type": "module",
"main": "lib/index.cjs",
"typings": "lib/index.d.ts",
"files": [
"lib",
Expand Down Expand Up @@ -32,6 +33,6 @@
"chat"
],
"peerDependencies": {
"@satorijs/satori": "^3.7.0"
"@satorijs/core": "^3.7.0"
}
}
26 changes: 19 additions & 7 deletions adapters/discord/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bot, Context, Fragment, h, Quester, Schema, Universal } from '@satorijs/satori'
import { Bot, Context, Fragment, h, HTTP, Schema, Universal } from '@satorijs/core'
import * as Discord from './utils'
import { DiscordMessageEncoder } from './message'
import { Internal, Webhook } from './types'
Expand All @@ -11,7 +11,7 @@ export class DiscordBot<C extends Context = Context> extends Bot<C, DiscordBot.C
static MessageEncoder = DiscordMessageEncoder
static inject = ['http']

public http: Quester
public http: HTTP
public internal: Internal
public webhooks: Record<string, Webhook> = {}
public webhookLock: Record<string, Promise<Webhook>> = {}
Expand Down Expand Up @@ -85,10 +85,22 @@ export class DiscordBot<C extends Context = Context> extends Bot<C, DiscordBot.C
return await Discord.decodeMessage(this, data, {}, undefined, recursive)
}

async getMessageList(channelId: string, before?: string) {
const messages = await this.internal.getChannelMessages(channelId, { before, limit: 100 })
async getMessageList(
channelId: string,
messageId?: string,
direction: Universal.Direction = 'before',
limit?: number,
order: Universal.Order = 'asc',
) {
const messages = await this.internal.getChannelMessages(channelId, {
[direction]: messageId,
limit: limit && Math.min(limit, 100),
})
const data = await Promise.all(messages.reverse().map(data => Discord.decodeMessage(this, data, {}, undefined, false)))
return { data, next: data[0]?.id }
const prev = data.at(direction === 'after' ? -1 : 0)?.id
const next = data.at(direction === 'before' ? 0 : -1)?.id
if (order === 'desc') data.reverse()
return { data, prev, next }
}

async getUser(userId: string) {
Expand Down Expand Up @@ -200,7 +212,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.Options {
export interface Config extends HTTP.Config, DiscordMessageEncoder.Config, WsClient.Options {
token: string
slash?: boolean
}
Expand All @@ -214,6 +226,6 @@ export namespace DiscordBot {
}).description('功能设置'),
WsClient.Options,
DiscordMessageEncoder.Config,
Quester.createConfig('https://discord.com/api/v10'),
HTTP.createConfig('https://discord.com/api/v10'),
])
}
4 changes: 2 additions & 2 deletions adapters/discord/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Dict, h, MessageEncoder, Quester, Schema, Universal } from '@satorijs/satori'
import { Context, Dict, h, MessageEncoder, Schema, Universal } from '@satorijs/core'
import { DiscordBot } from './bot'
import { ActionRow, Button, ButtonStyles, Channel, ComponentType, Message } from './types'
import { decodeMessage, sanitize } from './utils'
Expand Down Expand Up @@ -70,7 +70,7 @@ export class DiscordMessageEncoder<C extends Context = Context> extends MessageE

return message
} catch (e) {
if (Quester.Error.is(e) && e.response) {
if (this.bot.http.isError(e) && e.response) {
if (e.response.data?.code === 10015) {
this.bot.logger.debug('webhook has been deleted, recreating..., %o', e.response.data)
if (!this.bot.webhookLock[this.channelId]) this.bot.webhooks[this.channelId] = null
Expand Down
10 changes: 5 additions & 5 deletions adapters/discord/src/types/internal.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Dict, makeArray, Quester } from '@satorijs/satori'
import { Dict, HTTP, makeArray } from '@satorijs/core'
import { DiscordBot } from '../bot'

export class Internal {
constructor(private bot: DiscordBot) {}

static define(routes: Dict<Partial<Record<Quester.Method, string | string[]>>>) {
static define(routes: Dict<Partial<Record<HTTP.Method, string | string[]>>>) {
for (const path in routes) {
for (const key in routes[path]) {
const method = key as Quester.Method
const method = key as HTTP.Method
for (const name of makeArray(routes[path][method])) {
Internal.prototype[name] = async function (this: Internal, ...args: any[]) {
const raw = args.join(', ')
const url = path.replace(/\{([^}]+)\}/g, () => {
if (!args.length) throw new Error(`too few arguments for ${path}, received ${raw}`)
return args.shift()
})
const config: Quester.RequestConfig = {}
const config: HTTP.RequestConfig = {}
if (args.length === 1) {
if (method === 'GET' || method === 'DELETE') {
config.params = args[0]
Expand All @@ -32,7 +32,7 @@ export class Internal {
this.bot.logger.debug(`${method} ${url}`, config)
return (await this.bot.http(method, url, config)).data
} catch (error) {
if (!Quester.Error.is(error) || !error.response) throw error
if (!this.bot.http.isError(error) || !error.response) throw error
throw new Error(`[${error.response.status}] ${JSON.stringify(error.response.data)}`)
}
}
Expand Down
2 changes: 1 addition & 1 deletion adapters/discord/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Dict, h, pick, Session, Universal, valueMap } from '@satorijs/satori'
import { Context, Dict, h, pick, Session, Universal, valueMap } from '@satorijs/core'
import { DiscordBot } from './bot'
import * as Discord from './types'

Expand Down
2 changes: 1 addition & 1 deletion adapters/discord/src/ws.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Adapter, Context, Schema } from '@satorijs/satori'
import { Adapter, Context, Schema } from '@satorijs/core'
import { Gateway } from './types'
import { adaptSession, decodeUser } from './utils'
import { DiscordBot } from './bot'
Expand Down
2 changes: 0 additions & 2 deletions adapters/kook/.npmignore

This file was deleted.

7 changes: 4 additions & 3 deletions adapters/kook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "@satorijs/adapter-kook",
"description": "KOOK (开黑啦) Adapter for Satorijs",
"version": "4.5.1",
"main": "lib/index.js",
"type": "module",
"main": "lib/index.cjs",
"typings": "lib/index.d.ts",
"files": [
"lib",
Expand All @@ -29,9 +30,9 @@
"chat"
],
"devDependencies": {
"@cordisjs/plugin-server": "^0.2.0"
"@cordisjs/plugin-server": "^0.2.1"
},
"peerDependencies": {
"@satorijs/satori": "^3.7.0"
"@satorijs/core": "^3.7.0"
}
}
10 changes: 5 additions & 5 deletions adapters/kook/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bot, Context, Fragment, h, Quester, Schema, Universal } from '@satorijs/satori'
import { Bot, Context, Fragment, h, HTTP, Schema, Universal } from '@satorijs/core'
import { adaptGroup, adaptMessage, adaptUser, decodeGuildMember, decodeRole, encodeRole } from './utils'
import * as Kook from './types'
import { WsClient } from './ws'
Expand All @@ -9,7 +9,7 @@ export class KookBot<C extends Context = Context, T extends KookBot.Config = Koo
static MessageEncoder = KookMessageEncoder
static inject = ['http']

http: Quester
http: HTTP
internal: Kook.Internal

constructor(ctx: C, config: T) {
Expand All @@ -28,7 +28,7 @@ export class KookBot<C extends Context = Context, T extends KookBot.Config = Koo
}
}

async request<T = any>(method: Quester.Method, path: string, data = {}, headers: any = {}): Promise<T> {
async request<T = any>(method: HTTP.Method, path: string, data = {}, headers: any = {}): Promise<T> {
if (method === 'GET') {
return (await this.http.get(path, { params: data, headers })).data
} else {
Expand Down Expand Up @@ -170,7 +170,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 interface BaseConfig extends HTTP.Config, KookMessageEncoder.Config {}

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

Expand All @@ -185,6 +185,6 @@ export namespace KookBot {
HttpServer.Options,
]),
KookMessageEncoder.Config,
Quester.createConfig('https://www.kookapp.cn/api/v3'),
HTTP.createConfig('https://www.kookapp.cn/api/v3'),
] as const)
}
2 changes: 1 addition & 1 deletion adapters/kook/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Adapter, Context, Logger, sanitize, Schema } from '@satorijs/satori'
import { Adapter, Context, Logger, sanitize, Schema } from '@satorijs/core'
import {} from '@cordisjs/plugin-server'
import { KookBot } from './bot'
import { adaptSession } from './utils'
Expand Down
2 changes: 1 addition & 1 deletion adapters/kook/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, h, MessageEncoder, Schema } from '@satorijs/satori'
import { Context, h, MessageEncoder, Schema } from '@satorijs/core'
import { KookBot } from './bot'
import * as Kook from './types'

Expand Down
8 changes: 4 additions & 4 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 { Quester } from '@satorijs/satori'
import { HTTP } from '@satorijs/core'
import { KookBot } from './bot'

export enum Signal {
Expand Down Expand Up @@ -661,11 +661,11 @@ export interface Events {
}

export class Internal {
constructor(private http: Quester) {}
constructor(private http: HTTP) {}

static define(name: string, method: Quester.Method, path: string) {
static define(name: string, method: HTTP.Method, path: string) {
Internal.prototype[name] = async function (this: Internal, ...args: any[]) {
const config: Quester.RequestConfig = {}
const config: HTTP.RequestConfig = {}
if (method === 'GET' || method === 'DELETE') {
config.params = args[0]
} else {
Expand Down
2 changes: 1 addition & 1 deletion adapters/kook/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, h, isNullable, Session, Universal } from '@satorijs/satori'
import { Context, h, isNullable, Session, Universal } from '@satorijs/core'
import * as Kook from './types'
import { KookBot } from './bot'

Expand Down
2 changes: 1 addition & 1 deletion adapters/kook/src/ws.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Adapter, Context, Schema, Time, Universal } from '@satorijs/satori'
import { Adapter, Context, Schema, Time, Universal } from '@satorijs/core'
import { KookBot } from './bot'
import { adaptSession } from './utils'
import { Payload, Signal } from './types'
Expand Down
2 changes: 0 additions & 2 deletions adapters/lark/.npmignore

This file was deleted.

Loading

0 comments on commit 5172e2e

Please sign in to comment.