|
1 | 1 | import PicGo from '../core/PicGo'
|
2 | 2 | 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' |
4 | 6 |
|
5 | 7 | class Request {
|
6 |
| - ctx: PicGo |
7 |
| - request!: RequestPromiseAPI |
| 8 | + private readonly ctx: PicGo |
| 9 | + private proxy: Undefinable<string> = '' |
| 10 | + options: RequestPromiseOptions = {} |
8 | 11 | constructor (ctx: PicGo) {
|
9 | 12 | this.ctx = ctx
|
10 | 13 | 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 | + }) |
11 | 26 | }
|
12 | 27 |
|
13 | 28 | init (): void {
|
14 |
| - const options: RequestPromiseOptions = { |
15 |
| - jar: request.jar() |
16 |
| - } |
17 | 29 | const proxy = this.ctx.getConfig<Undefinable<string>>('picBed.proxy')
|
18 | 30 | if (proxy) {
|
19 |
| - options.proxy = proxy |
| 31 | + this.options.proxy = proxy |
20 | 32 | }
|
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) |
22 | 40 | }
|
23 | 41 | }
|
24 | 42 |
|
|
0 commit comments