Commit 4abc27e9 authored by nanahira's avatar nanahira

add http settings

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