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

xfetch

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xfetch - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

dist/attemptRequest.js

70

dist/index.js

@@ -6,25 +6,6 @@ 'use strict';

});
exports.Response = exports.Request = exports.Headers = undefined;
exports.validateResponse = exports.UnexpectedResponseError = exports.UnexpectedResponseCodeError = exports.Response = exports.Request = exports.Headers = undefined;
var _nodeFetch = require('node-fetch');
Object.defineProperty(exports, 'Headers', {
enumerable: true,
get: function get() {
return _nodeFetch.Headers;
}
});
Object.defineProperty(exports, 'Request', {
enumerable: true,
get: function get() {
return _nodeFetch.Request;
}
});
Object.defineProperty(exports, 'Response', {
enumerable: true,
get: function get() {
return _nodeFetch.Response;
}
});
var _nodeFetch2 = _interopRequireDefault(_nodeFetch);

@@ -40,19 +21,50 @@

var _attemptRequest = require('./attemptRequest');
var _attemptRequest2 = _interopRequireDefault(_attemptRequest);
var _errors = require('./errors');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /* eslint-disable no-process-env */
const debug = (0, _debug2.default)('xfetch');
exports.default = (url, options = {}) => {
if (process.env.HTTP_PROXY) {
debug('using proxy %s', process.env.HTTP_PROXY);
const validateResponse = response => {
if (!String(response.status).startsWith(2)) {
throw new _errors.UnexpectedResponseCodeError(response);
}
if (process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0') {
throw new Error('Must configure NODE_TLS_REJECT_UNAUTHORIZED.');
return true;
};
exports.default = (() => {
var _ref = _asyncToGenerator(function* (url, options = {}) {
if (process.env.HTTP_PROXY) {
debug('using proxy %s', process.env.HTTP_PROXY);
if (process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0') {
throw new Error('Must configure NODE_TLS_REJECT_UNAUTHORIZED.');
}
options.agent = new _httpsProxyAgent2.default(process.env.HTTP_PROXY);
}
options.agent = new _httpsProxyAgent2.default(process.env.HTTP_PROXY);
}
return (0, _attemptRequest2.default)(function () {
return (0, _nodeFetch2.default)(url, options);
}, options.validateResponse || validateResponse, options.retry);
});
return (0, _nodeFetch2.default)(url, options);
};
return function (_x) {
return _ref.apply(this, arguments);
};
})();
exports.Headers = _nodeFetch.Headers;
exports.Request = _nodeFetch.Request;
exports.Response = _nodeFetch.Response;
exports.UnexpectedResponseCodeError = _errors.UnexpectedResponseCodeError;
exports.UnexpectedResponseError = _errors.UnexpectedResponseError;
exports.validateResponse = validateResponse;
//# sourceMappingURL=index.js.map

@@ -13,2 +13,9 @@ {

},
"dependencies": {
"debug": "^2.6.1",
"es6-error": "^4.0.2",
"https-proxy-agent": "^1.0.0",
"node-fetch": "^2.0.0-alpha.3",
"retry": "^0.10.1"
},
"description": "A light-weight module that brings window.fetch to Node.js (with HTTP_PROXY support).",

@@ -18,5 +25,13 @@ "devDependencies": {

"babel-cli": "^6.23.0",
"babel-plugin-istanbul": "^4.0.0",
"babel-plugin-transform-flow-comments": "^6.22.0",
"babel-preset-env": "^1.1.10",
"babel-register": "^6.23.0",
"coveralls": "^2.11.16",
"eslint": "^3.16.1",
"eslint-config-canonical": "^7.1.0",
"flow-bin": "^0.40.0",
"husky": "^0.13.1",
"nock": "^9.0.7",
"nyc": "^10.1.2",
"semantic-release": "^6.3.6"

@@ -33,2 +48,15 @@ },

"name": "xfetch",
"nyc": {
"include": [
"src/**/*.js"
],
"instrument": false,
"reporter": [
"text-lcov"
],
"require": [
"babel-register"
],
"sourceMap": false
},
"repository": {

@@ -40,11 +68,7 @@ "type": "git",

"build": "NODE_ENV=production babel ./src --out-dir ./dist --copy-files --source-maps",
"precommit": "npm run test",
"test": "NODE_ENV=development ava --verbose --serial"
"lint": "eslint ./src ./test && flow",
"precommit": "npm run lint && npm run test && npm run build",
"test": "NODE_ENV=development nyc --reporter=text ava --verbose --serial"
},
"version": "1.0.0",
"dependencies": {
"debug": "^2.6.1",
"https-proxy-agent": "^1.0.0",
"node-fetch": "^2.0.0-alpha.3"
}
"version": "1.1.0"
}
# xfetch
[![Travis build status](http://img.shields.io/travis/gajus/xfetch/master.svg?style=flat-square)](https://travis-ci.org/gajus/xfetch)
[![Coveralls](https://img.shields.io/coveralls/gajus/xfetch.svg?style=flat-square)](https://coveralls.io/github/gajus/xfetch)
[![NPM version](http://img.shields.io/npm/v/xfetch.svg?style=flat-square)](https://www.npmjs.org/package/xfetch)

@@ -13,2 +14,6 @@ [![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)

## Behaviour
### HTTP proxy
Configure `HTTP_PROXY` (`http://host:port`) environment variable to proxy the requests.

@@ -18,1 +23,68 @@

> This is a lazy workaround to enable the proxy to work with TLS.
### Throws error if response code is non-2xx
Throws `UnexpectedResponseCodeError` error if response code is non-2xx.
### Request retry
Requests that result in non-2xx response will be retried.
`retry` option is used to configure request retry strategy.
The `retry` configuration shape matches [configuration of the `retry`](https://github.com/tim-kos/node-retry) module.
### Validating response
Define a custom validator function to force use the request retry strategy.
A custom validator is configured using `validateResponse` configuration, e.g.
```js
import xfetch, {
UnexpectedResponseError
};
const validateResponse = async (response) => {
const body = await response.text();
if (body.includes('rate error')) {
throw new UnexpectedResponseError(response);
}
return true;
}
xfetch('http://gajus.com', {validateResponse});
```
A custom validator must return a boolean flag indicating whether the response is valid. A custom validator can throw an error that extends `UnexpectedResponseError` error.
`xfetch` default validator can be imported and used to extend a custom validator, e.g.
```js
import xfetch, {
UnexpectedResponseError,
validateResponse as defaultValidateResponse
};
const validateResponse = async (response) => {
const responseIsValid = await defaultValidateResponse(response);
if (!responseIsValid) {
return responseIsValid;
}
const body = await response.text();
if (body.includes('rate error')) {
throw new UnexpectedResponseError(response);
}
return true;
}
xfetch('http://gajus.com', {validateResponse});
```

Sorry, the diff of this file is not supported yet

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