Skip to content

Commit

Permalink
update inject api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 28, 2023
1 parent cf64b1e commit 7041cdc
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 43 deletions.
9 changes: 3 additions & 6 deletions .vitepress/config/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
}, {
"text": "开发",
"items": [{
"text": "指南",
"text": "开发指南",
"link": "/guide/"
}, {
"text": "进阶",
"text": "进阶开发指南",
"link": "/cookbook/"
}, {
"text": "API",
"text": "API 参考",
"link": "/api/"
}, {
"text": "演练场:配置构型",
Expand Down Expand Up @@ -463,9 +463,6 @@
"items": [{
"text": "插件市场",
"link": "/market/"
}, {
"text": "服务类插件导航",
"link": "/market/service.md"
}, {
"text": "官方插件一览",
"link": "/plugins/"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"devDependencies": {
"@koishijs/components": "^1.4.8",
"@koishijs/core": "^4.15.2",
"@koishijs/core": "^4.15.3",
"@koishijs/dns": "^1.0.1",
"@koishijs/market": "^4.1.6",
"@koishijs/registry": "^6.0.4",
Expand All @@ -23,7 +23,7 @@
"cross-env": "^7.0.3",
"element-plus": "2.4.0",
"esbuild-register": "^3.4.2",
"koishi": "^4.15.2",
"koishi": "^4.15.3",
"marked-vue": "^1.2.3",
"typescript": "^5.1.6",
"vitepress": "1.0.0-rc.4",
Expand Down
14 changes: 7 additions & 7 deletions zh-CN/guide/adapter/adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ if (session) this.dispatch(session)

```ts
export class HttpServer<C extends Context> extends Adapter<C, LineBot<C>> {
constructor(ctx: C) {
static inject = ['router']

constructor(public ctx: C) {
super()

ctx.router.post('/line', async (ctx) => {
Expand All @@ -110,10 +112,9 @@ export class HttpServer<C extends Context> extends Adapter<C, LineBot<C>> {
}

async start(bot: LineBot) {
const user = await this.getSelf()
Object.assign(this, user)
await this.getLogin()
await bot.internal.setWebhookEndpoint({
endpoint: bot.ctx.root.config.selfUrl + '/line',
endpoint: this.ctx.router.config.selfUrl + '/line',
})
}
}
Expand All @@ -122,10 +123,9 @@ export class HttpServer<C extends Context> extends Adapter<C, LineBot<C>> {
任何一个适配器都需要通过 `start()``stop()` 方法来控制机器人的启动和停止 (你在前一个例子中没有看到这两个方法,只是因为 `WsClient` 已经内置了实现)。在这个例子中,我们通过内部接口对机器人数据做了初始化,并设置了 Webhook 回调地址:

```ts
const user = await this.getSelf()
Object.assign(this, user)
await this.getLogin()
await bot.internal.setWebhookEndpoint({
endpoint: bot.ctx.root.config.selfUrl + '/line',
endpoint: this.ctx.router.config.selfUrl + '/line',
})
```

Expand Down
2 changes: 2 additions & 0 deletions zh-CN/guide/adapter/message.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ Telegram 是另一种特殊情况。尽管其提供的资源链接是可用的

```ts
class LarkAdapter {
static inject = ['router']

constructor(ctx: Context) {
ctx.router.get('/lark/assets/:message_id/:key', async (ctx) => {
const key = ctx.params.key
Expand Down
4 changes: 2 additions & 2 deletions zh-CN/guide/console/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CustomProvider extends DataService<string[]> {
}

export const name = 'my-plugin'
export const using = ['console'] as const
export const inject = ['console'] as const

export function apply(ctx: Context) {
ctx.plugin(CustomProvider)
Expand Down Expand Up @@ -86,7 +86,7 @@ declare module '@koishijs/plugin-console' {
}

export const name = 'my-plugin'
export const using = ['console'] as const
export const inject = ['console'] as const

export function apply(ctx: Context) {
ctx.console.addListener('get-greeting', () => {
Expand Down
6 changes: 3 additions & 3 deletions zh-CN/guide/plugin/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function apply(ctx: Context) {

```ts title=plugin-dialogue.ts
export const name = 'dialogue'
export const using = ['database']
export const inject = ['database']

export function apply(ctx: Context) {
// 你可以立即访问数据库服务
Expand Down Expand Up @@ -106,7 +106,7 @@ ctx.plugin({
export const name = 'dialogue'

// 对于整体依赖的服务,使用 using 属性声明依赖关系
export const using = ['database']
export const inject = ['database']

export function apply(ctx: Context) {
ctx.command('dialogue').action((_, content) => {
Expand Down Expand Up @@ -339,7 +339,7 @@ class Console extends Service {
import {} from 'koishi-plugin-puppeteer'

// 通过 using 属性声明依赖,并通过 ctx 来访问服务
export const using = ['puppeteer']
export const inject = ['puppeteer']
export function apply(ctx: Context) {
ctx.puppeteer.render()
}
Expand Down
23 changes: 0 additions & 23 deletions zh-CN/market/service.md

This file was deleted.

0 comments on commit 7041cdc

Please sign in to comment.