teeny-request
Advanced tools
Comparing version 3.9.0 to 3.10.0
@@ -15,6 +15,8 @@ 'use strict'; | ||
var node_fetch_1 = require("node-fetch"); | ||
var stream_1 = require("stream"); | ||
var uuid = require("uuid"); | ||
// tslint:disable-next-line variable-name | ||
var HttpsProxyAgent = require('https-proxy-agent'); | ||
var requestToFetchOptions = function (reqOpts) { | ||
var options = __assign({}, reqOpts.method && { method: reqOpts.method }, reqOpts.timeout && { timeout: reqOpts.timeout }, reqOpts.gzip && { compress: reqOpts.gzip }); | ||
var options = __assign({ method: reqOpts.method || 'GET' }, reqOpts.timeout && { timeout: reqOpts.timeout }, reqOpts.gzip && { compress: reqOpts.gzip }); | ||
if (typeof reqOpts.json === 'object') { | ||
@@ -57,4 +59,110 @@ // Add Content-type: application/json header | ||
}; | ||
// create POST body from two parts as multipart/related content-type | ||
var createMultipartStream = function (boundary, multipart) { | ||
var finale = "--" + boundary + "--"; | ||
var stream = new stream_1.PassThrough(); | ||
for (var _i = 0, multipart_1 = multipart; _i < multipart_1.length; _i++) { | ||
var part = multipart_1[_i]; | ||
var preamble = "--" + boundary + "\r\nContent-Type: " + part['Content-Type'] + "\r\n\r\n"; | ||
stream.write(preamble); | ||
if (typeof part.body === 'string') { | ||
stream.write(part.body); | ||
stream.write('\r\n'); | ||
} | ||
else { | ||
part.body.pipe(stream, { end: false }); | ||
part.body.on('end', function () { | ||
stream.write('\r\n'); | ||
stream.write(finale); | ||
stream.end(); | ||
}); | ||
} | ||
} | ||
return stream; | ||
}; | ||
var teenyRequest = (function (reqOpts, callback) { | ||
var _a = requestToFetchOptions(reqOpts), uri = _a[0], options = _a[1]; | ||
var multipart = reqOpts.multipart; | ||
if (reqOpts.multipart && multipart.length === 2) { | ||
if (!callback) { | ||
console.log('Error, multipart without callback not implemented.'); | ||
return; | ||
} | ||
var boundary = uuid.v4(); | ||
options.headers['Content-Type'] = | ||
"multipart/related; boundary=" + boundary; | ||
options.body = createMultipartStream(boundary, multipart); | ||
// Multipart upload | ||
node_fetch_1.default(uri, options) | ||
.then(function (res) { | ||
var header = res.headers.get('content-type'); | ||
if (header === 'application/json' || | ||
header === 'application/json; charset=utf-8') { | ||
var response_1 = fetchToRequestResponse(res); | ||
res.json() | ||
.then(function (json) { | ||
response_1.body = json; | ||
callback(null, response_1, json); | ||
}) | ||
.catch(function (err) { | ||
callback(err); | ||
}); | ||
return; | ||
} | ||
res.text() | ||
.then(function (text) { | ||
var response = fetchToRequestResponse(res); | ||
response.body = text; | ||
callback(null, response, text); | ||
}) | ||
.catch(function (err) { | ||
callback(err); | ||
}); | ||
}) | ||
.catch(function (err) { | ||
callback(err); | ||
}); | ||
return; | ||
} | ||
if (callback === undefined) { // Stream mode | ||
var requestStream_1 = new stream_1.PassThrough(); | ||
options.compress = false; | ||
node_fetch_1.default(uri, options) | ||
.then(function (res) { | ||
if (!res.ok) { | ||
res.text() | ||
.then(function (text) { | ||
// tslint:disable-next-line:no-any | ||
var error = new Error(text); | ||
error.code = res.status; | ||
requestStream_1.emit('error', error); | ||
return; | ||
}) | ||
.catch(function (error) { | ||
requestStream_1.emit('error', error); | ||
}); | ||
return; | ||
} | ||
var encoding = res.headers.get('content-encoding'); | ||
res.body.on('error', function (err) { | ||
console.log('whoa there was an error, passing it on: ' + err); | ||
requestStream_1.emit('error', err); | ||
}); | ||
// tslint:disable-next-line:no-any | ||
res.body.toJSON = function () { | ||
var headers = __assign({}, (encoding && { 'content-encoding': encoding })); | ||
return { headers: headers }; | ||
}; | ||
requestStream_1.emit('response', res.body); | ||
}) | ||
.catch(function (err) { | ||
console.log('such a nice error:' + err); | ||
requestStream_1.emit('error', err); | ||
}); | ||
// fetch doesn't supply the raw HTTP stream, instead it | ||
// returns a PassThrough piped from the HTTP response | ||
// stream. | ||
return requestStream_1; | ||
} | ||
// GET or POST with callback | ||
node_fetch_1.default(uri, options) | ||
@@ -65,7 +173,12 @@ .then(function (res) { | ||
header === 'application/json; charset=utf-8') { | ||
var response_1 = fetchToRequestResponse(res); | ||
var response_2 = fetchToRequestResponse(res); | ||
if (response_2.statusCode === 204) { | ||
// Probably a DELETE | ||
callback(null, response_2, response_2); | ||
return; | ||
} | ||
res.json() | ||
.then(function (json) { | ||
response_1.body = json; | ||
callback(null, response_1, json); | ||
response_2.body = json; | ||
callback(null, response_2, json); | ||
}) | ||
@@ -90,2 +203,3 @@ .catch(function (err) { | ||
}); | ||
return; | ||
}); | ||
@@ -95,5 +209,5 @@ exports.teenyRequest = teenyRequest; | ||
return function (reqOpts, callback) { | ||
teenyRequest(__assign({}, defaults, reqOpts), callback); | ||
return teenyRequest(__assign({}, defaults, reqOpts), callback); | ||
}; | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "teeny-request", | ||
"version": "3.9.0", | ||
"version": "3.10.0", | ||
"description": "Like request, but smaller.", | ||
@@ -37,3 +37,4 @@ "main": "./build/src/index.js", | ||
"https-proxy-agent": "^2.2.1", | ||
"node-fetch": "^2.2.0" | ||
"node-fetch": "^2.2.0", | ||
"uuid": "^3.3.2" | ||
}, | ||
@@ -44,2 +45,3 @@ "devDependencies": { | ||
"@types/request": "^2.47.1", | ||
"@types/uuid": "^3.4.4", | ||
"gts": "^0.8.0", | ||
@@ -46,0 +48,0 @@ "mocha": "^5.2.0", |
Sorry, the diff of this file is not supported yet
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
30129
217
3
7
+ Addeduuid@^3.3.2
+ Addeduuid@3.4.0(transitive)