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

apisauce

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apisauce - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

README.md

280

dist/apisauce.js
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.responseToProblem = exports.create = exports.UNKNOWN_ERROR = exports.NETWORK_ERROR = exports.CONNECTION_ERROR = exports.TIMEOUT_ERROR = exports.SERVER_ERROR = exports.CLIENT_ERROR = exports.NONE = undefined;
Object.defineProperty(exports, '__esModule', { value: true });
var _axios = require('axios');
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var _axios2 = _interopRequireDefault(_axios);
var axios = _interopDefault(require('axios'));
var R = _interopDefault(require('ramda'));
var RS = _interopDefault(require('ramdasauce'));
var _ramda = require('ramda');
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
};
var _ramda2 = _interopRequireDefault(_ramda);
// check for an invalid config
var isInvalidConfig = R.anyPass([R.isNil, R.isEmpty, R.complement(R.has('baseURL')), R.complement(R.propIs(String, 'baseURL')), R.propSatisfies(R.isEmpty, 'baseURL')]);
var _ramdasauce = require('ramdasauce');
// the default headers given to axios
var DEFAULT_HEADERS = {
'Accept': 'application/json',
'Content-Type': 'application/json'
};
var _ramdasauce2 = _interopRequireDefault(_ramdasauce);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// check for an invalid config
var isInvalidConfig = _ramda2.default.anyPass([_ramda2.default.isNil, _ramda2.default.isEmpty, _ramda2.default.complement(_ramda2.default.has('baseURL')), _ramda2.default.complement(_ramda2.default.propIs(String, 'baseURL')), _ramda2.default.propSatisfies(_ramda2.default.isEmpty, 'baseURL')]);
// the default configuration for axios
// the default configuration for axios, default headers will also be merged in
var DEFAULT_CONFIG = {
timeout: 0,
headers: {}
timeout: 0
};

@@ -41,5 +41,5 @@

var NODEJS_CONNECTION_ERROR_CODES = ['ENOTFOUND', 'ECONNREFUSED', 'ECONNRESET'];
var in200s = _ramdasauce2.default.isWithin(200, 299);
var in400s = _ramdasauce2.default.isWithin(400, 499);
var in500s = _ramdasauce2.default.isWithin(500, 599);
var in200s = RS.isWithin(200, 299);
var in400s = RS.isWithin(400, 499);
var in500s = RS.isWithin(500, 599);

@@ -54,6 +54,11 @@ /**

// combine the user's defaults with ours
var combinedConfig = _ramda2.default.merge(DEFAULT_CONFIG, config);
var mergedHeaders = R.merge(DEFAULT_HEADERS, config.headers || {});
var combinedConfig = R.merge(DEFAULT_CONFIG, R.merge(config, { headers: mergedHeaders }));
// create the axios instance
var instance = _axios2.default.create(combinedConfig);
var instance = axios.create(combinedConfig);
// immediate reset headers because axios kept its own defaults
instance.defaults.headers = combinedConfig.headers;
var monitors = [];

@@ -64,2 +69,12 @@ var addMonitor = function addMonitor(monitor) {

var requestTransforms = [];
var responseTransforms = [];
var addRequestTransform = function addRequestTransform(transform) {
return requestTransforms.push(transform);
};
var addResponseTransform = function addResponseTransform(transform) {
return responseTransforms.push(transform);
};
// convenience for setting new request headers

@@ -73,4 +88,4 @@ var setHeader = function setHeader(name, value) {

var setHeaders = function setHeaders(headers) {
var keys = _ramda2.default.keys(headers);
_ramda2.default.forEach(function (header) {
var keys = R.keys(headers);
R.forEach(function (header) {
return setHeader(header, headers[header]);

@@ -81,110 +96,139 @@ }, keys);

// create the base object
var sauce = {
axiosInstance: instance,
monitors: monitors,
addMonitor: addMonitor,
setHeader: setHeader,
setHeaders: setHeaders
/**
Make the request for GET, HEAD, DELETE
*/
var doRequestWithoutBody = function doRequestWithoutBody(method, url) {
var params = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var axiosConfig = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
return doRequest(R.merge({ url: url, params: params, method: method }, axiosConfig));
};
// attach functions for each our HTTP verbs
sauce.get = _ramda2.default.partial(doRequestWithoutBody, [sauce, 'get']);
sauce.delete = _ramda2.default.partial(doRequestWithoutBody, [sauce, 'delete']);
sauce.head = _ramda2.default.partial(doRequestWithoutBody, [sauce, 'head']);
sauce.post = _ramda2.default.partial(doRequestWithBody, [sauce, 'post']);
sauce.put = _ramda2.default.partial(doRequestWithBody, [sauce, 'put']);
sauce.patch = _ramda2.default.partial(doRequestWithBody, [sauce, 'patch']);
/**
Make the request for POST, PUT, PATCH
*/
var doRequestWithBody = function doRequestWithBody(method, url) {
var data = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
var axiosConfig = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
// send it back
return sauce;
};
if (!R.isNil(data) && requestTransforms.length > 0) {
var _ret = function () {
var clonedData = R.clone(data);
// give an opportunity for anything to the response transforms to change stuff along the way
R.forEach(function (transform) {
transform({ data: clonedData, method: method, url: url });
}, requestTransforms);
return {
v: doRequest(R.merge({ url: url, method: method, data: clonedData }, axiosConfig))
};
}();
/**
Make the request for GET, HEAD, DELETE
*/
var doRequestWithoutBody = function doRequestWithoutBody(api, method, url) {
var params = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
var axiosConfig = arguments.length <= 4 || arguments[4] === undefined ? {} : arguments[4];
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
} else {
return doRequest(R.merge({ url: url, method: method, data: data }, axiosConfig));
}
};
return doRequest(api, _ramda2.default.merge({ url: url, params: params, method: method }, axiosConfig));
};
/**
Make the request with this config!
*/
var doRequest = function doRequest(axiosRequestConfig) {
var startedAt = RS.toNumber(new Date());
/**
Make the request for POST, PUT, PATCH
*/
var doRequestWithBody = function doRequestWithBody(api, method, url) {
var data = arguments.length <= 3 || arguments[3] === undefined ? null : arguments[3];
var axiosConfig = arguments.length <= 4 || arguments[4] === undefined ? {} : arguments[4];
// first convert the axios response, then execute our callback
var chain = R.pipe(R.partial(convertResponse, [startedAt]), runMonitors);
return doRequest(api, _ramda2.default.merge({ url: url, method: method, data: data }, axiosConfig));
};
// Make the request and execute the identical pipeline for both promise paths.
return instance.request(axiosRequestConfig).then(chain).catch(chain);
};
/**
Make the request with this config!
*/
var doRequest = function doRequest(api, axiosRequestConfig) {
var axiosInstance = api.axiosInstance;
/**
Fires after we convert from axios' response into our response. Exceptions
raised for each monitor will be ignored.
*/
var runMonitors = function runMonitors(ourResponse) {
monitors.forEach(function (fn) {
try {
fn(ourResponse);
} catch (error) {
// all monitor complaints will be ignored
}
});
return ourResponse;
};
var startedAt = _ramdasauce2.default.toNumber(new Date());
/**
Converts an axios response/error into our response.
*/
var convertResponse = function convertResponse(startedAt, axiosResponse) {
var end = RS.toNumber(new Date());
var duration = end - startedAt;
// first convert the axios response, then execute our callback
var chain = _ramda2.default.pipe(_ramda2.default.partial(convertResponse, [startedAt]), _ramda2.default.partial(runMonitors, [api]));
// new in Axios 0.13 -- some data could be buried 1 level now
var isError = axiosResponse instanceof Error;
var response = isError ? axiosResponse.response : axiosResponse;
var status = response && response.status || null;
var problem = isError ? getProblemFromError(axiosResponse) : getProblemFromStatus(status);
var ok = in200s(status);
var config = axiosResponse.config || null;
var headers = response && response.headers || null;
var data = response && response.data || null;
// Make the request and execute the identical pipeline for both promise paths.
return axiosInstance.request(axiosRequestConfig).then(chain).catch(chain);
};
/**
Fires after we convert from axios' response into our response. Exceptions
raised for each monitor will be ignored.
*/
var runMonitors = function runMonitors(api, ourResponse) {
api.monitors.forEach(function (fn) {
try {
fn(ourResponse);
} catch (error) {
// all monitor complaints will be ignored
// give an opportunity for anything to the response transforms to change stuff along the way
if (responseTransforms.length > 0) {
R.forEach(function (transform) {
transform({ duration: duration, problem: problem, ok: ok, status: status, headers: headers, config: config, data: data });
}, responseTransforms);
}
});
return ourResponse;
};
/**
Converts an axios response/error into our response.
*/
var convertResponse = function convertResponse(startedAt, axiosResponse) {
var end = _ramdasauce2.default.toNumber(new Date());
var duration = end - startedAt;
return {
duration: duration,
problem: responseToProblem(axiosResponse),
ok: _ramda2.default.pipe(_ramda2.default.propOr(0, 'status'), in200s)(axiosResponse),
status: axiosResponse.status || null,
headers: axiosResponse.headers || null,
config: axiosResponse.config || null,
data: axiosResponse.data || null
return { duration: duration, problem: problem, ok: ok, status: status, headers: headers, config: config, data: data };
};
};
/**
What's the problem for this response?
TODO: We're losing some error granularity, but i'm cool with that
until someone cares.
*/
var responseToProblem = function responseToProblem(response) {
if (response instanceof Error) {
/**
What's the problem for this response?
TODO: We're losing some error granularity, but i'm cool with that
until someone cares.
*/
var getProblemFromError = function getProblemFromError(error) {
// first check if the error message is Network Error (set by axios at 0.12) on platforms other than NodeJS.
if (response.message === 'Network Error') return NETWORK_ERROR;
if (error.message === 'Network Error') return NETWORK_ERROR;
// then check the specific error code
return _ramda2.default.cond([[_ramda2.default.contains(_ramda2.default.__, TIMEOUT_ERROR_CODES), _ramda2.default.always(TIMEOUT_ERROR)], [_ramda2.default.contains(_ramda2.default.__, NODEJS_CONNECTION_ERROR_CODES), _ramda2.default.always(CONNECTION_ERROR)], [_ramda2.default.T, _ramda2.default.always(UNKNOWN_ERROR)]])(response.code);
}
if (_ramda2.default.isNil(response) || !_ramda2.default.has('status')) return UNKNOWN_ERROR;
return _ramda2.default.cond([[in200s, _ramda2.default.always(NONE)], [in400s, _ramda2.default.always(CLIENT_ERROR)], [in500s, _ramda2.default.always(SERVER_ERROR)], [_ramda2.default.T, _ramda2.default.always(UNKNOWN_ERROR)]])(response.status || 0);
return R.cond([
// if we don't have an error code, we have a response status
[R.isNil, function () {
return getProblemFromStatus(error.response.status);
}], [R.contains(R.__, TIMEOUT_ERROR_CODES), R.always(TIMEOUT_ERROR)], [R.contains(R.__, NODEJS_CONNECTION_ERROR_CODES), R.always(CONNECTION_ERROR)], [R.T, R.always(UNKNOWN_ERROR)]])(error.code);
};
/**
* Given a HTTP status code, return back the appropriate problem enum.
*/
var getProblemFromStatus = function getProblemFromStatus(status) {
return R.cond([[R.isNil, R.always(UNKNOWN_ERROR)], [in200s, R.always(NONE)], [in400s, R.always(CLIENT_ERROR)], [in500s, R.always(SERVER_ERROR)], [R.T, R.always(UNKNOWN_ERROR)]])(status);
};
// create the base object
var sauce = {
axiosInstance: instance,
monitors: monitors,
addMonitor: addMonitor,
requestTransforms: requestTransforms,
responseTransforms: responseTransforms,
addRequestTransform: addRequestTransform,
addResponseTransform: addResponseTransform,
setHeader: setHeader,
setHeaders: setHeaders,
get: R.partial(doRequestWithoutBody, ['get']),
delete: R.partial(doRequestWithoutBody, ['delete']),
head: R.partial(doRequestWithoutBody, ['head']),
post: R.partial(doRequestWithBody, ['post']),
put: R.partial(doRequestWithBody, ['put']),
patch: R.partial(doRequestWithBody, ['patch'])
};
// send back the sauce
return sauce;
};
module.exports = {
responseToProblem: responseToProblem,
create: create,
var apisauce = {
DEFAULT_HEADERS: DEFAULT_HEADERS,
NONE: NONE,

@@ -196,5 +240,7 @@ CLIENT_ERROR: CLIENT_ERROR,

NETWORK_ERROR: NETWORK_ERROR,
UNKNOWN_ERROR: UNKNOWN_ERROR
UNKNOWN_ERROR: UNKNOWN_ERROR,
create: create
};
exports.DEFAULT_HEADERS = DEFAULT_HEADERS;
exports.NONE = NONE;

@@ -208,2 +254,2 @@ exports.CLIENT_ERROR = CLIENT_ERROR;

exports.create = create;
exports.responseToProblem = responseToProblem;
exports['default'] = apisauce;
{
"name": "apisauce",
"version": "0.3.0",
"version": "0.4.0",
"description": "A JSON API client for JavaScript. No, no. Not like that. Like this.",

@@ -12,3 +12,3 @@ "repository": {

"test": "ava",
"test:watch": "npm run test -- --watch",
"watch": "npm run test -- --watch",
"coverage": "nyc ava",

@@ -18,3 +18,3 @@ "dist": "npm run clean && npm run build && npm run docs",

"clean": "rm -rf dist docs",
"build": "BABEL_ENV=production gulp build"
"build": "BABEL_ENV=production rollup -c"
},

@@ -33,23 +33,22 @@ "keywords": [

"dependencies": {
"axios": "^0.12.0",
"ramda": "^0.21.0",
"ramdasauce": "^1.0.0"
"axios": "^0.13.1",
"ramda": "^0.22.1",
"ramdasauce": "^1.1.1"
},
"devDependencies": {
"ava": "^0.15.1",
"babel-cli": "^6.6.5",
"babel-core": "^6.9.1",
"babel-eslint": "^6.1.0",
"babel-plugin-transform-runtime": "^6.9.0",
"babel-preset-es2015": "^6.6.0",
"ava": "^0.16.0",
"babel-cli": "^6.11.4",
"babel-core": "^6.13.2",
"babel-eslint": "^6.1.2",
"babel-plugin-transform-runtime": "^6.12.0",
"babel-preset-es2015": "^6.13.2",
"babel-preset-es2015-rollup": "^1.2.0",
"babel-preset-stage-0": "^6.5.0",
"docdash": "^0.4.0",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-rollup": "^2.1.0",
"jsdoc": "^3.4.0",
"nyc": "^6.4.0",
"rollup": "^0.33.0",
"nyc": "^8.1.0",
"rollup": "^0.34.9",
"rollup-plugin-babel": "^2.6.1",
"standard": "^7.1.2",
"xyz": "^0.5.0"
"xyz": "^1.0.0"
},

@@ -56,0 +55,0 @@ "ava": {

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