Commit 62b7ae9f authored by nanahira's avatar nanahira

fix typing

parent 1aa511d3
import { Schema } from 'koishi';
import { Awaitable, Context } from 'koishi';
import { DefinePlugin } from 'koishi-thirdeye';
import { DefinePlugin, RegisterSchema, SchemaProperty } from 'koishi-thirdeye';
import {
DefineMultiSourcePlugin,
PicResult,
......@@ -8,11 +8,17 @@ import {
PicSourcePlugin,
} from '../src';
@DefinePlugin({ name: 'test-source', schema: PicSourceConfig })
class TestPicSourcePlugin extends PicSourcePlugin {
@RegisterSchema()
class Config extends PicSourceConfig {
@SchemaProperty({ default: 'https://cdn02.moecube.com:444' })
endpoint: string;
}
@DefinePlugin({ name: 'test-source', schema: Config })
class TestPicSourcePlugin extends PicSourcePlugin<Config> {
randomPic(picTags: string[]): Awaitable<PicResult> {
return {
url: `https://cdn02.moecube.com:444/images/ygopro-images-${this.name}/${
url: `${this.config.endpoint}/images/ygopro-images-${this.name}/${
picTags[0] || '10000'
}.jpg`,
description: picTags[0] || '10000',
......@@ -22,7 +28,7 @@ class TestPicSourcePlugin extends PicSourcePlugin {
export class TestMultiPicSourcePlugin extends DefineMultiSourcePlugin(
TestPicSourcePlugin,
PicSourceConfig,
Config,
) {}
console.log((TestMultiPicSourcePlugin['Config'] as Schema).dict.instances.type);
......@@ -30,7 +36,14 @@ console.log((TestMultiPicSourcePlugin['Config'] as Schema).dict.instances.type);
export default class ExtrasInDev {
constructor(ctx: Context) {
ctx.plugin(TestMultiPicSourcePlugin, {
instances: [{ name: 'zh-CN', isDefault: true }, { name: 'en-US' }],
instances: [
{
name: 'zh-CN',
isDefault: true,
endpoint: 'https://cdn02.moecube.com:444',
},
{ name: 'en-US' },
],
});
}
......
// import 'source-map-support/register';
import {
SchemaProperty,
RegisterSchema,
SchemaClass,
ClassType,
} from 'koishi-thirdeye';
import { SchemaProperty, RegisterSchema, SchemaClass } from 'koishi-thirdeye';
import { Quester } from 'koishi';
import {
Instances,
PicMiddleware,
PicMiddlewareInfo,
PicSourceInfo,
} from './def';
import { PicMiddleware, PicMiddlewareInfo, PicSourceInfo } from './def';
@RegisterSchema()
export class PicsPluginConfig {
......
......@@ -67,3 +67,7 @@ export function ClonePlugin<P extends { new (...args: any[]): any }>(
});
return clonedPlugin;
}
export type TypeFromClass<T> = T extends { new (...args: any[]): infer U }
? U
: never;
......@@ -17,6 +17,7 @@ import {
Instances,
ToInstancesConfig,
ClonePlugin,
TypeFromClass,
} from './def';
export class PicSource implements PicSourceInfo {
......@@ -49,9 +50,7 @@ export class PicSource implements PicSourceInfo {
}
}
export class PicSourcePlugin<
C extends PicSourceConfig = PicSourceConfig,
> extends PicSource {
export class PicSourcePlugin<C extends PicSourceConfig> extends PicSource {
constructor(ctx: Context, config: PartialDeep<C>) {
super(ctx);
}
......@@ -101,18 +100,19 @@ export class MultiPicSourcePlugin<C extends PicSourceConfig>
}
}
export function DefineMultiSourcePlugin<
C extends PicSourceConfig,
P extends PicSourcePlugin<C>,
>(
SourcePlugin: new (ctx: Context, config: C) => P,
SourceConfig: ClassType<C>,
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<C>>,
) => MultiPicSourcePlugin<C> {
const pluginClass = class SpecificMultiPicSourcePlugin extends MultiPicSourcePlugin<C> {
config: Instances<PartialDeep<TypeFromClass<C>>>,
) => MultiPicSourcePlugin<TypeFromClass<C>> {
const pluginClass = class SpecificMultiPicSourcePlugin extends MultiPicSourcePlugin<
TypeFromClass<C>
> {
getSourcePlugin() {
return SourcePlugin;
}
......
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