Commit 77abbc29 authored by nanahira's avatar nanahira

add @If

parent 746d57aa
......@@ -14,7 +14,7 @@
"koishi-decorators": "^1.1.1",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.0",
"schemastery-gen": "^3.1.1",
"typed-reflector": "^1.0.9"
},
"devDependencies": {
......@@ -5786,9 +5786,9 @@
"peer": true
},
"node_modules/schemastery-gen": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.0.tgz",
"integrity": "sha512-rVvv/c47aKU40f49+MWiM/anoZoKgo6+54jn4XL3frren/KISwklsJ9EstkVwUGeGiwxekP4ZNlWGS/3k0n+Aw==",
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.1.tgz",
"integrity": "sha512-zwNQR9VL8o0QE9kDN6I0y9c7kqdGxolVAdJqn6FVW2epW8CyVY+h1gMkAvABvP0gT3Aj/fATNj0cG+ampTgMkw==",
"dependencies": {
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
......@@ -11198,9 +11198,9 @@
"peer": true
},
"schemastery-gen": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.0.tgz",
"integrity": "sha512-rVvv/c47aKU40f49+MWiM/anoZoKgo6+54jn4XL3frren/KISwklsJ9EstkVwUGeGiwxekP4ZNlWGS/3k0n+Aw==",
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.1.tgz",
"integrity": "sha512-zwNQR9VL8o0QE9kDN6I0y9c7kqdGxolVAdJqn6FVW2epW8CyVY+h1gMkAvABvP0gT3Aj/fATNj0cG+ampTgMkw==",
"requires": {
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
......
......@@ -5,5 +5,5 @@ export class BasePlugin<C, PC = Partial<C>> {
constructor(protected ctx: Context, config: PC) {}
@InjectConfig()
protected config: C;
config: C;
}
......@@ -2,6 +2,7 @@ import 'reflect-metadata';
import { App, Context, Selection } from 'koishi';
import { Metadata } from './meta/metadata.decorators';
import {
Condition,
KoishiAddUsingList,
KoishiPartialUsing,
KoishiServiceInjectSym,
......@@ -120,3 +121,6 @@ export function UsingService(
}
};
}
export const If = <T>(func: Condition<boolean, T>): MethodDecorator =>
Metadata.append('KoishiIf', func);
// metadatas
import { Context } from 'koishi';
import { ProvideDefinition, SystemInjectFun } from './interfaces';
import { Condition, ProvideDefinition, SystemInjectFun } from './interfaces';
export const KoishiServiceInjectSym = 'KoishiServiceInjectSym';
export const KoishiServiceInjectSymKeys = 'KoishiServiceInjectSymKeys';
......@@ -18,6 +18,7 @@ export interface MetadataArrayMap {
KoishiSystemInjectSymKeys: string;
KoishiAddUsingList: keyof Context.Services;
KoishiPartialUsing: keyof Context.Services;
KoishiIf: Condition<boolean>;
}
export interface MetadataMap {
......
......@@ -15,3 +15,9 @@ export interface ProvideOptions {
export interface ProvideDefinition extends ProvideOptions {
serviceName: keyof Context.Services;
}
export type Condition<R, T = any> = (
o: T,
config: T extends { config: infer C } ? C : any,
ctx: Context,
) => R;
......@@ -113,6 +113,13 @@ export function DefinePlugin<T = any>(
methodKey,
false,
);
const conditions = reflector.getArray('KoishiIf', this, methodKey);
if (
conditions.some(
(condition) => !condition(this, this.__config as any, this.__ctx),
)
)
return;
const partialUsing = reflector.getArray(
KoishiPartialUsing,
this,
......
import { DefinePlugin } from '../src/register';
import { OnGuild, UseCommand } from 'koishi-decorators';
import { BasePlugin } from '../dist';
import { If } from '../src/decorators';
import { App } from 'koishi';
@DefinePlugin()
class MyPlugin extends BasePlugin<{ foo: boolean; bar: boolean }> {
@If<MyPlugin>((o, config, ctx) => config.foo)
@UseCommand('foo')
foo() {
return 'foo';
}
@If<MyPlugin>((o, config, ctx) => config.bar)
@UseCommand('bar')
bar() {
return 'bar';
}
}
describe('It should register conditionally', () => {
it('register command on condition', async () => {
const app = new App();
app.plugin(MyPlugin, { foo: true, bar: false });
await app.start();
const commandFoo = app.command('foo');
const commandBar = app.command('bar');
expect(await commandFoo.execute({})).toBe('foo');
expect(await commandBar.execute({})).toBeFalsy();
});
});
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