Commit e44a7a46 authored by nanahira's avatar nanahira

add PluginDef 3rd param for selection back

parent b12f01f2
......@@ -21,6 +21,7 @@ import WebSocket from 'ws';
import { KoishiNestRouter } from './utility/koa-router';
import './utility/koishi.workarounds';
import './utility/koishi.declares';
import { selectContext } from 'koishi-thirdeye';
@Injectable({
scope: Scope.DEFAULT,
......@@ -85,7 +86,8 @@ export class KoishiService
this.metascan.preRegisterContext(this);
if (this.koishiModuleOptions.usePlugins) {
for (const pluginDef of this.koishiModuleOptions.usePlugins) {
this.plugin(pluginDef.plugin, pluginDef.options);
const ctx = selectContext(this, pluginDef.selection);
ctx.plugin(pluginDef.plugin, pluginDef.options);
}
}
await this.metascan.registerContext(this);
......
......@@ -13,9 +13,10 @@ import {
MetadataArrayValueMap,
MetadataGenericMap,
MetadataKey,
PluginDefinitionWithSelection,
ServiceName,
} from './koishi.interfaces';
import { Context } from 'koishi';
import { Context, Plugin } from 'koishi';
import {
ContextScopeTypes,
getContextProvideToken,
......@@ -93,7 +94,29 @@ export const ConcatMetadata = <K extends keyof MetadataArrayValueMap>(
// Export all koishi-decorator decorators
export * from 'koishi-thirdeye/dist/src/decorators/common';
export { PluginDef } from 'koishi-thirdeye';
import {
PluginDef as _pluginDef,
PluginRegistrar,
Selection,
} from 'koishi-thirdeye';
export function PluginDef(
name: string,
options?: any,
selection?: Selection,
): PluginRegistrar.PluginDefinitionName & { selection?: Selection };
export function PluginDef<T extends Plugin>(
plugin: T,
options?: PluginRegistrar.PluginOptions<T>,
selection?: Selection,
): PluginDefinitionWithSelection<T>;
export function PluginDef<T extends Plugin>(
plugin: T,
options?: PluginRegistrar.PluginOptions<T>,
selection: Selection = {},
): PluginDefinitionWithSelection<T> {
return { ..._pluginDef(plugin, options), selection };
}
// Service
......
import { ModuleMetadata, Provider, Type } from '@nestjs/common';
import { App, Channel, Command, Context, User } from 'koishi';
import { App, Channel, Command, Context, User, Plugin } from 'koishi';
import { MetadataArrayMap, MetadataMap } from './koishi.constants';
import { PluginRegistrar, Selection } from 'koishi-thirdeye';
......@@ -14,10 +14,13 @@ export interface KoishiModuleTopOptions {
useWs?: boolean;
}
export type PluginDefinitionWithSelection<T extends Plugin> =
PluginRegistrar.PluginDefinition<Context, T> & { selection: Selection };
export interface KoishiModuleOptions
extends App.Config,
KoishiModuleTopOptions {
usePlugins?: PluginRegistrar.PluginDefinition<any>[];
usePlugins?: PluginDefinitionWithSelection<any>[];
loggerPrefix?: string;
loggerColor?: number;
moduleSelection?: KoishiModuleSelection[];
......
......@@ -127,6 +127,12 @@ describe('Koishi in Nest.js', () => {
).toBe(false);
});
it('should response to plugin', () => {
const command = koishiApp.command('from-plugin');
expect(command).toBeDefined();
expect(command.execute({})).resolves.toBe('fooo');
});
it('should handle command error', () => {
const command = koishiApp.command('boo');
expect(command).toBeDefined();
......
......@@ -4,16 +4,21 @@ import { Argv, Context, SessionError } from 'koishi';
import {
CommandTemplate,
CommandUsage,
DefinePlugin,
OnGuild,
OnPlatform,
PutOption,
PutValue,
RegisterSchema,
SchemaProperty,
StarterPlugin,
UseCommand,
UseEvent,
} from 'koishi-thirdeye';
import {
CommandInterceptors,
InjectContextUser,
PluginDef,
UsingService,
} from '../../src/utility/koishi.decorators';
import { Test } from '@nestjs/testing';
......@@ -101,6 +106,20 @@ export class KoishiTestService {
}
}
@RegisterSchema()
class TestingPluginConfig {
@SchemaProperty({ default: 'foo' })
foo: string;
}
@DefinePlugin()
class TestingPlugin extends StarterPlugin(TestingPluginConfig) {
@UseCommand('from-plugin')
testingCommand() {
return this.config.foo;
}
}
export function testingModule() {
return Test.createTestingModule({
imports: [
......@@ -113,6 +132,15 @@ export function testingModule() {
content: 'miiii',
},
},
usePlugins: [
PluginDef(
TestingPlugin,
{ foo: 'fooo' },
{
self: 'koishi',
},
),
],
}),
],
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