@everymundo/promise-data-to
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -8,2 +8,4 @@ 'use strict'; | ||
{ setResTxt } = require('../lib/set-response-text'), | ||
{ getDataFromXData } = require('../lib/get-data-from-xdata'), | ||
{ setHeaders } = require('../lib/set-headers'), | ||
protocols = {'http:': require('http'), 'https:': require('https')}, | ||
@@ -26,2 +28,3 @@ SIMULATE = +process.env.SIMULATE, | ||
maxRetry = MAX_RETRY_ATTEMPTS, | ||
compress = false // eslint-disable-line comma-dangle | ||
}, xData) => new Promise((resolve, reject) => { | ||
@@ -31,3 +34,4 @@ if (!http) http = protocols[protocol]; | ||
const | ||
data = typeof xData === 'string' ? xData : JSON.stringify(xData), | ||
// data = typeof xData === 'string' ? xData : JSON.stringify(xData), | ||
data = getDataFromXData(xData, compress), | ||
start = new Date(), | ||
@@ -48,7 +52,12 @@ post_options = { | ||
if (!headers['Content-type']) headers['Content-type'] = 'application/json'; | ||
setHeaders(headers, data, compress); | ||
// if (!headers['Content-type']) headers['Content-type'] = 'application/json'; | ||
if (data) headers['Content-Length'] = Buffer.byteLength(data); | ||
// if (compress && ['deflate', 'gzip'].includes(compress)) { | ||
// headers['Content-Encoding'] = compress; | ||
// } | ||
function post() { | ||
// if (data) headers['Content-Length'] = Buffer.byteLength(data); | ||
const post = () => { | ||
// Set up the request | ||
@@ -104,5 +113,11 @@ const post_req = http.request(post_options, (res) => { | ||
.on('error', (err) => { | ||
logr.error('ERROR!', err); | ||
logr.error('http.request', err); | ||
const stats = { | ||
code: 599, start, end: Date.now(), err, attempt, endpoint, | ||
code: 599, | ||
start, | ||
end: Date.now(), | ||
err, | ||
attempt, | ||
endpoint, | ||
headers, | ||
}; | ||
@@ -115,3 +130,3 @@ tryAgain(err, stats); | ||
post_req.end(); | ||
} | ||
}; | ||
@@ -140,2 +155,3 @@ function tryAgain(error, stats) { | ||
loadedAt: new Date(), | ||
getDataFromXData, | ||
}; |
{ | ||
"name": "@everymundo/promise-data-to", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "This is a helper to perform POST requests using promises and no external dependencies", | ||
@@ -12,4 +12,4 @@ "main": "index.js", | ||
"check-coverage": "istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100", | ||
"check-lint": "eslint *.js lib/*.js test/*.js routes/*.js", | ||
"fix-lint": "eslint --fix *.js lib/*.js test/*.js routes/*.js", | ||
"check-lint": "eslint *.js lib/*.js test/*.js", | ||
"fix-lint": "eslint --fix *.js lib/*.js test/*.js", | ||
"test": "RETRY_TIMEOUT_MS= MAX_RETRY_ATTEMPTS= SIMULATE= mocha" | ||
@@ -42,8 +42,8 @@ }, | ||
"@everymundo/cleanrequire": "^1.1.1", | ||
"@everymundo/simple-clone": "^1.0.0", | ||
"@everymundo/simple-clone": "^1.1.0", | ||
"chai": "^4.1.2", | ||
"ghooks": "^2.0.2", | ||
"ghooks": "^2.0.4", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^5.0.4", | ||
"sinon": "^4.4.6" | ||
"mocha": "^5.2.0", | ||
"sinon": "^4.5.0" | ||
}, | ||
@@ -50,0 +50,0 @@ "dependencies": { |
@@ -47,6 +47,5 @@ 'require strict'; | ||
Object.keys(expected).forEach(key => | ||
expect(lib[key]).to.be.instanceof(expected[key])); | ||
Object.keys(expected).forEach(key => expect(lib[key]).to.be.instanceof(expected[key])); | ||
}); | ||
}); | ||
}); |
@@ -8,2 +8,3 @@ 'require strict'; | ||
const | ||
sinon = require('sinon'), | ||
{ sandbox, spy } = require('sinon'), | ||
@@ -60,12 +61,47 @@ { expect } = require('chai'), | ||
const | ||
config = { | ||
http, | ||
host: 'localhost', | ||
port: 80, | ||
path: '/path', | ||
endpoint: 'http://localhost:80/path', | ||
headers: { Authorization: 'Authorization' }, | ||
}; | ||
const config = { | ||
http, | ||
host: 'localhost', | ||
port: 80, | ||
path: '/path', | ||
endpoint: 'http://localhost:80/path', | ||
headers: { Authorization: 'Authorization' }, | ||
}; | ||
context('Calling setHeaders', () => { | ||
const libSetHeaders = cleanrequire('../lib/set-headers.js'); | ||
beforeEach(() => { | ||
box.stub(libSetHeaders, 'setHeaders'); | ||
}); | ||
it('should call setHeaders passing the correct arguments', () => { | ||
const { setHeaders } = libSetHeaders; | ||
const data = { a: 1, b: 2, c: 3 }; | ||
const expectedData = require('zlib').gzipSync(JSON.stringify(data)); | ||
httpRequest.callsFake((options, callback) => { | ||
httpResponse.statusCode = 200; | ||
callback(httpResponse); | ||
return httpEmitter; | ||
}); | ||
const { promiseDataTo } = loadLib(); | ||
const customConfig = clone(config); | ||
const { headers } = customConfig; | ||
customConfig.http = http; | ||
customConfig.compress = 'gzip'; | ||
return promiseDataTo(customConfig, data) | ||
.then(() => { | ||
expect(setHeaders).to.have.property('calledOnce', true); | ||
// expect(libSetHeaders.setHeaders.calledWith(headers, expectedData, 'gzip')).to.be.true; | ||
sinon.assert.calledWith(setHeaders, headers, expectedData, 'gzip'); | ||
// expect(.calledWith(headers, expectedData, 'gzip')).to.be.true; | ||
}); | ||
}); | ||
}); | ||
it('should success when status is between 200 and 299 using http', () => { | ||
@@ -72,0 +108,0 @@ const |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
51049
23
1307
9