Commit 80f8cbef authored by nanahira's avatar nanahira

add templateParams

parent b5f2d7d7
......@@ -22,6 +22,8 @@ import { KoishiExceptionHandlerService } from '../koishi-exception-handler/koish
@Injectable()
export class KoishiMetascanService {
private readonly templateParams: any;
constructor(
private readonly metadataScanner: MetadataScanner,
private readonly moduleRef: ModuleRef,
......@@ -32,7 +34,9 @@ export class KoishiMetascanService {
private readonly koishiModuleOptions: KoishiModuleOptions,
private readonly intercepterManager: KoishiInterceptorManagerService,
private readonly exceptionHandler: KoishiExceptionHandlerService,
) {}
) {
this.templateParams = koishiModuleOptions.templateParams || {};
}
addInterceptors(
command: Command,
......@@ -46,7 +50,7 @@ export class KoishiMetascanService {
instance: Record<string, any>,
methodKey: string,
) {
const registrar = new Registrar(instance);
const registrar = new Registrar(instance, undefined, this.templateParams);
const baseContext = registrar.getScopeContext(ctx, methodKey, false);
const result = registrar.register(baseContext, methodKey, false);
if (result.type === 'command') {
......@@ -165,7 +169,11 @@ export class KoishiMetascanService {
return Promise.all(
this.runForEachProvider(ctx, (providerCtx, instance) => {
this.scanInstanceForProvidingContextService(providerCtx, instance);
const registrar = new Registrar(instance);
const registrar = new Registrar(
instance,
undefined,
this.templateParams,
);
registrar.performTopActions(providerCtx);
return Promise.all(
registrar
......
......@@ -23,6 +23,7 @@ export interface KoishiModuleOptions
moduleSelection?: KoishiModuleSelection[];
globalInterceptors?: KoishiCommandInterceptorRegistration[];
actionErrorMessage?: string;
templateParams?: any;
}
export interface KoishiModuleOptionsFactory {
......
......@@ -122,4 +122,10 @@ describe('Koishi in Nest.js', () => {
'pee!',
);
});
it('should work on template', () => {
const command = koishiApp.command('mii');
expect(command).toBeDefined();
expect(command.execute({ options: {} })).resolves.toBe('miiii');
});
});
......@@ -6,6 +6,7 @@ import {
OnGuild,
OnPlatform,
PutOption,
PutValue,
UseCommand,
} from 'koishi-decorators';
import { CommandInterceptors } from '../../src/utility/koishi.decorators';
......@@ -67,6 +68,11 @@ class KoishiTestService {
async onMoo() {
return 'zzzz';
}
@UseCommand('{{abstract.name}}')
async onAbstract(@PutValue('{{abstract.content}}') content: string) {
return content;
}
}
export function testingModule() {
......@@ -75,6 +81,12 @@ export function testingModule() {
KoishiModule.register({
useWs: true,
globalInterceptors: [PeeInterceptor],
templateParams: {
abstract: {
name: 'mii',
content: 'miiii',
},
},
}),
],
providers: [
......
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