diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eb130c..f07729f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [3.12.1] - 2023-XX-XX + +### Added +- Added `failOnEmptyBody` flag for `callServer` to specify wether or not a request should fail if it has no body. ## [3.12.0] - 2023-12-03 @@ -12,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Support for header-based logger feature - Added `risk_start_time` and `enforcer_start_time` fields to enforcer activities. + ### Changed - Changed the structure of the headers field on async activities to array diff --git a/lib/pxclient.js b/lib/pxclient.js index 2c6884d..784cfb7 100644 --- a/lib/pxclient.js +++ b/lib/pxclient.js @@ -169,7 +169,15 @@ class PxClient { } callServer(data, path, additionalHeaders, config, cb) { - pxHttpc.callServer(data, this.createHeaders(config, additionalHeaders), path, 'activities', config); + pxHttpc.callServer( + data, + this.createHeaders(config, additionalHeaders), + path, + 'activities', + config, + null, + false, + ); if (cb) { cb(); } diff --git a/lib/pxhttpc.js b/lib/pxhttpc.js index 46fff58..0b4e47f 100644 --- a/lib/pxhttpc.js +++ b/lib/pxhttpc.js @@ -17,7 +17,7 @@ module.exports = { * @param {string} callType - indication for a query or activities sending * @param {Function} callback - callback function. */ -function callServer(data, headers, uri, callType, config, callback) { +function callServer(data, headers, uri, callType, config, callback, failOnEmptyBody = true) { callback = callback || ((err) => { @@ -37,7 +37,12 @@ function callServer(data, headers, uri, callType, config, callback) { if (err.toString().toLowerCase().includes('timeout')) { return callback('timeout'); } else { - return handleError(callback, S2SErrorReason.UNKNOWN_ERROR, `encountered error with risk api call: ${err}`, response); + return handleError( + callback, + S2SErrorReason.UNKNOWN_ERROR, + `encountered error with risk api call: ${err}`, + response, + ); } } @@ -46,12 +51,12 @@ function callServer(data, headers, uri, callType, config, callback) { callback, S2SErrorReason.UNKNOWN_ERROR, `call to perimeterx server returned null or empty response: ${response}`, - response + response, ); } if (response.statusCode === 200) { - return handleOkResponse(callback, response); + return handleOkResponse(callback, response, failOnEmptyBody); } return handleUnexpectedHttpResponse(callback, response); @@ -62,23 +67,38 @@ function callServer(data, headers, uri, callType, config, callback) { } } -const handleOkResponse = (callback, response) => { +const handleOkResponse = (callback, response, failOnEmptyBody) => { let data = getDataFromResponse(response); - if (!data) { - return handleError(callback, S2SErrorReason.INVALID_RESPONSE, `unable to get data from response body: ${response.body}`, response); + if (!data && failOnEmptyBody) { + return handleError( + callback, + S2SErrorReason.INVALID_RESPONSE, + `unable to get data from response body: ${response.body}`, + response, + ); } if (typeof data !== 'object') { try { data = JSON.parse(data); } catch (e) { - return handleError(callback, S2SErrorReason.INVALID_RESPONSE, `error parsing response body - ${data}: ${e}`, response); + return handleError( + callback, + S2SErrorReason.INVALID_RESPONSE, + `error parsing response body - ${data}: ${e}`, + response, + ); } } - if (data.status && data.status !== 0) { - return handleError((err) => callback(err, data), S2SErrorReason.REQUEST_FAILED_ON_SERVER, data.message, response); + if (data && data.status && data.status !== 0) { + return handleError( + (err) => callback(err, data), + S2SErrorReason.REQUEST_FAILED_ON_SERVER, + data.message, + response, + ); } return callback(null, data);