Commit 80f8cbef authored by nanahira's avatar nanahira

add templateParams

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