teeny-request
Advanced tools
Comparing version 3.11.0 to 3.11.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 { Stream } 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): Stream; | ||
declare namespace teenyRequest { | ||
var defaults: (defaults: r.OptionalUriUrl) => (reqOpts: r.Options, callback?: r.RequestCallback | undefined) => void | Stream; | ||
} | ||
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 | Stream; | ||
} | ||
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 () { | ||
@@ -19,9 +32,20 @@ __assign = Object.assign || function(t) { | ||
var HttpsProxyAgent = require('https-proxy-agent'); | ||
var requestToFetchOptions = function (reqOpts) { | ||
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'; | ||
@@ -40,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') { | ||
@@ -51,13 +76,33 @@ 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; | ||
// headers need to be converted from a map to an obj | ||
var headers = {}; | ||
res.headers.forEach(function (value, key) { return headers[key] = value; }); | ||
return { | ||
statusCode: res.status, | ||
statusMessage: res.statusText, | ||
request: request, | ||
body: res.body, | ||
headers: headers, | ||
}; | ||
return response; | ||
}; | ||
// create POST body from two parts as multipart/related content-type | ||
var createMultipartStream = function (boundary, multipart) { | ||
} | ||
/** | ||
* Create POST body from two parts as multipart/related content-type | ||
* @private | ||
* @param boundary | ||
* @param multipart | ||
*/ | ||
function createMultipartStream(boundary, multipart) { | ||
var finale = "--" + boundary + "--"; | ||
@@ -83,5 +128,5 @@ var stream = new stream_1.PassThrough(); | ||
return stream; | ||
}; | ||
var teenyRequest = (function (reqOpts, callback) { | ||
var _a = requestToFetchOptions(reqOpts), uri = _a[0], options = _a[1]; | ||
} | ||
function teenyRequest(reqOpts, callback) { | ||
var _a = requestToFetchOptions(reqOpts), uri = _a.uri, options = _a.options; | ||
var multipart = reqOpts.multipart; | ||
@@ -101,27 +146,22 @@ if (reqOpts.multipart && multipart.length === 2) { | ||
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); | ||
res.json() | ||
.then(function (json) { | ||
response_1.body = json; | ||
callback(null, response_1, json); | ||
}) | ||
.catch(function (err) { | ||
callback(err); | ||
res.json().then(function (json) { | ||
response.body = json; | ||
callback(null, response, json); | ||
}, function (err) { | ||
callback(err, response, body); | ||
}); | ||
return; | ||
} | ||
res.text() | ||
.then(function (text) { | ||
var response = fetchToRequestResponse(res); | ||
res.text().then(function (text) { | ||
response.body = text; | ||
callback(null, response, text); | ||
}) | ||
.catch(function (err) { | ||
callback(err); | ||
}, function (err) { | ||
callback(err, response, body); | ||
}); | ||
}) | ||
.catch(function (err) { | ||
callback(err); | ||
}, function (err) { | ||
callback(err, null, null); | ||
}); | ||
@@ -136,11 +176,8 @@ return; | ||
if (!res.ok) { | ||
res.text() | ||
.then(function (text) { | ||
// tslint:disable-next-line:no-any | ||
var error = new Error(text); | ||
res.text().then(function (text) { | ||
var error = new RequestError(text); | ||
error.code = res.status; | ||
requestStream_1.emit('error', error); | ||
return; | ||
}) | ||
.catch(function (error) { | ||
}, function (error) { | ||
requestStream_1.emit('error', error); | ||
@@ -150,3 +187,2 @@ }); | ||
} | ||
var encoding = res.headers.get('content-encoding'); | ||
res.body.on('error', function (err) { | ||
@@ -156,10 +192,10 @@ console.log('whoa there was an error, passing it on: ' + 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) { | ||
var headers = {}; | ||
res.headers.forEach(function (value, key) { return headers[key] = value; }); | ||
requestStream_1.emit('response', { | ||
headers: headers, | ||
statusCode: res.status, | ||
statusMessage: res.statusText, | ||
}); | ||
}, function (err) { | ||
console.log('such a nice error:' + err); | ||
@@ -177,41 +213,41 @@ requestStream_1.emit('error', err); | ||
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_2 = fetchToRequestResponse(res); | ||
if (response_2.statusCode === 204) { | ||
if (response.statusCode === 204) { | ||
// Probably a DELETE | ||
callback(null, response_2, response_2); | ||
callback(null, response, body); | ||
return; | ||
} | ||
res.json() | ||
.then(function (json) { | ||
response_2.body = json; | ||
callback(null, response_2, json); | ||
}) | ||
.catch(function (err) { | ||
callback(err); | ||
res.json().then(function (json) { | ||
response.body = json; | ||
callback(null, response, json); | ||
}, function (err) { | ||
callback(err, response, body); | ||
}); | ||
return; | ||
} | ||
res.text() | ||
.then(function (text) { | ||
var response = fetchToRequestResponse(res); | ||
res.text().then(function (text) { | ||
var response = fetchToRequestResponse(reqOpts, res); | ||
response.body = text; | ||
callback(null, response, text); | ||
}) | ||
.catch(function (err) { | ||
callback(err); | ||
}, function (err) { | ||
callback(err, response, body); | ||
}); | ||
}) | ||
.catch(function (err) { | ||
callback(err); | ||
}, function (err) { | ||
callback(err, null, null); | ||
}); | ||
return; | ||
}); | ||
} | ||
exports.teenyRequest = teenyRequest; | ||
teenyRequest.defaults = function (defaults) { | ||
return function (reqOpts, callback) { | ||
return 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.11.0", | ||
"version": "3.11.1", | ||
"description": "Like request, but smaller.", | ||
@@ -8,3 +8,3 @@ "main": "./build/src/index.js", | ||
"scripts": { | ||
"test": "mocha build/test", | ||
"test": "nyc mocha build/test", | ||
"compile": "tsc -p .", | ||
@@ -16,3 +16,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" | ||
}, | ||
@@ -45,9 +46,18 @@ "files": [ | ||
"@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
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
32574
258
11