@capriza/http-utils
Advanced tools
Comparing version 0.2.0 to 0.2.1
var axios = require('axios'); | ||
var BaseUtils = require("./baseUtils"); | ||
var Queue = require("./rateLimitQueue"); | ||
const responseTypes = ["data", "status", "statusText", "headers", "config", "request"]; | ||
@@ -34,10 +35,2 @@ module.exports = class HttpUtils extends BaseUtils { | ||
async patch(url, opts, context) { | ||
return this._callRequest("patch", url, opts, context); | ||
} | ||
async head(url, opts, context) { | ||
return this._callRequest("head", url, opts, context); | ||
} | ||
async downloadUrlAttachment(uri, context) { | ||
@@ -48,3 +41,3 @@ var res = await this.get(uri, {responseType : 'arraybuffer'}, context); | ||
async _callRequest(method, url, opts = {}, { priority, debug, logger } = {}){ | ||
async _callRequest(method, url, opts = {}, { priority, debug, logger, responseType = 'data' } = {}){ | ||
const requestLog = logger || this.logger; | ||
@@ -64,5 +57,8 @@ return new Promise((resolve, reject)=>{ | ||
reject(err.response); | ||
} else { | ||
} else if (err.code){ | ||
requestLog.error(`[HttpUtils] HTTP ${method} error: ${err.code} on ${this.baseURL}${url}`); | ||
reject(err); | ||
} else { | ||
requestLog.error(`[HttpUtils] HTTP ${method} error: ${err} on ${this.baseURL}${url}`); | ||
reject(err); | ||
} | ||
@@ -80,5 +76,14 @@ }; | ||
this.requestQueue.add(doRequest, {priority}).then((response)=>resolve(response.data)).catch(onError); | ||
this.requestQueue.add(doRequest, {priority}).then((response)=>{ | ||
if(responseTypes.includes(responseType)) { | ||
return resolve(response[responseType]); | ||
} else if(responseType == "full") { | ||
return resolve(response); | ||
} else { | ||
return reject(`[HttpUtils] responseType = ${responseType} does't valid.`); | ||
} | ||
}).catch(onError); | ||
}); | ||
} | ||
}; |
{ | ||
"name": "@capriza/http-utils", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "HTTP Request utils that handles, request-response, errors, concurrency, priority and authentication", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
26715
320