Commit 455de00f authored by nanahira's avatar nanahira

move to plugin factory

parent 22c9700c
import { Schema } from 'koishi';
import { Awaitable, Context } from 'koishi';
import { DefinePlugin, RegisterSchema, SchemaProperty } from 'koishi-thirdeye';
import {
DefineMultiSourcePlugin,
PicResult,
PicSourceConfig,
PicSourcePlugin,
} from '../src';
DefinePlugin,
MultiInstancePlugin,
RegisterSchema,
SchemaProperty,
} from 'koishi-thirdeye';
import { PicResult, PicSourceConfig, PicSourcePlugin } from '../src';
@RegisterSchema()
class Config extends PicSourceConfig {
@SchemaProperty({ default: 'zh-CN' })
name: string;
@SchemaProperty({ default: 'https://cdn02.moecube.com:444' })
endpoint: string;
}
@DefinePlugin({ name: 'test-source', schema: Config })
class TestPicSourcePlugin extends PicSourcePlugin<Config> {
@DefinePlugin()
class TestPicSourcePlugin extends PicSourcePlugin(Config) {
randomPic(picTags: string[]): Awaitable<PicResult> {
return {
url: `${this.config.endpoint}/images/ygopro-images-${this.name}/${
......@@ -26,23 +28,23 @@ class TestPicSourcePlugin extends PicSourcePlugin<Config> {
}
}
export class TestMultiPicSourcePlugin extends DefineMultiSourcePlugin(
@DefinePlugin()
export class TestMultiPicSourcePlugin extends MultiInstancePlugin(
TestPicSourcePlugin,
Config,
) {}
console.log((TestMultiPicSourcePlugin['Config'] as Schema).dict.instances.type);
export default class ExtrasInDev {
constructor(ctx: Context) {
ctx.plugin(TestMultiPicSourcePlugin, {
instances: [
{
name: 'zh-CN',
// name: 'zh-CN',
isDefault: true,
endpoint: 'https://cdn02.moecube.com:444',
},
{ name: 'en-US' },
{
name: 'en-US',
},
],
});
}
......
This diff is collapsed.
......@@ -29,7 +29,7 @@
},
"homepage": "https://github.com/koishijs/koishi-plugin-pics",
"dependencies": {
"koishi-thirdeye": "^10.0.5",
"koishi-thirdeye": "^10.0.20",
"lodash": "^4.17.21"
},
"devDependencies": {
......@@ -51,13 +51,13 @@
"ts-jest": "^27.1.3",
"ts-loader": "^9.2.6",
"ts-node": "^10.5.0",
"typescript": "^4.4.4",
"typescript": "^4.6.4",
"webpack": "^5.59.1",
"webpack-cli": "^4.9.1",
"ws": "^8.3.0"
},
"peerDependencies": {
"koishi": "^4.6.1"
"koishi": "^4.6.2"
},
"jest": {
"moduleFileExtensions": [
......
......@@ -27,13 +27,11 @@ export type PicsPluginConfigLike = Partial<PicsPluginConfig>;
// For convenience of plugins
export class PicSourceConfig implements PicSourceInfo {
constructor(config: Partial<PicSourceInfo>) {}
@SchemaProperty({ type: 'string', default: [], description: '图源标签' })
tags: string[];
@SchemaProperty({ default: 1, description: '图源权重' })
weight: number;
@SchemaProperty({ default: 1, description: '图源名称', required: true })
@SchemaProperty({ description: '图源名称', required: true })
name: string;
@SchemaProperty({ description: '图源描述' })
description?: string;
......
import { Awaitable } from 'koishi';
import { ClassType, SchemaClass, SchemaProperty } from 'koishi-thirdeye';
import { Context } from 'vm';
export interface PicSourceInfo {
tags?: string[];
......@@ -25,49 +23,3 @@ export interface PicMiddlewareInfo {
name?: string;
prepend?: boolean;
}
export interface Instances<T> {
instances: T[];
}
export function ToInstancesConfig<T>(
instanceConfig: ClassType<T>,
): new () => Instances<T> {
const instanceConfigClass = class InstancesConfig {
instances: T[];
};
SchemaProperty({
type: SchemaClass(instanceConfig),
default: [],
array: true,
})(instanceConfigClass.prototype, 'instances');
return instanceConfigClass;
}
export function ClonePlugin<P extends { new (...args: any[]): any }>(
target: P,
name: string,
): P {
const pluginName = target.name;
const clonedPlugin = class extends target {};
for (const property of ['Config', 'schema', 'using']) {
Object.defineProperty(clonedPlugin, property, {
enumerable: true,
configurable: true,
writable: true,
value: target[property],
});
}
Object.defineProperty(clonedPlugin, 'name', {
enumerable: true,
configurable: true,
writable: true,
value: pluginName,
});
return clonedPlugin;
}
export type TypeFromClass<T> = T extends { new (...args: any[]): infer U }
? U
: never;
......@@ -2,6 +2,7 @@ import { Awaitable, Logger } from 'koishi';
import { PicMiddlewareConfig } from './config';
import {
BasePlugin,
CreatePluginFactory,
Inject,
InjectLogger,
LifecycleEvents,
......@@ -9,10 +10,8 @@ import {
import PicsContainer from './index';
import { PicMiddleware, PicNext } from './def';
export class PicMiddlewareBase<
C extends PicMiddlewareConfig = PicMiddlewareConfig,
>
extends BasePlugin<C>
export class BasePicMiddlewarePlugin
extends BasePlugin<PicMiddlewareConfig>
implements PicMiddleware, LifecycleEvents
{
@Inject(true)
......@@ -30,3 +29,8 @@ export class PicMiddlewareBase<
return next(url);
}
}
export const PicMiddlewarePlugin = CreatePluginFactory(
BasePicMiddlewarePlugin,
PicMiddlewareConfig,
);
import { DefinePlugin, Inject } from 'koishi-thirdeye';
import { Assets } from 'koishi';
import { PicMiddlewareBase } from '../middleware';
import { PicMiddlewareConfig } from '../config';
import { PicNext } from '../def';
import { PicMiddlewarePlugin } from '../middleware';
@DefinePlugin({ schema: PicMiddlewareConfig })
export class PicAssetsTransformMiddleware extends PicMiddlewareBase {
@DefinePlugin()
export class PicAssetsTransformMiddleware extends PicMiddlewarePlugin() {
@Inject()
private assets: Assets;
......
import { DefinePlugin } from 'koishi-thirdeye';
import { PicMiddlewareBase } from '../middleware';
import { PicMiddlewareConfig } from '../config';
import { PicMiddlewarePlugin } from '../middleware';
import { PicNext } from '../def';
@DefinePlugin({ schema: PicMiddlewareConfig })
export class PicDownloaderMiddleware extends PicMiddlewareBase {
@DefinePlugin()
export class PicDownloaderMiddleware extends PicMiddlewarePlugin() {
override async use(url: string, next: PicNext) {
const downloadedUrl = await this.pics.download(url);
return next(downloadedUrl);
......
import { Context, Awaitable, Logger, Plugin } from 'koishi';
import { Context, Awaitable, Logger } from 'koishi';
import {
PartialDeep,
InjectConfig,
Inject,
InjectLogger,
BasePlugin,
ClassType,
DefinePlugin,
LifecycleEvents,
CreatePluginFactory,
} from 'koishi-thirdeye';
import PicsContainer from '.';
import { PicSourceConfig } from './config';
import {
PicSourceInfo,
PicResult,
Instances,
ToInstancesConfig,
ClonePlugin,
TypeFromClass,
} from './def';
import { PicSourceInfo, PicResult } from './def';
export class PicSource implements PicSourceInfo {
constructor(protected ctx: Context) {}
constructor(public ctx: Context) {}
tags: string[] = [];
weight = 1;
name = 'default';
......@@ -50,19 +40,19 @@ export class PicSource implements PicSourceInfo {
}
}
export class PicSourcePlugin<C extends PicSourceConfig> extends PicSource {
constructor(ctx: Context, config: PartialDeep<C>) {
export class BasePicSourcePlugin extends PicSource {
constructor(ctx: Context, config: PartialDeep<PicSourceConfig>) {
super(ctx);
}
@InjectConfig()
protected config: C;
config: PicSourceConfig;
@Inject(true)
protected pics: PicsContainer;
pics: PicsContainer;
@InjectLogger()
protected logger: Logger;
logger: Logger;
onApply() {
this.config.applyTo(this);
......@@ -70,56 +60,7 @@ export class PicSourcePlugin<C extends PicSourceConfig> extends PicSource {
}
}
export class MultiPicSourcePlugin<C extends PicSourceConfig>
extends BasePlugin<Instances<C>>
implements LifecycleEvents
{
@Inject(true)
protected pics: PicsContainer;
@InjectLogger()
protected logger: Logger;
getSourcePlugin(): new (ctx: Context, config: any) => PicSourcePlugin<C> {
throw new Error(`Not implemented`);
}
registerSourceInstances() {
const sourcePlugin = this.getSourcePlugin();
for (const instanceConfig of this.config.instances) {
const clonedSourcePlugin = ClonePlugin(
sourcePlugin,
`${instanceConfig.name}-${instanceConfig.name}`,
);
this.ctx.plugin(clonedSourcePlugin, instanceConfig);
}
}
onApply() {
this.registerSourceInstances();
}
}
export function DefineMultiSourcePlugin<C extends ClassType<PicSourceConfig>>(
SourcePlugin: new (ctx: Context, config: TypeFromClass<C>) => PicSourcePlugin<
TypeFromClass<C>
>,
SourceConfig: C,
name = SourcePlugin.name,
): new (
context: Context,
config: Instances<PartialDeep<TypeFromClass<C>>>,
) => MultiPicSourcePlugin<TypeFromClass<C>> {
const pluginClass = class SpecificMultiPicSourcePlugin extends MultiPicSourcePlugin<
TypeFromClass<C>
> {
getSourcePlugin() {
return SourcePlugin;
}
};
return DefinePlugin({
name,
schema: ToInstancesConfig(SourceConfig),
})(pluginClass);
}
export const PicSourcePlugin = CreatePluginFactory(
BasePicSourcePlugin,
PicSourceConfig,
);
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