Skip to content

Commit

Permalink
✨ Feature: dynamic proxy getter with ctx.Request.request
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #64
  • Loading branch information
Molunerfinn committed Feb 8, 2021
1 parent b10b963 commit 687805f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import DB from '../utils/db'
import PluginHandler from '../lib/PluginHandler'
import { IBuildInEvent } from '../utils/enum'
import { version } from '../../package.json'
import { CONFIG_CHANGE } from '../utils/buildInEvent'
import { eventBus } from '../utils/eventBus'
import { RequestPromiseAPI } from 'request-promise-native'

class PicGo extends EventEmitter implements IPicGo {
private config!: IConfig
Expand Down Expand Up @@ -134,6 +137,10 @@ class PicGo extends EventEmitter implements IPicGo {
setConfig (config: IStringKeyMap<any>): void {
Object.keys(config).forEach((name: string) => {
set(this.config, name, config[name])
eventBus.emit(CONFIG_CHANGE, {
configName: name,
value: config[name]
})
})
}

Expand All @@ -159,6 +166,12 @@ class PicGo extends EventEmitter implements IPicGo {
}
}

// for v1.5.0+
get request (): RequestPromiseAPI {
// TODO: replace request with got: https://github.com/sindresorhus/got
return this.Request.request
}

async upload (input?: any[]): Promise<IImgInfo[] | Error> {
if (this.configPath === '') {
this.log.error('The configuration file only supports JSON format.')
Expand Down
34 changes: 26 additions & 8 deletions src/lib/Request.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import PicGo from '../core/PicGo'
import request, { RequestPromiseOptions, RequestPromiseAPI } from 'request-promise-native'
import { Undefinable } from '../types'
import { Undefinable, IConfigChangePayload, IConfig } from '../types'
import { CONFIG_CHANGE } from '../utils/buildInEvent'
import { eventBus } from '../utils/eventBus'

class Request {
ctx: PicGo
request!: RequestPromiseAPI
private readonly ctx: PicGo
private proxy: Undefinable<string> = ''
options: RequestPromiseOptions = {}
constructor (ctx: PicGo) {
this.ctx = ctx
this.init()
eventBus.on(CONFIG_CHANGE, (data: IConfigChangePayload<string | IConfig['picBed']>) => {
switch (data.configName) {
case 'picBed':
if ((data.value as IConfig['picBed'])?.proxy) {
this.proxy = (data.value as IConfig['picBed']).proxy
}
break
case 'picBed.proxy':
this.proxy = data.value as string
break
}
})
}

init (): void {
const options: RequestPromiseOptions = {
jar: request.jar()
}
const proxy = this.ctx.getConfig<Undefinable<string>>('picBed.proxy')
if (proxy) {
options.proxy = proxy
this.options.proxy = proxy
}
this.request = request.defaults(options)
}

// #64 dynamic get proxy value
get request (): RequestPromiseAPI {
// remove jar because we don't need anymore
this.options.proxy = this.proxy || undefined
return request.defaults(this.options)
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import LifecyclePlugins from '../lib/LifecyclePlugins'
import Commander from '../lib/Commander'
import PluginLoader from '../lib/PluginLoader'
import Request from '../lib/Request'
import { RequestPromiseAPI } from 'request-promise-native'

interface IPicGo extends NodeJS.EventEmitter {
configPath: string
Expand All @@ -16,6 +17,7 @@ interface IPicGo extends NodeJS.EventEmitter {
helper: IHelper
VERSION: string
GUI_VERSION?: string
request: RequestPromiseAPI

registerCommands: () => void
getConfig: <T>(name?: string) => T
Expand Down Expand Up @@ -314,3 +316,8 @@ interface ILogger {
error: (...msg: ILogArgvType[]) => void
warn: (...msg: ILogArgvType[]) => void
}

interface IConfigChangePayload<T> {
configName: string
value: T
}
1 change: 1 addition & 0 deletions src/utils/buildInEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CONFIG_CHANGE = 'CONFIG_CHANGE'
6 changes: 6 additions & 0 deletions src/utils/eventBus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { EventEmitter } from 'events'

const eventBus = new EventEmitter()
export {
eventBus
}

4 comments on commit 687805f

@d-w-x
Copy link

@d-w-x d-w-x commented on 687805f Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请问这里对request库的替换是无痕的吗?
插件只需要将原来的 ctx.Request.request 替换成 ctx.request 即可? 还是需要后期做一些额外的适配性工作?

@Molunerfinn
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请问这里对request库的替换是无痕的吗?
插件只需要将原来的 ctx.Request.request 替换成 ctx.request 即可? 还是需要后期做一些额外的适配性工作?

理论上是的,会尽可能保持一致性的输入和输出。如果遇到实在无法保证的属性,也会在文档中标识出来。1.5.0会发个beta版本,届时会替换上got

@d-w-x
Copy link

@d-w-x d-w-x commented on 687805f Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,谢谢,
别忘了同时替换官方库中这些的写法,以作为最新 request 用法的示例.

建议在某个版本中将默认的图床完全从 PicGo 中移除,作为插件存在.

@Molunerfinn
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,谢谢,
别忘了同时替换官方库中这些的写法,以作为最新 request 用法的示例.

建议在某个版本中将默认的图床完全从 PicGo 中移除,作为插件存在.

未来会考虑,之后要做web版本适配的话,会考虑做一些抽离。

Please sign in to comment.