Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

http-call

Package Overview
Dependencies
Maintainers
3
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-call - npm Package Compare versions

Comparing version 5.1.0 to 5.1.1

159

lib/http.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const uri = require("url");

@@ -129,6 +128,4 @@ const util = require("util");

*/
static patch(url, options = {}) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this.request(url, Object.assign({}, options, { method: 'PATCH' }));
});
static async patch(url, options = {}) {
return this.request(url, Object.assign({}, options, { method: 'PATCH' }));
}

@@ -146,6 +143,4 @@ /**

*/
static delete(url, options = {}) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
return this.request(url, Object.assign({}, options, { method: 'DELETE' }));
});
static async delete(url, options = {}) {
return this.request(url, Object.assign({}, options, { method: 'DELETE' }));
}

@@ -167,8 +162,6 @@ /**

}
static request(url, options = {}) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
let http = new this(url, options);
yield http._request();
return http;
});
static async request(url, options = {}) {
let http = new this(url, options);
await http._request();
return http;
}

@@ -210,62 +203,56 @@ get method() {

}
_request() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
this._debugRequest();
try {
this.response = yield this._performRequest();
}
catch (err) {
debug(err);
return this._maybeRetry(err);
}
if (this._shouldParseResponseBody)
yield this._parse();
this._debugResponse();
if (this._responseRedirect)
return this._redirect();
if (!this._responseOK) {
throw new HTTPError(this);
}
if (!this.partial)
yield this._getNextRange();
});
async _request() {
this._debugRequest();
try {
this.response = await this._performRequest();
}
catch (err) {
debug(err);
return this._maybeRetry(err);
}
if (this._shouldParseResponseBody)
await this._parse();
this._debugResponse();
if (this._responseRedirect)
return this._redirect();
if (!this._responseOK) {
throw new HTTPError(this);
}
if (!this.partial)
await this._getNextRange();
}
_redirect() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
this._redirectRetries++;
if (this._redirectRetries > 10)
throw new Error(`Redirect loop at ${this.url}`);
if (!this.headers.location)
throw new Error(`Redirect from ${this.url} has no location header`);
const location = this.headers.location;
if (Array.isArray(location)) {
this.url = location[0];
}
else {
this.url = location;
}
yield this._request();
});
async _redirect() {
this._redirectRetries++;
if (this._redirectRetries > 10)
throw new Error(`Redirect loop at ${this.url}`);
if (!this.headers.location)
throw new Error(`Redirect from ${this.url} has no location header`);
const location = this.headers.location;
if (Array.isArray(location)) {
this.url = location[0];
}
else {
this.url = location;
}
await this._request();
}
_maybeRetry(err) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
this._errorRetries++;
const allowed = (err) => {
if (this._errorRetries > 5)
return false;
if (!err.code)
return false;
if (err.code === 'ENOTFOUND')
return true;
return require('is-retry-allowed')(err);
};
if (allowed(err)) {
let noise = Math.random() * 100;
// tslint:disable-next-line
yield this._wait((1 << this._errorRetries) * 100 + noise);
yield this._request();
return;
}
throw err;
});
async _maybeRetry(err) {
this._errorRetries++;
const allowed = (err) => {
if (this._errorRetries > 5)
return false;
if (!err.code)
return false;
if (err.code === 'ENOTFOUND')
return true;
return require('is-retry-allowed')(err);
};
if (allowed(err)) {
let noise = Math.random() * 100;
// tslint:disable-next-line
await this._wait((1 << this._errorRetries) * 100 + noise);
await this._request();
return;
}
throw err;
}

@@ -309,9 +296,7 @@ _debugRequest() {

}
_parse() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
this.body = yield concat(this.response);
let json = this.response.headers['content-type'] && deps_1.deps.contentType.parse(this.response).type.startsWith('application/json');
if (json)
this.body = JSON.parse(this.body);
});
async _parse() {
this.body = await concat(this.response);
let json = this.response.headers['content-type'] && deps_1.deps.contentType.parse(this.response).type.startsWith('application/json');
if (json)
this.body = JSON.parse(this.body);
}

@@ -334,10 +319,8 @@ _parseBody(body) {

}
_getNextRange() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const next = this.headers['next-range'];
this.options.headers.range = Array.isArray(next) ? next[0] : next;
let prev = this.body;
yield this._request();
this.body = prev.concat(this.body);
});
async _getNextRange() {
const next = this.headers['next-range'];
this.options.headers.range = Array.isArray(next) ? next[0] : next;
let prev = this.body;
await this._request();
this.body = prev.concat(this.body);
}

@@ -344,0 +327,0 @@ _redactedHeaders(headers) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const nock = require("nock");

@@ -19,39 +18,39 @@ const querystring = require("querystring");

describe('HTTP.get()', () => {
test('makes a GET request', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
test('makes a GET request', async () => {
api.get('/').reply(200, { message: 'ok' });
let { body } = yield http_1.HTTP.get('https://api.jdxcode.com');
let { body } = await http_1.HTTP.get('https://api.jdxcode.com');
expect(body).toEqual({ message: 'ok' });
}));
test('makes a GET request', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('makes a GET request', async () => {
api.get('/').reply(200, { message: 'ok' }, {
'content-type': 'application/json; charset=UTF-8',
});
let { body } = yield http_1.HTTP.get('https://api.jdxcode.com');
let { body } = await http_1.HTTP.get('https://api.jdxcode.com');
expect(body).toEqual({ message: 'ok' });
}));
test('gets headers', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('gets headers', async () => {
api.get('/').reply(200, { message: 'ok' }, { myheader: 'ok' });
let { body, headers } = yield http_1.HTTP.get('https://api.jdxcode.com');
let { body, headers } = await http_1.HTTP.get('https://api.jdxcode.com');
expect(body).toEqual({ message: 'ok' });
expect(headers).toMatchObject({ myheader: 'ok' });
}));
test('can build a new HTTP with defaults', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('can build a new HTTP with defaults', async () => {
const MyHTTP = http_1.HTTP.create({ host: 'api.jdxcode.com' });
api.get('/').reply(200, { message: 'ok' });
let { body } = yield MyHTTP.get('/');
let { body } = await MyHTTP.get('/');
expect(body).toEqual({ message: 'ok' });
}));
test('makes a request to a port', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('makes a request to a port', async () => {
api = nock('https://api.jdxcode.com:3000');
api.get('/').reply(200, { message: 'ok' });
let { body } = yield http_1.HTTP.get('https://api.jdxcode.com:3000');
let { body } = await http_1.HTTP.get('https://api.jdxcode.com:3000');
expect(body).toEqual({ message: 'ok' });
}));
test('makes a http GET request', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('makes a http GET request', async () => {
api = nock('http://api.jdxcode.com');
api.get('/').reply(200, { message: 'ok' });
let { body } = yield http_1.HTTP.get('http://api.jdxcode.com');
let { body } = await http_1.HTTP.get('http://api.jdxcode.com');
expect(body).toEqual({ message: 'ok' });
}));
test('can set default user agent', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('can set default user agent', async () => {
http_1.HTTP.defaults.headers = { 'user-agent': 'mynewuseragent' };

@@ -62,7 +61,7 @@ api

.reply(200, { message: 'ok' });
let { body } = yield http_1.HTTP.get('https://api.jdxcode.com/');
let { body } = await http_1.HTTP.get('https://api.jdxcode.com/');
expect(body).toEqual({ message: 'ok' });
delete http_1.HTTP.defaults.headers['user-agent'];
}));
test('can set user agent as a global', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('can set user agent as a global', async () => {
global['http-call'] = { userAgent: 'mynewuseragent' };

@@ -73,7 +72,7 @@ api

.reply(200, { message: 'ok' });
let { body } = yield http_1.HTTP.get('https://api.jdxcode.com/');
let { body } = await http_1.HTTP.get('https://api.jdxcode.com/');
expect(body).toEqual({ message: 'ok' });
delete global['http-call'];
}));
test('sets user-agent header', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('sets user-agent header', async () => {
api

@@ -83,5 +82,5 @@ .matchHeader('user-agent', `http-call/${require('../package.json').version} node-${process.version}`)

.reply(200, { message: 'ok' });
yield http_1.HTTP.get('https://api.jdxcode.com');
}));
test('sets custom headers', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
await http_1.HTTP.get('https://api.jdxcode.com');
});
test('sets custom headers', async () => {
api

@@ -92,9 +91,9 @@ .matchHeader('foo', 'bar')

let headers = { foo: 'bar' };
yield http_1.HTTP.get('https://api.jdxcode.com', { headers });
}));
test('does not fail on undefined header', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
await http_1.HTTP.get('https://api.jdxcode.com', { headers });
});
test('does not fail on undefined header', async () => {
api.get('/').reply(200);
let headers = { foo: undefined };
yield http_1.HTTP.get('https://api.jdxcode.com', { headers });
}));
await http_1.HTTP.get('https://api.jdxcode.com', { headers });
});
describe('wait mocked out', () => {

@@ -108,3 +107,3 @@ let wait = http_1.HTTP.prototype._wait;

});
test('retries then succeeds', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
test('retries then succeeds', async () => {
api.get('/').replyWithError({ message: 'timed out 1', code: 'ETIMEDOUT' });

@@ -115,6 +114,6 @@ api.get('/').replyWithError({ message: 'timed out 2', code: 'ETIMEDOUT' });

api.get('/').reply(200, { message: 'foo' });
let { body } = yield http_1.HTTP.get('https://api.jdxcode.com');
let { body } = await http_1.HTTP.get('https://api.jdxcode.com');
expect(body).toEqual({ message: 'foo' });
}));
test('retries 5 times on ETIMEDOUT', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('retries 5 times on ETIMEDOUT', async () => {
expect.assertions(1);

@@ -128,3 +127,3 @@ api.get('/').replyWithError({ message: 'timed out 1', code: 'ETIMEDOUT' });

try {
yield http_1.HTTP.get('https://api.jdxcode.com');
await http_1.HTTP.get('https://api.jdxcode.com');
}

@@ -134,15 +133,15 @@ catch (err) {

}
}));
});
});
test('retries on ENOTFOUND', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
test('retries on ENOTFOUND', async () => {
api.get('/').replyWithError({ message: 'not found', code: 'ENOTFOUND' });
api.get('/').reply(200, { message: 'foo' });
let { body } = yield http_1.HTTP.get('https://api.jdxcode.com');
let { body } = await http_1.HTTP.get('https://api.jdxcode.com');
expect(body).toMatchObject({ message: 'foo' });
}));
test('errors on EFOOBAR', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('errors on EFOOBAR', async () => {
expect.assertions(1);
api.get('/').replyWithError({ message: 'oom', code: 'OUT_OF_MEM' });
try {
yield http_1.HTTP.get('https://api.jdxcode.com');
await http_1.HTTP.get('https://api.jdxcode.com');
}

@@ -152,8 +151,8 @@ catch (err) {

}
}));
test('displays 404 error', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('displays 404 error', async () => {
expect.assertions(2);
api.get('/').reply(404, 'oops! not found');
try {
yield http_1.HTTP.get('https://api.jdxcode.com');
await http_1.HTTP.get('https://api.jdxcode.com');
}

@@ -165,8 +164,8 @@ catch (err) {

}
}));
test('displays error message', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('displays error message', async () => {
expect.assertions(3);
api.get('/').reply(404, { message: 'uh oh', otherinfo: [1, 2, 3] });
try {
yield http_1.HTTP.get('https://api.jdxcode.com');
await http_1.HTTP.get('https://api.jdxcode.com');
}

@@ -179,8 +178,8 @@ catch (err) {

}
}));
test('displays object error', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('displays object error', async () => {
expect.assertions(3);
api.get('/').reply(404, { otherinfo: [1, 2, 3] });
try {
yield http_1.HTTP.get('https://api.jdxcode.com');
await http_1.HTTP.get('https://api.jdxcode.com');
}

@@ -193,10 +192,10 @@ catch (err) {

}
}));
test('follows redirect', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('follows redirect', async () => {
api.get('/foo1').reply(302, null, { Location: 'https://api.jdxcode.com/foo2' });
api.get('/foo2').reply(302, null, { Location: 'https://api.jdxcode.com/foo3' });
api.get('/foo3').reply(200, { success: true });
yield http_1.HTTP.get('https://api.jdxcode.com/foo1');
}));
test('follows redirect only 10 times', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
await http_1.HTTP.get('https://api.jdxcode.com/foo1');
});
test('follows redirect only 10 times', async () => {
api.get('/foo1').reply(302, null, { Location: 'https://api.jdxcode.com/foo2' });

@@ -215,3 +214,3 @@ api.get('/foo2').reply(302, null, { Location: 'https://api.jdxcode.com/foo3' });

try {
yield http_1.HTTP.get('https://api.jdxcode.com/foo1');
await http_1.HTTP.get('https://api.jdxcode.com/foo1');
}

@@ -221,16 +220,16 @@ catch (err) {

}
}));
});
});
describe('HTTP.post()', () => {
test('makes a POST request', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
test('makes a POST request', async () => {
api.post('/', { foo: 'bar' }).reply(200, { message: 'ok' });
let { body } = yield http_1.HTTP.post('https://api.jdxcode.com', { body: { foo: 'bar' } });
let { body } = await http_1.HTTP.post('https://api.jdxcode.com', { body: { foo: 'bar' } });
expect(body).toEqual({ message: 'ok' });
}));
test('does not include a body if no body is passed in', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('does not include a body if no body is passed in', async () => {
api.post('/').reply(200, { message: 'ok' });
let { body } = yield http_1.HTTP.post('https://api.jdxcode.com');
let { body } = await http_1.HTTP.post('https://api.jdxcode.com');
expect(body).toEqual({ message: 'ok' });
}));
test('faithfully passes custom-encoded content-types', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
});
test('faithfully passes custom-encoded content-types', async () => {
let apiEncoded = nock('https://api.jdxcode.com', {

@@ -254,5 +253,5 @@ reqheaders: {

apiEncoded.post('/', querystring.stringify(body)).reply(200, { message: 'ok' });
let rsp = yield http_1.HTTP.post('https://api.jdxcode.com/', options);
let rsp = await http_1.HTTP.post('https://api.jdxcode.com/', options);
expect(rsp.body).toEqual({ message: 'ok' });
}));
});
});

@@ -301,2 +300,3 @@ describe('HTTP.parseBody()', () => {

.get('/')
// .matchHeader('range', '4')
.reply(206, [4, 5, 6], {

@@ -306,40 +306,41 @@ 'next-range': '7',

.get('/')
// .matchHeader('range', '7')
.reply(206, [7, 8, 9]);
});
test('gets next body when next-range is set', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
let { body } = yield http_1.HTTP.get('https://api.jdxcode.com');
test('gets next body when next-range is set', async () => {
let { body } = await http_1.HTTP.get('https://api.jdxcode.com');
expect(body).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
}));
});
});
});
describe('HTTP.put()', () => {
test('makes a PUT request', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
test('makes a PUT request', async () => {
api.put('/', { foo: 'bar' }).reply(200, { message: 'ok' });
let { body } = yield http_1.HTTP.put('https://api.jdxcode.com', { body: { foo: 'bar' } });
let { body } = await http_1.HTTP.put('https://api.jdxcode.com', { body: { foo: 'bar' } });
expect(body).toEqual({ message: 'ok' });
}));
});
});
describe('HTTP.patch()', () => {
test('makes a PATCH request', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
test('makes a PATCH request', async () => {
api.patch('/', { foo: 'bar' }).reply(200, { message: 'ok' });
let { body } = yield http_1.HTTP.patch('https://api.jdxcode.com', { body: { foo: 'bar' } });
let { body } = await http_1.HTTP.patch('https://api.jdxcode.com', { body: { foo: 'bar' } });
expect(body).toEqual({ message: 'ok' });
}));
});
});
describe('HTTP.delete()', () => {
test('makes a DELETE request', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
test('makes a DELETE request', async () => {
api.delete('/', { foo: 'bar' }).reply(200, { message: 'ok' });
let { body } = yield http_1.HTTP.delete('https://api.jdxcode.com', { body: { foo: 'bar' } });
let { body } = await http_1.HTTP.delete('https://api.jdxcode.com', { body: { foo: 'bar' } });
expect(body).toEqual({ message: 'ok' });
}));
});
});
describe('HTTP.stream()', () => {
test('streams a response', (done) => tslib_1.__awaiter(this, void 0, void 0, function* () {
test('streams a response', async (done) => {
api = nock('http://api.jdxcode.com');
api.get('/').reply(200, { message: 'ok' });
let { response } = yield http_1.HTTP.stream('http://api.jdxcode.com');
let { response } = await http_1.HTTP.stream('http://api.jdxcode.com');
response.setEncoding('utf8');
response.on('data', data => expect(data).toEqual('{"message":"ok"}'));
response.on('end', done);
}));
});
});
{
"name": "http-call",
"description": "make http requests",
"version": "5.1.0",
"version": "5.1.1",
"author": "Jeff Dickey @jdxcode",

@@ -12,3 +12,2 @@ "bugs": "https://github.com/heroku/http-call/issues",

"is-stream": "^1.1.0",
"tslib": "^1.9.0",
"tunnel-agent": "^0.6.0"

@@ -20,10 +19,10 @@ },

"@types/is-stream": "^1.1.0",
"@types/jest": "^22.2.2",
"@types/nock": "^9.1.2",
"@types/node": "^9.6.0",
"@types/jest": "^22.2.3",
"@types/nock": "^9.1.3",
"@types/node": "^9.6.6",
"jest": "^22.4.3",
"nock": "^9.2.3",
"ts-jest": "^22.4.2",
"nock": "^9.2.5",
"ts-jest": "^22.4.4",
"tslint": "^5.9.1",
"typescript": "^2.7.2"
"typescript": "^2.8.3"
},

@@ -30,0 +29,0 @@ "engines": {

@@ -11,3 +11,5 @@ http-call

const {HTTP} = require('http-call')
const {body} = await HTTP.get('https://api.github.com')
const {body: user} = await HTTP.get('https://api.github.com/users/me')
// do something with user
// automatically converts from json

@@ -14,0 +16,0 @@ // set headers

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc