Commit b0e3c29b authored by nanahira's avatar nanahira

fix command in service

parent e37410c8
...@@ -549,6 +549,10 @@ export class AppModule {} ...@@ -549,6 +549,10 @@ export class AppModule {}
## 更新历史 ## 更新历史
### 1.5
* 增加了指令拦截器。
### 1.4.2 ### 1.4.2
* `KoishiWsAdapter`: 支持正则表达式路径。 * `KoishiWsAdapter`: 支持正则表达式路径。
......
import { App, Command, Context, Router } from 'koishi'; import { App, Argv, Command, Context, Router } from 'koishi';
import { import {
Inject, Inject,
Injectable, Injectable,
...@@ -24,6 +24,7 @@ import { Filter, ReplacedContext } from './utility/replaced-context'; ...@@ -24,6 +24,7 @@ import { Filter, ReplacedContext } from './utility/replaced-context';
export class KoishiService export class KoishiService
extends App extends App
implements OnModuleInit, OnApplicationBootstrap, OnModuleDestroy { implements OnModuleInit, OnApplicationBootstrap, OnModuleDestroy {
private globalInterceptors: KoishiCommandInterceptorRegistration[];
constructor( constructor(
@Inject(KOISHI_MODULE_OPTIONS) @Inject(KOISHI_MODULE_OPTIONS)
private readonly koishiModuleOptions: KoishiModuleOptions, private readonly koishiModuleOptions: KoishiModuleOptions,
...@@ -35,6 +36,7 @@ export class KoishiService ...@@ -35,6 +36,7 @@ export class KoishiService
...koishiModuleOptions, ...koishiModuleOptions,
port: 0, port: 0,
}); });
this.globalInterceptors = this.globalInterceptors;
this.router = new Router(); this.router = new Router();
this._nestKoaTmpInstance.use(KoaBodyParser()); this._nestKoaTmpInstance.use(KoaBodyParser());
this._nestKoaTmpInstance.use(this.router.routes()); this._nestKoaTmpInstance.use(this.router.routes());
...@@ -88,7 +90,7 @@ export class KoishiService ...@@ -88,7 +90,7 @@ export class KoishiService
interceptors: KoishiCommandInterceptorRegistration[] = [], interceptors: KoishiCommandInterceptorRegistration[] = [],
): Context { ): Context {
return new ReplacedContext(filter, this, null, [ return new ReplacedContext(filter, this, null, [
...(this.koishiModuleOptions.globalInterceptors || []), ...this.globalInterceptors,
...interceptors, ...interceptors,
]); ]);
} }
...@@ -119,4 +121,21 @@ export class KoishiService ...@@ -119,4 +121,21 @@ export class KoishiService
const filter = typeof arg === 'function' ? arg : arg.filter; const filter = typeof arg === 'function' ? arg : arg.filter;
return this.cloneContext((s) => this.filter(s) && !filter(s)); return this.cloneContext((s) => this.filter(s) && !filter(s));
} }
command<D extends string>(
def: D,
config?: Command.Config,
): Command<never, never, Argv.ArgumentType<D>>;
command<D extends string>(
def: D,
desc: string,
config?: Command.Config,
): Command<never, never, Argv.ArgumentType<D>>;
command(def: string, ...args: [Command.Config?] | [string, Command.Config?]) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const cmd = super.command(def, ...args);
this.addInterceptors(cmd, this.globalInterceptors);
return cmd;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment