Skip to content

Commit

Permalink
feat: proxy support for image sources
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikoTan committed Mar 9, 2024
1 parent 23f8c07 commit d518546
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
}
},
"peerDependencies": {
"@koishijs/plugin-proxy-agent": "^0.1.4",
"koishi": "^4.17.0"
},
"devDependencies": {
"@koishijs/plugin-proxy-agent": "^0.1.4",
"@koishijs/assets": "^1.0.2",
"koishi": "^4.17.0"
},
Expand Down
23 changes: 17 additions & 6 deletions packages/core/src/source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Context, Schema } from 'koishi'
import { Context, Quester, Schema } from 'koishi'
import type {} from '@koishijs/plugin-proxy-agent'
import type { Inject } from 'cordis'

export abstract class ImageSource<Config extends ImageSource.Config = ImageSource.Config> {
Expand All @@ -7,13 +8,17 @@ export abstract class ImageSource<Config extends ImageSource.Config = ImageSourc
languages: string[] = []
source: string

http: Quester

constructor(public ctx: Context, public config: Config) {
this.ctx.booru.register(this)

this.http = ctx.http.extend({ proxyAgent: config.proxyAgent })
}

/**
* split query into tags, default implementation is comma-separated.
*
*
* e.g. `tag1, wordy tag2, UPPER CASED tag3` => `['tag1', 'wordy_tag2', 'upper_cased_tag3']`
*/
tokenize(query: string): string[] {
Expand All @@ -27,13 +32,19 @@ export namespace ImageSource {
export interface Config {
label: string
weight: number
proxyAgent: string
}

export function createSchema(o: { label: string }) {
return Schema.object({
label: Schema.string().default(o.label).description('图源标签,可用于在指令中手动指定图源。'),
weight: Schema.number().min(1).default(1).description('图源权重。在多个符合标签的图源中,将按照各自的权重随机选择。'),
}).description('全局设置')
return Schema.intersect([
Schema.object({
label: Schema.string().default(o.label).description('图源标签,可用于在指令中手动指定图源。'),
weight: Schema.number().min(1).default(1).description('图源权重。在多个符合标签的图源中,将按照各自的权重随机选择。'),
}).description('全局设置'),
Schema.object({
proxyAgent: Schema.string().default(undefined).description('请求图片时使用代理服务器。'),
}).description('请求设置'),
])
}

export const Config: Schema<Config> = createSchema({ label: 'default' })
Expand Down

0 comments on commit d518546

Please sign in to comment.