diff --git a/src/core/PicGo.ts b/src/core/PicGo.ts index 923ad69..76b870b 100644 --- a/src/core/PicGo.ts +++ b/src/core/PicGo.ts @@ -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 @@ -134,6 +137,10 @@ class PicGo extends EventEmitter implements IPicGo { setConfig (config: IStringKeyMap): void { Object.keys(config).forEach((name: string) => { set(this.config, name, config[name]) + eventBus.emit(CONFIG_CHANGE, { + configName: name, + value: config[name] + }) }) } @@ -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 { if (this.configPath === '') { this.log.error('The configuration file only supports JSON format.') diff --git a/src/lib/Request.ts b/src/lib/Request.ts index b003a18..127c6d9 100644 --- a/src/lib/Request.ts +++ b/src/lib/Request.ts @@ -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 = '' + options: RequestPromiseOptions = {} constructor (ctx: PicGo) { this.ctx = ctx this.init() + eventBus.on(CONFIG_CHANGE, (data: IConfigChangePayload) => { + 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>('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) } } diff --git a/src/types/index.d.ts b/src/types/index.d.ts index e2deceb..d2fd86a 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -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 @@ -16,6 +17,7 @@ interface IPicGo extends NodeJS.EventEmitter { helper: IHelper VERSION: string GUI_VERSION?: string + request: RequestPromiseAPI registerCommands: () => void getConfig: (name?: string) => T @@ -314,3 +316,8 @@ interface ILogger { error: (...msg: ILogArgvType[]) => void warn: (...msg: ILogArgvType[]) => void } + +interface IConfigChangePayload { + configName: string + value: T +} diff --git a/src/utils/buildInEvent.ts b/src/utils/buildInEvent.ts new file mode 100644 index 0000000..9041869 --- /dev/null +++ b/src/utils/buildInEvent.ts @@ -0,0 +1 @@ +export const CONFIG_CHANGE = 'CONFIG_CHANGE' diff --git a/src/utils/eventBus.ts b/src/utils/eventBus.ts new file mode 100644 index 0000000..d49fa17 --- /dev/null +++ b/src/utils/eventBus.ts @@ -0,0 +1,6 @@ +import { EventEmitter } from 'events' + +const eventBus = new EventEmitter() +export { + eventBus +}