Commit d6d3d2b4 authored by nanahira's avatar nanahira

bump version

parent 4bdd6d17
This diff is collapsed.
......@@ -29,7 +29,7 @@
"author": "Nanahira <nanahira@momobako.com>",
"license": "MIT",
"devDependencies": {
"@koishijs/plugin-adapter-onebot": "^4.1.5",
"@koishijs/plugin-adapter-onebot": "^4.2.4",
"@types/jest": "^27.0.3",
"@types/lodash": "^4.14.177",
"@types/node": "^16.11.9",
......@@ -44,15 +44,15 @@
"rimraf": "^3.0.2",
"supertest": "^6.1.6",
"ts-jest": "^27.0.7",
"typescript": "^4.5.2",
"typescript": "^4.6.4",
"ws": "^8.2.3"
},
"dependencies": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
"koishi-decorators": "^2.0.4",
"koishi-decorators": "^2.1.0",
"lodash": "^4.17.21",
"minato-decorators": "^2.0.6",
"minato-decorators": "^2.0.7",
"reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.13",
"typed-reflector": "^1.0.10"
......@@ -75,7 +75,7 @@
"testEnvironment": "node"
},
"peerDependencies": {
"koishi": "^4.7.0",
"schemastery": "^3.1.0"
"koishi": "^4.7.1",
"schemastery": "^3.4.3"
}
}
......@@ -12,7 +12,6 @@ import {
KoishiSystemInjectSymKeys,
ProvideOptions,
SystemInjectFun,
TypedMethodDecorator,
} from './def';
import { TopLevelAction } from 'koishi-decorators';
import { ModelClassType, ModelRegistrar } from 'minato-decorators';
......@@ -27,14 +26,14 @@ export { PluginDef } from 'koishi-decorators';
// Service API
export function Inject(
name?: keyof Context.Services,
name?: ServiceName,
addUsing?: boolean,
): PropertyDecorator;
export function Inject(addUsing?: boolean): PropertyDecorator;
export function Inject(
...args: [(keyof Context.Services | boolean)?, boolean?]
...args: [(ServiceName | boolean)?, boolean?]
): PropertyDecorator {
let name: keyof Context.Services;
let name: ServiceName;
let addUsing = false;
if (args.length === 1) {
if (typeof args[0] === 'boolean') {
......@@ -43,7 +42,7 @@ export function Inject(
name = args[0];
}
} else if (args.length >= 2) {
name = args[0] as keyof Context.Services;
name = args[0] as ServiceName;
addUsing = args[1];
}
return (obj, key) => {
......@@ -59,7 +58,7 @@ export function Inject(
return dec(obj, key);
}
}
const serviceName = name || (key as keyof Context.Services);
const serviceName = name || (key as ServiceName);
if (addUsing) {
Metadata.appendUnique(KoishiAddUsingList, serviceName)(obj.constructor);
}
......@@ -73,10 +72,10 @@ export function Inject(
}
export function Provide(
name: keyof Context.Services,
name: ServiceName,
options?: ProvideOptions,
): ClassDecorator {
Context.service(name);
Context.service(name, options);
return Metadata.append(KoishiServiceProvideSym, {
...options,
serviceName: name,
......@@ -105,7 +104,7 @@ export const Caller = () =>
});
export function UsingService(
...services: (keyof Context.Services)[]
...services: ServiceName[]
): ClassDecorator & MethodDecorator {
return (obj, key?) => {
for (const service of services) {
......
// metadatas
import { Context, Schema } from 'koishi';
import {
Condition,
ControlType,
ControlTypeMap,
ProvideDefinition,
SystemInjectFun,
} from './interfaces';
import { ControlType, ProvideDefinition, SystemInjectFun } from './interfaces';
import { ClassType } from 'schemastery-gen';
export const KoishiServiceInjectSym = 'KoishiServiceInjectSym';
......@@ -23,13 +17,13 @@ export interface MetadataArrayMap {
KoishiServiceProvideSym: ProvideDefinition;
KoishiServiceInjectSymKeys: string;
KoishiSystemInjectSymKeys: string;
KoishiAddUsingList: keyof Context.Services;
KoishiPartialUsing: keyof Context.Services;
KoishiAddUsingList: ServiceName;
KoishiPartialUsing: ServiceName;
KoishiControl: ControlType;
}
export interface MetadataMap {
KoishiServiceInjectSym: keyof Context.Services;
KoishiServiceInjectSym: ServiceName;
KoishiSystemInjectSym: SystemInjectFun;
KoishiPredefineSchema: Schema | ClassType<any>;
KoishiPredefineName: string;
......
......@@ -7,12 +7,14 @@ export * from 'koishi-decorators/dist/src/def/interfaces';
export type SystemInjectFun = <T = any>(obj: PluginMeta<T>) => any;
export interface ProvideOptions {
export type ServiceName = keyof Context;
export interface ProvideOptions extends Context.ServiceOptions {
immediate?: boolean;
}
export interface ProvideDefinition extends ProvideOptions {
serviceName: keyof Context.Services;
serviceName: ServiceName;
}
export type Condition<R, T = any, Ext extends any[] = []> = (
......
import { MapPluginToConfig, PluginClass } from '../def';
import { Dict } from 'koishi';
import { AnyClass, ClassType, Mixin } from 'schemastery-gen';
import { ClassType, Mixin } from 'schemastery-gen';
import { MappingPluginBase } from './mapping-base';
import { CreatePluginFactory } from '../plugin-factory';
import _ from 'lodash';
......
......@@ -69,9 +69,7 @@ export function MultiInstancePlugin<
const plugin = factory(outerConfig);
if (innerPlugin['using']) {
UsingService(...(innerPlugin['using'] as (keyof Context.Services)[]))(
plugin,
);
UsingService(...(innerPlugin['using'] as ServiceName[]))(plugin);
}
return plugin;
......
import { Context, Plugin, Schema, WebSocketLayer } from 'koishi';
import {
Condition,
ControlType,
KoishiAddUsingList,
KoishiPartialUsing,
......@@ -22,7 +21,7 @@ export interface KoishiPluginRegistrationOptions<T = any> {
name?: string;
schema?: Schema<T> | Type<T>;
Config?: Schema<T> | Type<T>;
using?: (keyof Context.Services)[];
using?: ServiceName[];
}
export interface PluginMeta<T = any> {
......@@ -124,6 +123,8 @@ export function DefinePlugin<T>(
return this.__ctx[name];
},
set: (val: any) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.__ctx[name] = val;
},
});
......@@ -234,6 +235,8 @@ export function DefinePlugin<T>(
(serviceDef) => !serviceDef.immediate === !immediate,
);
for (const key of providingServices) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.__ctx[key.serviceName] = this as any;
}
}
......@@ -242,6 +245,8 @@ export function DefinePlugin<T>(
const providingServices = this._getProvidingServices();
for (const key of providingServices) {
if (this.__ctx[key.serviceName] === (this as never)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.__ctx[key.serviceName] = null;
}
}
......
......@@ -28,7 +28,7 @@ describe('InjectUsing', () => {
it('Should include injected using services', () => {
expect(MyPlugin.name).toBe('foo-plugin');
expect(MyPlugin['Config']).toEqual(Config);
const usingList = (MyPlugin as any).using as (keyof Context.Services)[];
const usingList = (MyPlugin as any).using as ServiceName[];
expect(usingList).toBeInstanceOf(Array);
expect(usingList.length).toEqual(5);
expect(usingList.includes('database')).toEqual(true);
......
......@@ -35,7 +35,7 @@ describe('Scope', () => {
} as Session;
app.plugin(MyClass);
const methodCtx = app.command('foo').context;
const methodCtx = app.command('foo').ctx;
expect(methodCtx.filter(correctSession)).toBe(true);
expect(methodCtx.filter(wrongSession1)).toBe(false);
......
......@@ -60,7 +60,7 @@ class MyConsumer {
eagerPongResult: string;
@UseEvent('service')
async onService(name: keyof Context.Services) {
async onService(name: ServiceName) {
if (name === 'myProvider') {
this.pongResult = this.myProvider.ping();
} else if (name === 'myEagerProvider') {
......@@ -83,7 +83,7 @@ class MyUsingConsumer {
eagerPongResult: string;
@UseEvent('service')
async onService(name: keyof Context.Services) {
async onService(name: ServiceName) {
if (name === 'myProvider') {
this.pongResult = this.myProvider.ping();
} else if (name === 'myEagerProvider') {
......
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