"use strict" ; import * as vscode from "vscode" ; import axios , { AxiosResponse } from "axios" ; import { getConfig } from "./config" ; import { HttpProxyAgent } from "http-proxy-agent" ; export interface Response { language : string ; data : string ; } class Snippet { currLanguage ? : string ; currQuery ? : string ; currNum : number ; verboseState : boolean ; requestCache : any ; constructor ( ) { this . currNum = 0 ; this . verboseState = getConfig ( "verbose" ) ; this . requestCache = new Object ( ) ; } toggleVerbose ( ) { this . verboseState = ! this . verboseState ; } async load ( language , query , num ) { this . currLanguage = language ; this . currQuery = query ; this . currNum = num ; let response = await this . _doRequest ( ) ; return { language , data : response . data } ; } getNextAnswerNumber ( ) { this . currNum ++ ; return this . currNum ; } getPreviousAnswerNumber ( ) { if ( this . currNum == 0 ) { return null ; } this . currNum -- ; return this . currNum ; } getCurrentQuery ( ) { return this . currQuery ; } getCurrentAnswerNumber ( ) { return this . currNum ; } getVerbose ( ) { return this . verboseState ; } private _requestConfig ( ) { let config = { headers : { "User-Agent" : "curl/7.43.0" } } ; let proxy = vscode . workspace . getConfiguration ( "http" ) [ "proxy" ] ; if ( proxy !== "" ) { let agent = new HttpProxyAgent ( proxy ) ; config [ "agent" ] = agent ; } return config ; } private getUrl ( language , query ) { let baseUrl = getConfig ( "baseUrl" ) ; let num = this . currNum ; let params = this . verboseState ? "qT" : "QT" ; let path = `/vscode:${ language }/${ query }/${ num }?${ params }&style=bw` ; return baseUrl + path ; } private async _doRequest ( ) { let query = encodeURI ( this . currQuery . replace ( / /g , "+" ) ) ; let url = this . getUrl ( this . currLanguage , query ) ; let data = this . requestCache [ url ] ; if ( data ) { return data ; } let res = await axios . get ( url , this . _requestConfig ( ) ) ; this . requestCache [ url ] = res ; return res ; } } export default new Snippet ( ) ;