Commit 4abc27e9 authored by nanahira's avatar nanahira

add http settings

parent ef3e5a2e
......@@ -60,7 +60,7 @@ export class TestMultiPicSourcePlugin extends MultiInstancePlugin(
export default class ExtrasInDev {
constructor(ctx: Context) {
ctx.plugin(TestPicSourcePlugin, {
ctx.plugin(TestPlainPicSourcePlugin, {
// name: 'zh-CN',
isDefault: true,
// endpoint: 'https://cdn02.moecube.com:444',
......
......@@ -32,6 +32,9 @@ export type PicsPluginConfigLike = Partial<PicsPluginConfig>;
// For convenience of plugins
const questerConfigDict = { ...Quester.Config.dict };
delete questerConfigDict.endpoint;
export class PicSourceConfig implements PicSourceInfo {
@SchemaProperty({ type: 'string', default: [], description: '图源标签' })
tags: string[];
......@@ -44,6 +47,13 @@ export class PicSourceConfig implements PicSourceInfo {
@SchemaProperty({ description: '是否为默认图源' })
isDefault?: boolean;
@SchemaProperty({
type: Schema.object(questerConfigDict),
description: '请求设置',
default: {},
})
http: Omit<Quester.Config, 'endpoint'>;
// 给目标对象注入上述对象。
applyTo(target: PicSourceInfo) {
target.tags = this.tags;
......
import { Context, Awaitable, Logger, Schema } from 'koishi';
import { Context, Awaitable, Logger, Schema, Quester } from 'koishi';
import {
PartialDeep,
InjectConfig,
......@@ -8,6 +8,7 @@ import {
schemaFromClass,
Apply,
Reusable,
SchemaClass,
} from 'koishi-thirdeye';
import PicsContainer from '.';
import { PicSourceConfig } from './config';
......@@ -58,6 +59,8 @@ export class BasePicSourcePlugin extends PicSource {
super(ctx);
}
http: Quester;
@InjectConfig()
config: PicSourceConfig;
......@@ -69,6 +72,7 @@ export class BasePicSourcePlugin extends PicSource {
@Apply()
initializeSource() {
this.http = this.ctx.http.extend(this.config.httpConfig || {});
this.config.applyTo(this);
this.pics.addSource(this);
}
......@@ -82,16 +86,18 @@ export const PicSourcePlugin = CreatePluginFactory(
export function PlainPicSourcePlugin<C>(dict: {
[K in keyof C]: Schema<C[K]>;
}) {
const Config = schemaFromClass(PicSourceConfig) as unknown as Schema<
const Config = SchemaClass(PicSourceConfig) as unknown as Schema<
PartialDeep<PicSourceConfig & C>,
PicSourceConfig & C
>;
Object.assign(Config.dict, dict);
return class PlainPicSourcePluginBase extends PicSource {
config: PicSourceInfo & C;
constructor(ctx: Context, config: PartialDeep<C & PicSourceInfo>) {
http: Quester;
config: PicSourceConfig & C;
constructor(ctx: Context, config: PartialDeep<C & PicSourceConfig>) {
super(ctx);
this.config = config as PicSourceInfo & C;
this.config = config as PicSourceConfig & C;
this.http = this.ctx.http.extend(this.config.httpConfig || {});
this.applyConfig(config);
ctx.pics.addSource(this);
}
......
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