teeny-request
Advanced tools
Comparing version 3.9.0 to 3.9.1
@@ -0,10 +1,15 @@ | ||
/// <reference types="node" /> | ||
import * as r from 'request'; | ||
interface Callback { | ||
(err: Error | null, response?: r.Response, body?: {} | string): void; | ||
import { PassThrough } from 'stream'; | ||
export declare class RequestError extends Error { | ||
code?: number; | ||
} | ||
interface TeenyRequest { | ||
(reqOpts: r.OptionsWithUri, callback: Callback): void; | ||
defaults: ((options: r.OptionsWithUri) => ((reqOpts: r.OptionsWithUri, callback: Callback) => void)); | ||
declare function teenyRequest(reqOpts: r.Options): PassThrough; | ||
declare namespace teenyRequest { | ||
var defaults: (defaults: r.OptionalUriUrl) => (reqOpts: r.Options, callback?: r.RequestCallback | undefined) => void | PassThrough; | ||
} | ||
declare const teenyRequest: TeenyRequest; | ||
declare function teenyRequest(reqOpts: r.Options, callback: r.RequestCallback): void; | ||
declare namespace teenyRequest { | ||
var defaults: (defaults: r.OptionalUriUrl) => (reqOpts: r.Options, callback?: r.RequestCallback | undefined) => void | PassThrough; | ||
} | ||
export { teenyRequest }; |
@@ -1,2 +0,15 @@ | ||
'use strict'; | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
} | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || function () { | ||
@@ -15,11 +28,24 @@ __assign = Object.assign || function(t) { | ||
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 RequestError = /** @class */ (function (_super) { | ||
__extends(RequestError, _super); | ||
function RequestError() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return RequestError; | ||
}(Error)); | ||
exports.RequestError = RequestError; | ||
/** | ||
* Convert options from Request to Fetch format | ||
* @private | ||
* @param reqOpts Request options | ||
*/ | ||
function requestToFetchOptions(reqOpts) { | ||
var options = __assign({ method: reqOpts.method || 'GET' }, reqOpts.timeout && { timeout: reqOpts.timeout }, reqOpts.gzip && { compress: reqOpts.gzip }); | ||
if (typeof reqOpts.json === 'object') { | ||
// Add Content-type: application/json header | ||
if (!reqOpts.headers) { | ||
reqOpts.headers = {}; | ||
} | ||
reqOpts.headers = reqOpts.headers || {}; | ||
reqOpts.headers['Content-Type'] = 'application/json'; | ||
@@ -38,3 +64,4 @@ // Set body to JSON representation of value | ||
options.headers = reqOpts.headers; | ||
var uri = reqOpts.uri; | ||
var uri = (reqOpts.uri || | ||
reqOpts.url); | ||
if (reqOpts.useQuerystring === true || typeof reqOpts.qs === 'object') { | ||
@@ -49,26 +76,152 @@ var qs = require('querystring'); | ||
} | ||
return [uri, options]; | ||
}; | ||
var fetchToRequestResponse = function (res) { | ||
var response = { | ||
return { uri: uri, options: options }; | ||
} | ||
/** | ||
* Convert a response from `fetch` to `request` format. | ||
* @private | ||
* @param opts The `request` options used to create the request. | ||
* @param res The Fetch response | ||
* @returns A `request` response object | ||
*/ | ||
function fetchToRequestResponse(opts, res) { | ||
var request = res.body; | ||
request.headers = opts.headers || {}; | ||
request.href = res.url; | ||
return { | ||
statusCode: res.status, | ||
statusMessage: res.statusText, | ||
request: request, | ||
body: res.body | ||
}; | ||
return response; | ||
}; | ||
var teenyRequest = (function (reqOpts, callback) { | ||
var _a = requestToFetchOptions(reqOpts), uri = _a[0], options = _a[1]; | ||
} | ||
/** | ||
* Create POST body from two parts as multipart/related content-type | ||
* @private | ||
* @param boundary | ||
* @param multipart | ||
*/ | ||
function createMultipartStream(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; | ||
} | ||
function teenyRequest(reqOpts, callback) { | ||
var _a = requestToFetchOptions(reqOpts), uri = _a.uri, options = _a.options; | ||
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'); | ||
var response = fetchToRequestResponse(reqOpts, res); | ||
var body = response.body; | ||
if (header === 'application/json' || | ||
header === 'application/json; charset=utf-8') { | ||
res.json() | ||
.then(function (json) { | ||
response.body = json; | ||
callback(null, response, json); | ||
}) | ||
.catch(function (err) { | ||
callback(err, response, body); | ||
}); | ||
return; | ||
} | ||
res.text() | ||
.then(function (text) { | ||
response.body = text; | ||
callback(null, response, text); | ||
}) | ||
.catch(function (err) { | ||
callback(err, response, body); | ||
}); | ||
}) | ||
.catch(function (err) { | ||
callback(err, null, null); | ||
}); | ||
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) { | ||
var error = new RequestError(text); | ||
error.code = res.status; | ||
requestStream_1.emit('error', error); | ||
return; | ||
}) | ||
.catch(function (error) { | ||
requestStream_1.emit('error', error); | ||
}); | ||
return; | ||
} | ||
res.body.on('error', function (err) { | ||
console.log('whoa there was an error, passing it on: ' + err); | ||
requestStream_1.emit('error', err); | ||
}); | ||
var headers = Object.assign({}, res.headers.raw()); | ||
requestStream_1.emit('response', { | ||
headers: headers, | ||
statusCode: res.status, | ||
statusMessage: res.statusText, | ||
}); | ||
}) | ||
.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) | ||
.then(function (res) { | ||
var header = res.headers.get('content-type'); | ||
var response = fetchToRequestResponse(reqOpts, res); | ||
var body = response.body; | ||
if (header === 'application/json' || | ||
header === 'application/json; charset=utf-8') { | ||
var response_1 = fetchToRequestResponse(res); | ||
if (response.statusCode === 204) { | ||
// Probably a DELETE | ||
callback(null, response, body); | ||
return; | ||
} | ||
res.json() | ||
.then(function (json) { | ||
response_1.body = json; | ||
callback(null, response_1, json); | ||
response.body = json; | ||
callback(null, response, json); | ||
}) | ||
.catch(function (err) { | ||
callback(err); | ||
callback(err, response, body); | ||
}); | ||
@@ -79,3 +232,3 @@ return; | ||
.then(function (text) { | ||
var response = fetchToRequestResponse(res); | ||
var response = fetchToRequestResponse(reqOpts, res); | ||
response.body = text; | ||
@@ -85,15 +238,20 @@ callback(null, response, text); | ||
.catch(function (err) { | ||
callback(err); | ||
callback(err, response, body); | ||
}); | ||
}) | ||
.catch(function (err) { | ||
callback(err); | ||
callback(err, null, null); | ||
}); | ||
}); | ||
return; | ||
} | ||
exports.teenyRequest = teenyRequest; | ||
teenyRequest.defaults = function (defaults) { | ||
return function (reqOpts, callback) { | ||
teenyRequest(__assign({}, defaults, reqOpts), callback); | ||
var opts = __assign({}, defaults, reqOpts); | ||
if (callback === undefined) { | ||
return teenyRequest(opts); | ||
} | ||
teenyRequest(opts, callback); | ||
}; | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "teeny-request", | ||
"version": "3.9.0", | ||
"version": "3.9.1", | ||
"description": "Like request, but smaller.", | ||
"main": "./build/src/index.js", | ||
"types": "./build/src/index.d.ts", | ||
"scripts": { | ||
"test": "mocha build/test", | ||
"test": "nyc mocha build/test", | ||
"compile": "tsc -p .", | ||
@@ -14,3 +15,4 @@ "pretest": "npm run compile", | ||
"prepare": "npm run compile", | ||
"posttest": "npm run check" | ||
"posttest": "npm run check", | ||
"coverage": "nyc report --reporter=json && codecov -f coverage/*.json" | ||
}, | ||
@@ -38,12 +40,23 @@ "files": [ | ||
"https-proxy-agent": "^2.2.1", | ||
"node-fetch": "^2.2.0" | ||
"node-fetch": "^2.2.0", | ||
"uuid": "^3.3.2" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^5.2.5", | ||
"@types/nock": "^9.3.0", | ||
"@types/node-fetch": "^2.1.2", | ||
"@types/request": "^2.47.1", | ||
"@types/uuid": "^3.4.4", | ||
"codecov": "^3.1.0", | ||
"gts": "^0.8.0", | ||
"mocha": "^5.2.0", | ||
"nock": "^10.0.2", | ||
"nyc": "^13.1.0", | ||
"typescript": "^3.0.1" | ||
}, | ||
"nyc": { | ||
"exclude": [ | ||
"build/test" | ||
] | ||
} | ||
} |
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
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
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
32510
266
3
11
2
+ Addeduuid@^3.3.2
+ Addeduuid@3.4.0(transitive)