Commit acb983b3 authored by nanahira's avatar nanahira

fix things of schema register

parent 27420ac9
...@@ -18,6 +18,7 @@ import { ...@@ -18,6 +18,7 @@ import {
MetadataMap, MetadataMap,
OnContextFunction, OnContextFunction,
Selection, Selection,
SystemInjectFun,
} from './def'; } from './def';
import 'reflect-metadata'; import 'reflect-metadata';
import { App, Argv, Command, Context, FieldCollector, Session } from 'koishi'; import { App, Argv, Command, Context, FieldCollector, Session } from 'koishi';
...@@ -208,7 +209,7 @@ export function Provide( ...@@ -208,7 +209,7 @@ export function Provide(
return Metadata.appendUnique(KoishiServiceProvideSym, name); return Metadata.appendUnique(KoishiServiceProvideSym, name);
} }
const InjectSystem = (fun: (obj: PluginClass) => any) => const InjectSystem = (fun: SystemInjectFun) =>
Metadata.set(KoishiSystemInjectSym, fun, KoishiSystemInjectSymKeys); Metadata.set(KoishiSystemInjectSym, fun, KoishiSystemInjectSymKeys);
export const InjectContext = (select?: Selection) => export const InjectContext = (select?: Selection) =>
...@@ -223,9 +224,10 @@ export const InjectApp = () => InjectSystem((obj) => obj.__ctx.app); ...@@ -223,9 +224,10 @@ export const InjectApp = () => InjectSystem((obj) => obj.__ctx.app);
export const InjectConfig = (raw = false) => export const InjectConfig = (raw = false) =>
InjectSystem((obj) => (raw ? obj.__rawConfig : obj.__config)); InjectSystem((obj) => (raw ? obj.__rawConfig : obj.__config));
export const InjectLogger = (name?: string) => export const InjectLogger = (name?: string) =>
InjectSystem((obj) => InjectSystem((obj, config) =>
obj.__ctx.logger( obj.__ctx.logger(
name || name ||
config.name ||
Object.getPrototypeOf(Object.getPrototypeOf(obj))?.constructor?.name || Object.getPrototypeOf(Object.getPrototypeOf(obj))?.constructor?.name ||
'default', 'default',
), ),
......
...@@ -4,8 +4,8 @@ import { ...@@ -4,8 +4,8 @@ import {
CommandDefinitionFun, CommandDefinitionFun,
DoRegisterConfig, DoRegisterConfig,
OnContextFunction, OnContextFunction,
SystemInjectFun,
} from './interfaces'; } from './interfaces';
import { PluginClass } from '../register';
export const KoishiOnContextScope = 'KoishiOnContextScope'; export const KoishiOnContextScope = 'KoishiOnContextScope';
export const KoishiDoRegister = 'KoishiDoRegister'; export const KoishiDoRegister = 'KoishiDoRegister';
...@@ -33,5 +33,5 @@ export interface MetadataArrayMap { ...@@ -33,5 +33,5 @@ export interface MetadataArrayMap {
export interface MetadataMap { export interface MetadataMap {
KoishiDoRegister: DoRegisterConfig; KoishiDoRegister: DoRegisterConfig;
KoishiServiceInjectSym: keyof Context.Services; KoishiServiceInjectSym: keyof Context.Services;
KoishiSystemInjectSym: (obj: PluginClass) => any; KoishiSystemInjectSym: SystemInjectFun;
} }
...@@ -13,6 +13,7 @@ import { ...@@ -13,6 +13,7 @@ import {
User, User,
} from 'koishi'; } from 'koishi';
import { MetadataArrayMap, MetadataMap } from './constants'; import { MetadataArrayMap, MetadataMap } from './constants';
import { KoishiPluginRegistrationOptions, PluginClass } from '../register';
export interface Type<T = any> extends Function { export interface Type<T = any> extends Function {
new (...args: any[]): T; new (...args: any[]): T;
...@@ -142,3 +143,8 @@ export type CommandPutConfig< ...@@ -142,3 +143,8 @@ export type CommandPutConfig<
> = MappingStruct<CommandPutConfigMap, K>; > = MappingStruct<CommandPutConfigMap, K>;
export type CommandDefinitionFun = (cmd: Command) => Command; export type CommandDefinitionFun = (cmd: Command) => Command;
export type SystemInjectFun = <T = any>(
obj: PluginClass<T>,
pluginMeta: KoishiPluginRegistrationOptions<T>,
) => any;
...@@ -51,14 +51,26 @@ function getContextFromFilters(ctx: Context, filters: OnContextFunction[]) { ...@@ -51,14 +51,26 @@ function getContextFromFilters(ctx: Context, filters: OnContextFunction[]) {
return targetCtx; return targetCtx;
} }
function getSchemaFromOption<T>(schema: Schema<T> | Type<T>): Schema<T> {
if (!schema) {
return;
}
if (typeof schema !== 'function') {
return schema;
}
return schemaFromClass(schema);
}
export function KoishiPlugin<T = any>( export function KoishiPlugin<T = any>(
options: KoishiPluginRegistrationOptions<T> = {}, options: KoishiPluginRegistrationOptions<T> = {},
) { ) {
return function <C extends { new (...args: any[]): any; schema?: Schema }>( return function <
originalClass: C, C extends { new (...args: any[]): any; schema?: Schema; name?: string }
) { >(originalClass: C) {
const newClass = class extends originalClass implements PluginClass { const newClass = class extends originalClass implements PluginClass {
static schema = options.schema || originalClass.schema || undefined; static schema = getSchemaFromOption(
options.schema || originalClass.schema,
);
__ctx: Context; __ctx: Context;
__config: T; __config: T;
__rawConfig: T; __rawConfig: T;
...@@ -73,7 +85,7 @@ export function KoishiPlugin<T = any>( ...@@ -73,7 +85,7 @@ export function KoishiPlugin<T = any>(
Object.defineProperty(this, key, { Object.defineProperty(this, key, {
configurable: true, configurable: true,
enumerable: true, enumerable: true,
get: () => valueFunction(this), get: () => valueFunction(this, options),
}); });
} }
} }
......
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