import request , { RequestPromiseOptions , RequestPromiseAPI } from 'request-promise-native' ; import { IPicGo , Undefinable , IConfigChangePayload , IConfig } from '../types' ; import { IBusEvent } from '../utils/enum' ; import { eventBus } from '../utils/eventBus' ; export class Request { private readonly ctx : IPicGo ; private proxy : Undefinable < string > = '' ; options : RequestPromiseOptions = { } ; constructor ( ctx ) { this . ctx = ctx ; this . init ( ) ; eventBus . on ( IBusEvent . CONFIG_CHANGE , ( data ) => { 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 ; } } ) ; } private init ( ) { const proxy = this . ctx . getConfig < Undefinable < string > > ( 'picBed.proxy' ) ; if ( proxy ) { this . proxy = proxy ; } } get request ( ) : RequestPromiseAPI { this . options . proxy = this . proxy || undefined ; return request . defaults ( this . options ) ; } } export default Request ;