Skip to content

Commit 687805f

Browse files
committed
✨ Feature: dynamic proxy getter with ctx.Request.request
ISSUES CLOSED: #64
1 parent b10b963 commit 687805f

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

src/core/PicGo.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import DB from '../utils/db'
1717
import PluginHandler from '../lib/PluginHandler'
1818
import { IBuildInEvent } from '../utils/enum'
1919
import { version } from '../../package.json'
20+
import { CONFIG_CHANGE } from '../utils/buildInEvent'
21+
import { eventBus } from '../utils/eventBus'
22+
import { RequestPromiseAPI } from 'request-promise-native'
2023

2124
class PicGo extends EventEmitter implements IPicGo {
2225
private config!: IConfig
@@ -134,6 +137,10 @@ class PicGo extends EventEmitter implements IPicGo {
134137
setConfig (config: IStringKeyMap<any>): void {
135138
Object.keys(config).forEach((name: string) => {
136139
set(this.config, name, config[name])
140+
eventBus.emit(CONFIG_CHANGE, {
141+
configName: name,
142+
value: config[name]
143+
})
137144
})
138145
}
139146

@@ -159,6 +166,12 @@ class PicGo extends EventEmitter implements IPicGo {
159166
}
160167
}
161168

169+
// for v1.5.0+
170+
get request (): RequestPromiseAPI {
171+
// TODO: replace request with got: https://github.com/sindresorhus/got
172+
return this.Request.request
173+
}
174+
162175
async upload (input?: any[]): Promise<IImgInfo[] | Error> {
163176
if (this.configPath === '') {
164177
this.log.error('The configuration file only supports JSON format.')

src/lib/Request.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
import PicGo from '../core/PicGo'
22
import request, { RequestPromiseOptions, RequestPromiseAPI } from 'request-promise-native'
3-
import { Undefinable } from '../types'
3+
import { Undefinable, IConfigChangePayload, IConfig } from '../types'
4+
import { CONFIG_CHANGE } from '../utils/buildInEvent'
5+
import { eventBus } from '../utils/eventBus'
46

57
class Request {
6-
ctx: PicGo
7-
request!: RequestPromiseAPI
8+
private readonly ctx: PicGo
9+
private proxy: Undefinable<string> = ''
10+
options: RequestPromiseOptions = {}
811
constructor (ctx: PicGo) {
912
this.ctx = ctx
1013
this.init()
14+
eventBus.on(CONFIG_CHANGE, (data: IConfigChangePayload<string | IConfig['picBed']>) => {
15+
switch (data.configName) {
16+
case 'picBed':
17+
if ((data.value as IConfig['picBed'])?.proxy) {
18+
this.proxy = (data.value as IConfig['picBed']).proxy
19+
}
20+
break
21+
case 'picBed.proxy':
22+
this.proxy = data.value as string
23+
break
24+
}
25+
})
1126
}
1227

1328
init (): void {
14-
const options: RequestPromiseOptions = {
15-
jar: request.jar()
16-
}
1729
const proxy = this.ctx.getConfig<Undefinable<string>>('picBed.proxy')
1830
if (proxy) {
19-
options.proxy = proxy
31+
this.options.proxy = proxy
2032
}
21-
this.request = request.defaults(options)
33+
}
34+
35+
// #64 dynamic get proxy value
36+
get request (): RequestPromiseAPI {
37+
// remove jar because we don't need anymore
38+
this.options.proxy = this.proxy || undefined
39+
return request.defaults(this.options)
2240
}
2341
}
2442

src/types/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import LifecyclePlugins from '../lib/LifecyclePlugins'
22
import Commander from '../lib/Commander'
33
import PluginLoader from '../lib/PluginLoader'
44
import Request from '../lib/Request'
5+
import { RequestPromiseAPI } from 'request-promise-native'
56

67
interface IPicGo extends NodeJS.EventEmitter {
78
configPath: string
@@ -16,6 +17,7 @@ interface IPicGo extends NodeJS.EventEmitter {
1617
helper: IHelper
1718
VERSION: string
1819
GUI_VERSION?: string
20+
request: RequestPromiseAPI
1921

2022
registerCommands: () => void
2123
getConfig: <T>(name?: string) => T
@@ -314,3 +316,8 @@ interface ILogger {
314316
error: (...msg: ILogArgvType[]) => void
315317
warn: (...msg: ILogArgvType[]) => void
316318
}
319+
320+
interface IConfigChangePayload<T> {
321+
configName: string
322+
value: T
323+
}

src/utils/buildInEvent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const CONFIG_CHANGE = 'CONFIG_CHANGE'

src/utils/eventBus.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { EventEmitter } from 'events'
2+
3+
const eventBus = new EventEmitter()
4+
export {
5+
eventBus
6+
}

0 commit comments

Comments
 (0)