Commit 5551d867 authored by nanahira's avatar nanahira

plugin factory

parent 2eeb9e02
import { Context } from 'koishi';
import { ClassType, Mixin, SchemaProperty } from 'schemastery-gen';
import { AnyClass, ClassType, Mixin } from 'schemastery-gen';
import { PluginSchema } from './decorators';
import { PartialDeep } from './base-plugin';
export function CreatePluginFactory<C, P>(
export function CreatePluginFactory<C, IC, P extends { config: IC }>(
basePlugin: new (ctx: Context, config: C) => P,
baseConfig: ClassType<C>,
): <S>(specificConfig?: ClassType<S>) => new (ctx: Context, config: S & C) => P;
export function CreatePluginFactory<C, P>(
basePlugin: new (ctx: Context, config: C) => any,
baseConfig: ClassType<C>,
): <S>(
specificConfig?: ClassType<S>,
) => new (ctx: Context, config: S & C) => P {
return (specificConfig) => {
baseConfig: ClassType<IC>,
): <S>(specificConfig?: ClassType<S>) => new (
ctx: Context,
config: PartialDeep<S> & C,
) => Omit<C, 'config'> & {
config: IC & S;
};
export function CreatePluginFactory(
basePlugin: new (ctx: Context, config: any) => any,
baseConfig: AnyClass,
) {
return (specificConfig: AnyClass) => {
const plugin = class specificPlugin extends basePlugin {};
const config = specificConfig
? Mixin(specificConfig, baseConfig)
......
......@@ -62,7 +62,7 @@ class Outer2 extends MultiInstancePlugin(Inner2, OuterMessageConfig) {
}
}
describe('It should register multi plugin instance', () => {
describe('register multi plugin instance', () => {
it('should work on schemastery-gen', async () => {
const app = new App();
app.plugin(Outer, { msg: 'hello', instances: [{ msg: 'world' }] });
......
import { SchemaProperty } from 'schemastery-gen';
import { UseCommand } from 'koishi-decorators';
import { CreatePluginFactory } from '../src/plugin-factory';
import { App, Context } from 'koishi';
import { DefinePlugin } from '../src/register';
import { InjectConfig } from '../src/decorators';
import { BasePlugin } from '../src/base-plugin';
class MessageConfig {
@SchemaProperty()
msg: string;
getMsg() {
return this.msg;
}
}
class Base extends BasePlugin<MessageConfig> {
@UseCommand('message')
async onMessage() {
return this.config.getMsg();
}
}
const Factory = CreatePluginFactory(Base, MessageConfig);
class SpecificConfig {
@SchemaProperty()
msg2: string;
getMsg2() {
return this.msg2;
}
}
@DefinePlugin()
class SpecificPlugin extends Factory(SpecificConfig) {
@UseCommand('message2')
async onMessage2() {
return this.config.getMsg2();
}
}
describe('plugin factory', () => {
it('should register SpecificPlugin', async () => {
const app = new App();
app.plugin(SpecificPlugin, { msg: 'hello', msg2: 'world' });
await app.start();
const innerCommand = app.command('message');
const outerCommand = app.command('message2');
expect(await innerCommand.execute({})).toBe('hello');
expect(await outerCommand.execute({})).toBe('world');
});
});
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