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