diff --git a/packages/console/src/client.ts b/packages/console/src/client.ts index 281acae9..3903dbe0 100644 --- a/packages/console/src/client.ts +++ b/packages/console/src/client.ts @@ -43,8 +43,8 @@ export class Client { refresh() { Object.keys(this.ctx.root[Context.internal]).forEach(async (name) => { - if (!name.startsWith('console.')) return - const key = name.slice(8) + if (!name.startsWith('console.services.')) return + const key = name.slice(17) const service = this.ctx.get(name) as DataService if (!service) return if (await this.ctx.serial('console/intercept', this, service.options)) { diff --git a/packages/console/src/index.ts b/packages/console/src/index.ts index e8e13875..867cac70 100644 --- a/packages/console/src/index.ts +++ b/packages/console/src/index.ts @@ -10,12 +10,8 @@ export * from './client' export * from './entry' export * from './service' -type NestedServices = { - [K in keyof Console.Services as `console.${K}`]: Console.Services[K] -} - declare module 'koishi' { - interface Context extends NestedServices { + interface Context { console: Console } @@ -58,6 +54,16 @@ export abstract class Console extends Service { readonly listeners: Dict = Object.create(null) readonly clients: Dict = Object.create(null) + public services = new Proxy({} as Console.Services, { + get: (target, key, receiver) => { + if (typeof key === 'symbol') return Reflect.get(target, key, receiver) + return this.ctx.get(`console.services.${key}`) + }, + set: (target, key, value, receiver) => { + return false + }, + }) + constructor(public ctx: Context) { super(ctx, 'console', true) ctx.plugin(EntryProvider) @@ -108,11 +114,11 @@ export abstract class Console extends Service { } refresh(type: K) { - return this.ctx.get(`console.${type}`)?.refresh() + return this.ctx.get(`console.services.${type}`)?.refresh() } patch(type: K, value: Console.Services[K] extends DataService ? T : never) { - return this.ctx.get(`console.${type}`)?.patch(value as any) + return this.ctx.get(`console.services.${type}`)?.patch(value as any) } } diff --git a/packages/console/src/service.ts b/packages/console/src/service.ts index d27bfd48..f53e29d2 100644 --- a/packages/console/src/service.ts +++ b/packages/console/src/service.ts @@ -17,7 +17,7 @@ export abstract class DataService extends Service { } constructor(protected ctx: Context, protected key: keyof Console.Services, public options: DataService.Options = {}) { - super(ctx, `console.${key}`, options.immediate) + super(ctx, `console.services.${key}`, options.immediate) } start() {