Commit 455de00f authored by nanahira's avatar nanahira

move to plugin factory

parent 22c9700c
import { Schema } from 'koishi';
import { Awaitable, Context } from 'koishi'; import { Awaitable, Context } from 'koishi';
import { DefinePlugin, RegisterSchema, SchemaProperty } from 'koishi-thirdeye';
import { import {
DefineMultiSourcePlugin, DefinePlugin,
PicResult, MultiInstancePlugin,
PicSourceConfig, RegisterSchema,
PicSourcePlugin, SchemaProperty,
} from '../src'; } from 'koishi-thirdeye';
import { PicResult, PicSourceConfig, PicSourcePlugin } from '../src';
@RegisterSchema() @RegisterSchema()
class Config extends PicSourceConfig { class Config extends PicSourceConfig {
@SchemaProperty({ default: 'zh-CN' })
name: string;
@SchemaProperty({ default: 'https://cdn02.moecube.com:444' }) @SchemaProperty({ default: 'https://cdn02.moecube.com:444' })
endpoint: string; endpoint: string;
} }
@DefinePlugin({ name: 'test-source', schema: Config }) @DefinePlugin()
class TestPicSourcePlugin extends PicSourcePlugin<Config> { class TestPicSourcePlugin extends PicSourcePlugin(Config) {
randomPic(picTags: string[]): Awaitable<PicResult> { randomPic(picTags: string[]): Awaitable<PicResult> {
return { return {
url: `${this.config.endpoint}/images/ygopro-images-${this.name}/${ url: `${this.config.endpoint}/images/ygopro-images-${this.name}/${
...@@ -26,23 +28,23 @@ class TestPicSourcePlugin extends PicSourcePlugin<Config> { ...@@ -26,23 +28,23 @@ class TestPicSourcePlugin extends PicSourcePlugin<Config> {
} }
} }
export class TestMultiPicSourcePlugin extends DefineMultiSourcePlugin( @DefinePlugin()
export class TestMultiPicSourcePlugin extends MultiInstancePlugin(
TestPicSourcePlugin, TestPicSourcePlugin,
Config,
) {} ) {}
console.log((TestMultiPicSourcePlugin['Config'] as Schema).dict.instances.type);
export default class ExtrasInDev { export default class ExtrasInDev {
constructor(ctx: Context) { constructor(ctx: Context) {
ctx.plugin(TestMultiPicSourcePlugin, { ctx.plugin(TestMultiPicSourcePlugin, {
instances: [ instances: [
{ {
name: 'zh-CN', // name: 'zh-CN',
isDefault: true, isDefault: true,
endpoint: 'https://cdn02.moecube.com:444', endpoint: 'https://cdn02.moecube.com:444',
}, },
{ name: 'en-US' }, {
name: 'en-US',
},
], ],
}); });
} }
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
"version": "9.0.3", "version": "9.0.3",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"koishi-thirdeye": "^10.0.5", "koishi-thirdeye": "^10.0.20",
"lodash": "^4.17.21" "lodash": "^4.17.21"
}, },
"devDependencies": { "devDependencies": {
...@@ -31,13 +31,13 @@ ...@@ -31,13 +31,13 @@
"ts-jest": "^27.1.3", "ts-jest": "^27.1.3",
"ts-loader": "^9.2.6", "ts-loader": "^9.2.6",
"ts-node": "^10.5.0", "ts-node": "^10.5.0",
"typescript": "^4.4.4", "typescript": "^4.6.4",
"webpack": "^5.59.1", "webpack": "^5.59.1",
"webpack-cli": "^4.9.1", "webpack-cli": "^4.9.1",
"ws": "^8.3.0" "ws": "^8.3.0"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.6.1" "koishi": "^4.6.2"
} }
}, },
"node_modules/@ampproject/remapping": { "node_modules/@ampproject/remapping": {
...@@ -1070,13 +1070,13 @@ ...@@ -1070,13 +1070,13 @@
} }
}, },
"node_modules/@koishijs/core": { "node_modules/@koishijs/core": {
"version": "4.6.1", "version": "4.6.2",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.6.1.tgz", "resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.6.2.tgz",
"integrity": "sha512-2HzdUi76YSLFZYpPXcmEwwVyHQLPF8BL0GJR6PuCYdOYfHykzwA5NmqOGx29EyeuYzs8sS6yr25IQktxMYqsdg==", "integrity": "sha512-GM0497Bd5BDPXuzJMppJik+Zb55bbLTX14aJmkFgPW9maU9oz3ZZRW+EeMpFK449fPG83P2UsIulJHNbD5L/hQ==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"@koishijs/utils": "^5.4.1", "@koishijs/utils": "^5.4.2",
"cosmotype": "^1.0.6", "cosmotype": "^1.0.9",
"fastest-levenshtein": "^1.0.12", "fastest-levenshtein": "^1.0.12",
"ns-require": "^1.1.2" "ns-require": "^1.1.2"
}, },
...@@ -1139,9 +1139,9 @@ ...@@ -1139,9 +1139,9 @@
"peer": true "peer": true
}, },
"node_modules/@koishijs/utils": { "node_modules/@koishijs/utils": {
"version": "5.4.1", "version": "5.4.2",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.4.1.tgz", "resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.4.2.tgz",
"integrity": "sha512-M3L9KqtdTyCzQK1LxTtp7QCucc75IA2vRwnYJA1gXMAxPC3SoEjUVmsk/oNFBSknmfADdViI5mFJtUXMCpJW1g==", "integrity": "sha512-cUwrqVQqTM/F2DZO9APEDlfz2mrUe5L7b2pYmhaTi8Pggk9kT78hvBx4fnLWrHiFV3BPuikghqZSfkladFcugw==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"@koishijs/segment": "^1.1.1", "@koishijs/segment": "^1.1.1",
...@@ -5220,14 +5220,14 @@ ...@@ -5220,14 +5220,14 @@
} }
}, },
"node_modules/koishi": { "node_modules/koishi": {
"version": "4.6.1", "version": "4.6.2",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.6.1.tgz", "resolved": "https://registry.npmjs.org/koishi/-/koishi-4.6.2.tgz",
"integrity": "sha512-9NtcXrIKLN8AG9zEf3GzweSiHQubew0AWfjaci0grIxy760T77D8iLVsDbVhzmGSnc26WOR2q3CMFdRmE2igVA==", "integrity": "sha512-vgjKxSJxAOsFwBnO097d8Oo8AC0jTvdfrsAHgu8u1d3DDJkHzUr8vQEjcJjvAp411Ev5VIvswFeSpeTVWDv6VA==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"@koa/router": "^10.1.1", "@koa/router": "^10.1.1",
"@koishijs/core": "^4.6.1", "@koishijs/core": "^4.6.2",
"@koishijs/utils": "^5.4.1", "@koishijs/utils": "^5.4.2",
"@types/koa": "*", "@types/koa": "*",
"@types/koa__router": "*", "@types/koa__router": "*",
"@types/ws": "^8.5.3", "@types/ws": "^8.5.3",
...@@ -5245,9 +5245,9 @@ ...@@ -5245,9 +5245,9 @@
} }
}, },
"node_modules/koishi-decorators": { "node_modules/koishi-decorators": {
"version": "2.0.0", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-2.0.0.tgz", "resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-2.0.1.tgz",
"integrity": "sha512-YAbjOLdBxJU0DnBM7vqH+UNfSqMrejlcTEc+m++ZvuMbkUA4tHZ5iciwJFV2iPZUKSZy7GbmImNbXDmmjowF5w==", "integrity": "sha512-dDrBS4Su1NtmrkIeCdcCKsC6uaBYTRTspDdeeoPLOMuSJf6fqqXwxa2qEosvG5475MXs9SaNQDaKjh0gq/NY6A==",
"dependencies": { "dependencies": {
"@types/koa": "^2.13.4", "@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11", "@types/koa__router": "^8.0.11",
...@@ -5260,21 +5260,21 @@ ...@@ -5260,21 +5260,21 @@
} }
}, },
"node_modules/koishi-thirdeye": { "node_modules/koishi-thirdeye": {
"version": "10.0.5", "version": "10.0.20",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.5.tgz", "resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.20.tgz",
"integrity": "sha512-GzPNyJCnUNklHefi0vc0nArV+dEh1QuTddom9MQ6Sawa6nbjbnZNWKFY08eQxNR6CR87tYK93WtO+URwcBbEew==", "integrity": "sha512-aYBiQ6R4A8VcZ7xw0qfECIJvWsuEoW3MWdBH+SuWi0dcrwhvfF3naEtkC0ZGEnWOkauyW1YyOzTK/Fieikmv4w==",
"dependencies": { "dependencies": {
"@types/koa": "^2.13.4", "@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11", "@types/koa__router": "^8.0.11",
"cosmotype-decorators": "^2.0.5", "cosmotype-decorators": "^2.0.5",
"koishi-decorators": "^2.0.0", "koishi-decorators": "^2.0.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.4", "schemastery-gen": "^3.1.10",
"typed-reflector": "^1.0.10" "typed-reflector": "^1.0.10"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.6.0", "koishi": "^4.6.2",
"schemastery": "^3.1.0" "schemastery": "^3.1.0"
} }
}, },
...@@ -6444,16 +6444,16 @@ ...@@ -6444,16 +6444,16 @@
} }
}, },
"node_modules/schemastery-gen": { "node_modules/schemastery-gen": {
"version": "3.1.4", "version": "3.1.10",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.4.tgz", "resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.10.tgz",
"integrity": "sha512-zhM5A2p/SdPKe8ouObCYSOA3k5I1CpbOfSvv8DWp7kCbVnSgfOMSBCB4Yue3/D81+MAE2re93nSt7k/Tolz3mQ==", "integrity": "sha512-hwCLOICGEodCcQt+eLoEEvNhm7iSEwJB37JQ+27cHbggTry/Rq07ivXVsPw63Et74EobEaOGDVOTzJ4kIQa01w==",
"dependencies": { "dependencies": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"typed-reflector": "^1.0.10" "typed-reflector": "^1.0.10"
}, },
"peerDependencies": { "peerDependencies": {
"schemastery": "^3.3.2" "schemastery": "^3.3.3"
} }
}, },
"node_modules/semver": { "node_modules/semver": {
...@@ -7243,9 +7243,9 @@ ...@@ -7243,9 +7243,9 @@
} }
}, },
"node_modules/typescript": { "node_modules/typescript": {
"version": "4.4.4", "version": "4.6.4",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.4.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.6.4.tgz",
"integrity": "sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==", "integrity": "sha512-9ia/jWHIEbo49HfjrLGfKbZSuWo9iTMwXO+Ca3pRsSpbsMbc7/IU8NKdCZVRRBafVPGnoJeFL76ZOAA84I9fEg==",
"dev": true, "dev": true,
"bin": { "bin": {
"tsc": "bin/tsc", "tsc": "bin/tsc",
...@@ -8558,13 +8558,13 @@ ...@@ -8558,13 +8558,13 @@
} }
}, },
"@koishijs/core": { "@koishijs/core": {
"version": "4.6.1", "version": "4.6.2",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.6.1.tgz", "resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.6.2.tgz",
"integrity": "sha512-2HzdUi76YSLFZYpPXcmEwwVyHQLPF8BL0GJR6PuCYdOYfHykzwA5NmqOGx29EyeuYzs8sS6yr25IQktxMYqsdg==", "integrity": "sha512-GM0497Bd5BDPXuzJMppJik+Zb55bbLTX14aJmkFgPW9maU9oz3ZZRW+EeMpFK449fPG83P2UsIulJHNbD5L/hQ==",
"peer": true, "peer": true,
"requires": { "requires": {
"@koishijs/utils": "^5.4.1", "@koishijs/utils": "^5.4.2",
"cosmotype": "^1.0.6", "cosmotype": "^1.0.9",
"fastest-levenshtein": "^1.0.12", "fastest-levenshtein": "^1.0.12",
"ns-require": "^1.1.2" "ns-require": "^1.1.2"
} }
...@@ -8612,9 +8612,9 @@ ...@@ -8612,9 +8612,9 @@
"peer": true "peer": true
}, },
"@koishijs/utils": { "@koishijs/utils": {
"version": "5.4.1", "version": "5.4.2",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.4.1.tgz", "resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.4.2.tgz",
"integrity": "sha512-M3L9KqtdTyCzQK1LxTtp7QCucc75IA2vRwnYJA1gXMAxPC3SoEjUVmsk/oNFBSknmfADdViI5mFJtUXMCpJW1g==", "integrity": "sha512-cUwrqVQqTM/F2DZO9APEDlfz2mrUe5L7b2pYmhaTi8Pggk9kT78hvBx4fnLWrHiFV3BPuikghqZSfkladFcugw==",
"peer": true, "peer": true,
"requires": { "requires": {
"@koishijs/segment": "^1.1.1", "@koishijs/segment": "^1.1.1",
...@@ -11783,14 +11783,14 @@ ...@@ -11783,14 +11783,14 @@
} }
}, },
"koishi": { "koishi": {
"version": "4.6.1", "version": "4.6.2",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.6.1.tgz", "resolved": "https://registry.npmjs.org/koishi/-/koishi-4.6.2.tgz",
"integrity": "sha512-9NtcXrIKLN8AG9zEf3GzweSiHQubew0AWfjaci0grIxy760T77D8iLVsDbVhzmGSnc26WOR2q3CMFdRmE2igVA==", "integrity": "sha512-vgjKxSJxAOsFwBnO097d8Oo8AC0jTvdfrsAHgu8u1d3DDJkHzUr8vQEjcJjvAp411Ev5VIvswFeSpeTVWDv6VA==",
"peer": true, "peer": true,
"requires": { "requires": {
"@koa/router": "^10.1.1", "@koa/router": "^10.1.1",
"@koishijs/core": "^4.6.1", "@koishijs/core": "^4.6.2",
"@koishijs/utils": "^5.4.1", "@koishijs/utils": "^5.4.2",
"@types/koa": "*", "@types/koa": "*",
"@types/koa__router": "*", "@types/koa__router": "*",
"@types/ws": "^8.5.3", "@types/ws": "^8.5.3",
...@@ -11805,9 +11805,9 @@ ...@@ -11805,9 +11805,9 @@
} }
}, },
"koishi-decorators": { "koishi-decorators": {
"version": "2.0.0", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-2.0.0.tgz", "resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-2.0.1.tgz",
"integrity": "sha512-YAbjOLdBxJU0DnBM7vqH+UNfSqMrejlcTEc+m++ZvuMbkUA4tHZ5iciwJFV2iPZUKSZy7GbmImNbXDmmjowF5w==", "integrity": "sha512-dDrBS4Su1NtmrkIeCdcCKsC6uaBYTRTspDdeeoPLOMuSJf6fqqXwxa2qEosvG5475MXs9SaNQDaKjh0gq/NY6A==",
"requires": { "requires": {
"@types/koa": "^2.13.4", "@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11", "@types/koa__router": "^8.0.11",
...@@ -11817,17 +11817,17 @@ ...@@ -11817,17 +11817,17 @@
} }
}, },
"koishi-thirdeye": { "koishi-thirdeye": {
"version": "10.0.5", "version": "10.0.20",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.5.tgz", "resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.20.tgz",
"integrity": "sha512-GzPNyJCnUNklHefi0vc0nArV+dEh1QuTddom9MQ6Sawa6nbjbnZNWKFY08eQxNR6CR87tYK93WtO+URwcBbEew==", "integrity": "sha512-aYBiQ6R4A8VcZ7xw0qfECIJvWsuEoW3MWdBH+SuWi0dcrwhvfF3naEtkC0ZGEnWOkauyW1YyOzTK/Fieikmv4w==",
"requires": { "requires": {
"@types/koa": "^2.13.4", "@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11", "@types/koa__router": "^8.0.11",
"cosmotype-decorators": "^2.0.5", "cosmotype-decorators": "^2.0.5",
"koishi-decorators": "^2.0.0", "koishi-decorators": "^2.0.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.4", "schemastery-gen": "^3.1.10",
"typed-reflector": "^1.0.10" "typed-reflector": "^1.0.10"
} }
}, },
...@@ -12699,9 +12699,9 @@ ...@@ -12699,9 +12699,9 @@
} }
}, },
"schemastery-gen": { "schemastery-gen": {
"version": "3.1.4", "version": "3.1.10",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.4.tgz", "resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.10.tgz",
"integrity": "sha512-zhM5A2p/SdPKe8ouObCYSOA3k5I1CpbOfSvv8DWp7kCbVnSgfOMSBCB4Yue3/D81+MAE2re93nSt7k/Tolz3mQ==", "integrity": "sha512-hwCLOICGEodCcQt+eLoEEvNhm7iSEwJB37JQ+27cHbggTry/Rq07ivXVsPw63Et74EobEaOGDVOTzJ4kIQa01w==",
"requires": { "requires": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
...@@ -13261,9 +13261,9 @@ ...@@ -13261,9 +13261,9 @@
} }
}, },
"typescript": { "typescript": {
"version": "4.4.4", "version": "4.6.4",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.4.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.6.4.tgz",
"integrity": "sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==", "integrity": "sha512-9ia/jWHIEbo49HfjrLGfKbZSuWo9iTMwXO+Ca3pRsSpbsMbc7/IU8NKdCZVRRBafVPGnoJeFL76ZOAA84I9fEg==",
"dev": true "dev": true
}, },
"universalify": { "universalify": {
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
}, },
"homepage": "https://github.com/koishijs/koishi-plugin-pics", "homepage": "https://github.com/koishijs/koishi-plugin-pics",
"dependencies": { "dependencies": {
"koishi-thirdeye": "^10.0.5", "koishi-thirdeye": "^10.0.20",
"lodash": "^4.17.21" "lodash": "^4.17.21"
}, },
"devDependencies": { "devDependencies": {
...@@ -51,13 +51,13 @@ ...@@ -51,13 +51,13 @@
"ts-jest": "^27.1.3", "ts-jest": "^27.1.3",
"ts-loader": "^9.2.6", "ts-loader": "^9.2.6",
"ts-node": "^10.5.0", "ts-node": "^10.5.0",
"typescript": "^4.4.4", "typescript": "^4.6.4",
"webpack": "^5.59.1", "webpack": "^5.59.1",
"webpack-cli": "^4.9.1", "webpack-cli": "^4.9.1",
"ws": "^8.3.0" "ws": "^8.3.0"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.6.1" "koishi": "^4.6.2"
}, },
"jest": { "jest": {
"moduleFileExtensions": [ "moduleFileExtensions": [
......
...@@ -27,13 +27,11 @@ export type PicsPluginConfigLike = Partial<PicsPluginConfig>; ...@@ -27,13 +27,11 @@ export type PicsPluginConfigLike = Partial<PicsPluginConfig>;
// For convenience of plugins // For convenience of plugins
export class PicSourceConfig implements PicSourceInfo { export class PicSourceConfig implements PicSourceInfo {
constructor(config: Partial<PicSourceInfo>) {}
@SchemaProperty({ type: 'string', default: [], description: '图源标签' }) @SchemaProperty({ type: 'string', default: [], description: '图源标签' })
tags: string[]; tags: string[];
@SchemaProperty({ default: 1, description: '图源权重' }) @SchemaProperty({ default: 1, description: '图源权重' })
weight: number; weight: number;
@SchemaProperty({ default: 1, description: '图源名称', required: true }) @SchemaProperty({ description: '图源名称', required: true })
name: string; name: string;
@SchemaProperty({ description: '图源描述' }) @SchemaProperty({ description: '图源描述' })
description?: string; description?: string;
......
import { Awaitable } from 'koishi'; import { Awaitable } from 'koishi';
import { ClassType, SchemaClass, SchemaProperty } from 'koishi-thirdeye';
import { Context } from 'vm';
export interface PicSourceInfo { export interface PicSourceInfo {
tags?: string[]; tags?: string[];
...@@ -25,49 +23,3 @@ export interface PicMiddlewareInfo { ...@@ -25,49 +23,3 @@ export interface PicMiddlewareInfo {
name?: string; name?: string;
prepend?: boolean; 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'; ...@@ -2,6 +2,7 @@ import { Awaitable, Logger } from 'koishi';
import { PicMiddlewareConfig } from './config'; import { PicMiddlewareConfig } from './config';
import { import {
BasePlugin, BasePlugin,
CreatePluginFactory,
Inject, Inject,
InjectLogger, InjectLogger,
LifecycleEvents, LifecycleEvents,
...@@ -9,10 +10,8 @@ import { ...@@ -9,10 +10,8 @@ import {
import PicsContainer from './index'; import PicsContainer from './index';
import { PicMiddleware, PicNext } from './def'; import { PicMiddleware, PicNext } from './def';
export class PicMiddlewareBase< export class BasePicMiddlewarePlugin
C extends PicMiddlewareConfig = PicMiddlewareConfig, extends BasePlugin<PicMiddlewareConfig>
>
extends BasePlugin<C>
implements PicMiddleware, LifecycleEvents implements PicMiddleware, LifecycleEvents
{ {
@Inject(true) @Inject(true)
...@@ -30,3 +29,8 @@ export class PicMiddlewareBase< ...@@ -30,3 +29,8 @@ export class PicMiddlewareBase<
return next(url); return next(url);
} }
} }
export const PicMiddlewarePlugin = CreatePluginFactory(
BasePicMiddlewarePlugin,
PicMiddlewareConfig,
);
import { DefinePlugin, Inject } from 'koishi-thirdeye'; import { DefinePlugin, Inject } from 'koishi-thirdeye';
import { Assets } from 'koishi'; import { Assets } from 'koishi';
import { PicMiddlewareBase } from '../middleware';
import { PicMiddlewareConfig } from '../config';
import { PicNext } from '../def'; import { PicNext } from '../def';
import { PicMiddlewarePlugin } from '../middleware';
@DefinePlugin({ schema: PicMiddlewareConfig }) @DefinePlugin()
export class PicAssetsTransformMiddleware extends PicMiddlewareBase { export class PicAssetsTransformMiddleware extends PicMiddlewarePlugin() {
@Inject() @Inject()
private assets: Assets; private assets: Assets;
......
import { DefinePlugin } from 'koishi-thirdeye'; import { DefinePlugin } from 'koishi-thirdeye';
import { PicMiddlewareBase } from '../middleware'; import { PicMiddlewarePlugin } from '../middleware';
import { PicMiddlewareConfig } from '../config';
import { PicNext } from '../def'; import { PicNext } from '../def';
@DefinePlugin({ schema: PicMiddlewareConfig }) @DefinePlugin()
export class PicDownloaderMiddleware extends PicMiddlewareBase { export class PicDownloaderMiddleware extends PicMiddlewarePlugin() {
override async use(url: string, next: PicNext) { override async use(url: string, next: PicNext) {
const downloadedUrl = await this.pics.download(url); const downloadedUrl = await this.pics.download(url);
return next(downloadedUrl); return next(downloadedUrl);
......
import { Context, Awaitable, Logger, Plugin } from 'koishi'; import { Context, Awaitable, Logger } from 'koishi';
import { import {
PartialDeep, PartialDeep,
InjectConfig, InjectConfig,
Inject, Inject,
InjectLogger, InjectLogger,
BasePlugin, CreatePluginFactory,
ClassType,
DefinePlugin,
LifecycleEvents,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import PicsContainer from '.'; import PicsContainer from '.';
import { PicSourceConfig } from './config'; import { PicSourceConfig } from './config';
import { import { PicSourceInfo, PicResult } from './def';
PicSourceInfo,
PicResult,
Instances,
ToInstancesConfig,
ClonePlugin,
TypeFromClass,
} from './def';
export class PicSource implements PicSourceInfo { export class PicSource implements PicSourceInfo {
constructor(protected ctx: Context) {} constructor(public ctx: Context) {}
tags: string[] = []; tags: string[] = [];
weight = 1; weight = 1;
name = 'default'; name = 'default';
...@@ -50,19 +40,19 @@ export class PicSource implements PicSourceInfo { ...@@ -50,19 +40,19 @@ export class PicSource implements PicSourceInfo {
} }
} }
export class PicSourcePlugin<C extends PicSourceConfig> extends PicSource { export class BasePicSourcePlugin extends PicSource {
constructor(ctx: Context, config: PartialDeep<C>) { constructor(ctx: Context, config: PartialDeep<PicSourceConfig>) {
super(ctx); super(ctx);
} }
@InjectConfig() @InjectConfig()
protected config: C; config: PicSourceConfig;
@Inject(true) @Inject(true)
protected pics: PicsContainer; pics: PicsContainer;
@InjectLogger() @InjectLogger()
protected logger: Logger; logger: Logger;
onApply() { onApply() {
this.config.applyTo(this); this.config.applyTo(this);
...@@ -70,56 +60,7 @@ export class PicSourcePlugin<C extends PicSourceConfig> extends PicSource { ...@@ -70,56 +60,7 @@ export class PicSourcePlugin<C extends PicSourceConfig> extends PicSource {
} }
} }
export class MultiPicSourcePlugin<C extends PicSourceConfig> export const PicSourcePlugin = CreatePluginFactory(
extends BasePlugin<Instances<C>> BasePicSourcePlugin,
implements LifecycleEvents PicSourceConfig,
{ );
@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);
}
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