import * as rp from "request-promise-native" ; import * as HttpStatus from "http-status" ; import Proxy from "./Proxy" ; import { ICreateProxyBody , ICreateProxyResponse , IGetProxyResponse , IPopulateProxiesBody , IPopulateProxiesResponse , IGetProxiesResponse } from "./interfaces" ; export interface Proxies { [ name ] : Proxy ; } export default class Toxiproxy { host : string ; constructor ( host ) { this . host = host ; } async createProxy ( body ) { try { return new Proxy ( this , < ICreateProxyResponse > await rp . post ( { body : body , json : true , url : `${ this . host }/proxies` } ) ) ; } catch ( err ) { if ( ! ( "statusCode" in err ) ) { throw err ; } if ( err . statusCode === HttpStatus . CONFLICT ) { throw new Error ( `Proxy ${ body . name } already exists` ) ; } throw new Error ( `Response status was not ${ HttpStatus . OK }: ${ err . statusCode }` ) ; } } async populate ( body ) { try { const res = < IPopulateProxiesResponse > await rp . post ( { body : body , json : true , url : `${ this . host }/populate` } ) ; const proxies = { } ; for ( const proxyResponse of res . proxies ) { proxies [ proxyResponse . name ] = new Proxy ( this , proxyResponse ) ; } return proxies ; } catch ( err ) { if ( ! ( "statusCode" in err ) ) { throw err ; } throw new Error ( `Response status was not ${ HttpStatus . OK }: ${ err . statusCode }` ) ; } } async get ( name ) { try { return new Proxy ( this , < IGetProxyResponse > await rp . get ( { json : true , url : `${ this . host }/proxies/${ name }` } ) ) ; } catch ( err ) { if ( ! ( "statusCode" in err ) ) { throw err ; } throw new Error ( `Response status was not ${ HttpStatus . OK }: ${ err . statusCode }` ) ; } } async getVersion ( ) { try { return await rp . get ( { json : true , url : `${ this . host }/version` } ) ; } catch ( err ) { if ( ! ( "statusCode" in err ) ) { throw err ; } throw new Error ( `Response status was not ${ HttpStatus . OK }: ${ err . statusCode }` ) ; } } async reset ( ) { try { return await rp . post ( { json : true , url : `${ this . host }/reset` } ) ; } catch ( err ) { if ( ! ( "statusCode" in err ) ) { throw err ; } throw new Error ( `Response status was not ${ HttpStatus . NO_CONTENT }: ${ err . statusCode }` ) ; } } async getAll ( ) { try { const responses = < IGetProxiesResponse > await rp . get ( { json : true , url : `${ this . host }/proxies` } ) ; const proxies = { } ; for ( const name in responses ) { proxies [ name ] = new Proxy ( this , responses [ name ] ) ; } return proxies ; } catch ( err ) { if ( ! ( "statusCode" in err ) ) { throw err ; } throw new Error ( `Response status was not ${ HttpStatus . OK }: ${ err . statusCode }` ) ; } } }