Skip to content

Commit

Permalink
refa: migrate @satorijs/router to @cordisjs/server
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 9, 2023
1 parent f951d4b commit 42366d1
Show file tree
Hide file tree
Showing 37 changed files with 47 additions and 323 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ globals:
KOISHI_CONFIG: true

extends:
- '@koishijs/eslint-config'
- '@cordisjs/eslint-config'

plugins:
- mocha
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ await ctx.start()

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

// your application will be listening http://localhost:8080
Expand Down
2 changes: 1 addition & 1 deletion adapters/dingtalk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"chat"
],
"devDependencies": {
"@satorijs/router": "^1.1.2"
"@cordisjs/server": "^0.1.3"
},
"peerDependencies": {
"@satorijs/satori": "^3.2.3"
Expand Down
2 changes: 1 addition & 1 deletion adapters/dingtalk/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Adapter, Context, Logger } from '@satorijs/satori'
import {} from '@satorijs/router'
import {} from '@cordisjs/server'
import { DingtalkBot } from './bot'
import crypto from 'node:crypto'
import { Message } from './types'
Expand Down
2 changes: 1 addition & 1 deletion adapters/kook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"chat"
],
"devDependencies": {
"@satorijs/router": "^1.1.2"
"@cordisjs/server": "^0.1.3"
},
"peerDependencies": {
"@satorijs/satori": "^3.2.3"
Expand Down
2 changes: 1 addition & 1 deletion adapters/kook/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Adapter, Context, Logger, sanitize, Schema } from '@satorijs/satori'
import {} from '@satorijs/router'
import {} from '@cordisjs/server'
import { KookBot } from './bot'
import { adaptSession } from './utils'

Expand Down
2 changes: 1 addition & 1 deletion adapters/lark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"chat"
],
"devDependencies": {
"@satorijs/router": "^1.1.2"
"@cordisjs/server": "^0.1.3"
},
"peerDependencies": {
"@satorijs/satori": "^3.2.3"
Expand Down
2 changes: 1 addition & 1 deletion adapters/lark/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import internal from 'stream'
import { Adapter, Context, Logger, Schema } from '@satorijs/satori'
import {} from '@satorijs/router'
import {} from '@cordisjs/server'

import { FeishuBot } from './bot'
import { AllEvents } from './types'
Expand Down
2 changes: 1 addition & 1 deletion adapters/line/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"chat"
],
"devDependencies": {
"@satorijs/router": "^1.1.2"
"@cordisjs/server": "^0.1.3"
},
"peerDependencies": {
"@satorijs/satori": "^3.2.3"
Expand Down
10 changes: 5 additions & 5 deletions adapters/line/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Adapter, Context } from '@satorijs/satori'
import {} from '@satorijs/router'
import {} from '@cordisjs/server'
import crypto from 'node:crypto'
import { LineBot } from './bot'
import { WebhookRequestBody } from './types'
import { adaptSessions } from './utils'
import internal from 'stream'

export class HttpServer<C extends Context = Context> extends Adapter<C, LineBot<C>> {
static inject = ['router']
static inject = ['server']

async connect(bot: LineBot<C>) {
bot.ctx.router.post('/line', async (ctx) => {
bot.ctx.server.post('/line', async (ctx) => {
const sign = ctx.headers['x-line-signature']?.toString()
const parsed = ctx.request.body as WebhookRequestBody
const { destination } = parsed
Expand All @@ -29,7 +29,7 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, LineBot<
ctx.status = 200
ctx.body = 'ok'
})
bot.ctx.router.get('/line/assets/:self_id/:message_id', async (ctx) => {
bot.ctx.server.get('/line/assets/:self_id/:message_id', async (ctx) => {
const messageId = ctx.params.message_id
const selfId = ctx.params.self_id
const bot = this.bots.find((bot) => bot.selfId === selfId)
Expand All @@ -45,7 +45,7 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, LineBot<
})
await bot.getLogin()
await bot.internal.setWebhookEndpoint({
endpoint: bot.ctx.router.config.selfUrl + '/line',
endpoint: bot.ctx.server.config.selfUrl + '/line',
})
bot.logger.debug('listening updates %c', 'line:' + bot.selfId)
bot.online()
Expand Down
2 changes: 1 addition & 1 deletion adapters/matrix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"chat"
],
"devDependencies": {
"@satorijs/router": "^1.1.2"
"@cordisjs/server": "^0.1.3"
},
"peerDependencies": {
"@satorijs/satori": "^3.2.3"
Expand Down
2 changes: 1 addition & 1 deletion adapters/matrix/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Adapter, Context } from '@satorijs/satori'
import { Context as KoaContext } from 'koa'
import {} from '@satorijs/router'
import {} from '@cordisjs/server'
import { MatrixBot } from './bot'
import { dispatchSession } from './utils'
import { ClientEvent, M_ROOM_MEMBER } from './types'
Expand Down
2 changes: 1 addition & 1 deletion adapters/slack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"chat"
],
"devDependencies": {
"@satorijs/router": "^1.1.2"
"@cordisjs/server": "^0.1.3"
},
"peerDependencies": {
"@satorijs/satori": "^3.2.3"
Expand Down
2 changes: 1 addition & 1 deletion adapters/slack/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Adapter, Context, Schema } from '@satorijs/satori'
import {} from '@satorijs/router'
import {} from '@cordisjs/server'
import { SlackBot } from './bot'
import crypto from 'node:crypto'
import { EnvelopedEvent, SlackEvent, SocketEvent } from './types'
Expand Down
2 changes: 1 addition & 1 deletion adapters/telegram/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"chat"
],
"devDependencies": {
"@satorijs/router": "^1.1.2"
"@cordisjs/server": "^0.1.3"
},
"peerDependencies": {
"@satorijs/satori": "^3.2.3"
Expand Down
2 changes: 1 addition & 1 deletion adapters/telegram/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Adapter, Context, sanitize, Schema, trimSlash } from '@satorijs/satori'
import {} from '@satorijs/router'
import {} from '@cordisjs/server'
import { TelegramBot } from './bot'
import { handleUpdate } from './utils'
import * as Telegram from './types'
Expand Down
2 changes: 1 addition & 1 deletion adapters/wechat-official/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"chat"
],
"devDependencies": {
"@satorijs/router": "^1.1.2",
"@types/xml2js": "^0.4.13"
"@cordisjs/server": "^0.1.3",
},
"peerDependencies": {
"@satorijs/satori": "^3.2.3"
Expand Down
2 changes: 1 addition & 1 deletion adapters/wechat-official/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Adapter, Context } from '@satorijs/satori'
import {} from '@satorijs/router'
import {} from '@cordisjs/server'
import { WechatOfficialBot } from './bot'
import xml2js from 'xml2js'
import { Message } from './types'
Expand Down
2 changes: 1 addition & 1 deletion adapters/wecom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"chat"
],
"devDependencies": {
"@satorijs/router": "^1.1.2",
"@types/xml2js": "^0.4.13"
"@cordisjs/server": "^0.1.3",
},
"peerDependencies": {
"@satorijs/satori": "^3.2.3"
Expand Down
2 changes: 1 addition & 1 deletion adapters/wecom/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Adapter, Context } from '@satorijs/satori'
import {} from '@satorijs/router'
import {} from '@cordisjs/server'
import { WecomBot } from './bot'
import xml2js from 'xml2js'
import { Message } from './types'
Expand Down
2 changes: 1 addition & 1 deletion adapters/whatsapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"satori"
],
"devDependencies": {
"@satorijs/router": "^1.1.2"
"@cordisjs/server": "^0.1.3"
},
"peerDependencies": {
"@satorijs/satori": "^3.2.3"
Expand Down
2 changes: 1 addition & 1 deletion adapters/whatsapp/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Adapter, Context, Logger, Quester, remove, Schema } from '@satorijs/satori'
import {} from '@satorijs/router'
import {} from '@cordisjs/server'
import { Internal } from './internal'
import { WhatsAppBot } from './bot'
import { WebhookBody } from './types'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
},
"license": "MIT",
"devDependencies": {
"@koishijs/eslint-config": "^1.0.4",
"@types/chai": "^4.3.9",
"@cordisjs/eslint-config": "^1.0.4",
"@types/mocha": "^9.1.1",
"@types/node": "^20.8.9",
"c8": "^7.14.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
},
"dependencies": {
"@satorijs/element": "^2.6.0",
"@cordisjs/logger": "^0.1.2",
"@satorijs/protocol": "^1.1.2",
"cordis": "^3.3.1",
"cordis": "^3.4.0",
"cordis-axios": "^4.0.1",
"cosmokit": "^1.5.1",
"reggol": "^1.6.3",
"schemastery": "^3.14.1",
"ws": "^8.14.2"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
await this.context.parallel('bot-disconnect', this)
await this.adapter?.disconnect(this)
} catch (error) {
this.context.emit('internal/warning', error)
this.context.emit('internal/error', error)
} finally {
this.offline()
}
Expand Down
21 changes: 7 additions & 14 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import { Awaitable, defineProperty, Dict } from 'cosmokit'
import { Bot } from './bot'
import { Session } from './session'
import Schema from 'schemastery'
import Logger from 'reggol'
import Quester from 'cordis-axios'
import * as logger from '@cordisjs/logger'
import { Event, SendOptions } from '@satorijs/protocol'
import h from '@satorijs/element'

h.warn = new Logger('element').warn
h.warn = new logger.Logger('element').warn

// do not remove the `type` modifier
// because `esModuleInterop` is not respected by esbuild
export type { Fragment, Render } from '@satorijs/element'

export { Logger } from '@cordisjs/logger'

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

export * as Satori from '@satorijs/protocol'
export * as Universal from '@satorijs/protocol'
Expand Down Expand Up @@ -99,7 +101,7 @@ export interface Events<C extends Context = Context> extends cordis.Events<C> {
export interface Service extends Context.Associate<'service'> {}

export class Service<C extends Context = Context> extends cordis.Service<C> {
public logger: Logger
public logger: logger.Logger

constructor(ctx: C, name: string, immediate?: boolean) {
super(ctx, name, immediate)
Expand Down Expand Up @@ -140,17 +142,8 @@ export class Context extends cordis.Context {

constructor(config: Context.Config = {}) {
super(config)

this.baseDir = globalThis.process?.cwd() || ''
this.plugin(logger)
this.http = new Quester(config.request)

this.on('internal/warning', function (format, ...args) {
this.logger('app').warn(format, ...args)
})
}

logger(name: string) {
return new Logger(name, { [Context.current]: this })
}

component(name: string, component: Component<this[typeof Context.session]>, options: Component.Options = {}) {
Expand Down
2 changes: 0 additions & 2 deletions packages/router/.npmignore

This file was deleted.

48 changes: 0 additions & 48 deletions packages/router/package.json

This file was deleted.

Loading

0 comments on commit 42366d1

Please sign in to comment.