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

axios-middleware

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

axios-middleware - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

118

dist/axios-middleware.common.js
/**
* axios-middleware v0.1.3
* axios-middleware v0.2.0
* (c) 2018 Émile Bergeron

@@ -21,3 +21,3 @@ * @license MIT

* Default implementation is a noop.
* @param error
* @param {Object} error
*/

@@ -28,2 +28,11 @@ HttpMiddleware.prototype.onRequestError = function onRequestError (error) { };

* Default implementation is the identity function.
* @param {Promise} promise
* @returns {Promise}
*/
HttpMiddleware.prototype.onSync = function onSync (promise) {
return promise;
};
/**
* Default implementation is the identity function.
* @param {Object} response

@@ -38,6 +47,13 @@ * @returns {Object}

* Default implementation is a noop.
* @param error
* @param {Object} error
*/
HttpMiddleware.prototype.onResponseError = function onResponseError (error) { };
/**
* @property {Array} middlewares stack
* @property {AxiosInstance} http
* @property {Function} originalAdapter
* @property {Number} _requestInterceptor
* @property {Number} _responseInterceptor
*/
var HttpMiddlewareService = function HttpMiddlewareService(axios) {

@@ -50,3 +66,4 @@ this.middlewares = [];

/**
* @param {Axios} axios
* @param {AxiosInstance} axios
* @returns {HttpMiddlewareService}
*/

@@ -60,13 +77,4 @@ HttpMiddlewareService.prototype.setHttp = function setHttp (axios) {

this.http = axios;
var interceptors = axios.interceptors;
this._requestInterceptor = interceptors.request.use(
function (config) { return this$1._onRequest(config); },
function (error) { return this$1._onRequestError(error); }
);
this._responseInterceptor = interceptors.response.use(
function (response) { return this$1._onResponse(response); },
function (error) { return this$1._onResponseError(error); }
);
this.originalAdapter = axios.defaults.adapter;
axios.defaults.adapter = function (config) { return this$1.adapter(config); };
}

@@ -76,7 +84,9 @@ return this;

/**
* @returns {HttpMiddlewareService}
*/
HttpMiddlewareService.prototype.unsetHttp = function unsetHttp () {
if (this.http) {
var interceptors = this.http.interceptors;
interceptors.request.eject(this._requestInterceptor);
interceptors.response.eject(this._responseInterceptor);
this.http.defaults.adapter = this.originalAdapter;
this.http = null;
}

@@ -95,4 +105,5 @@ return this;

/**
* Add a middleware or an array of middlewares to the stack.
* Adds a middleware or an array of middlewares to the stack.
* @param {HttpMiddleware|Array} middlewares
* @returns {HttpMiddlewareService}
*/

@@ -112,7 +123,9 @@ HttpMiddlewareService.prototype.register = function register (middlewares) {

});
return this;
};
/**
* Remove a middleware from the registered stack.
* Removes a middleware from the registered stack.
* @param {HttpMiddleware} middleware
* @returns {HttpMiddlewareService}
*/

@@ -124,42 +137,63 @@ HttpMiddlewareService.prototype.unregister = function unregister (middleware) {

}
return this;
};
/**
* Removes all the middleware from the stack.
* @returns {HttpMiddlewareService}
*/
HttpMiddlewareService.prototype.reset = function reset () {
this.middlewares.length = 0;
return this;
};
HttpMiddlewareService.prototype._onRequest = function _onRequest (config) {
return this.middlewares.reduce(
function (acc, middleware) { return (middleware.onRequest ? middleware.onRequest(acc) : acc); },
config);
};
/**
* @param config
* @returns {Promise}
*/
HttpMiddlewareService.prototype.adapter = function adapter (config) {
var this$1 = this;
HttpMiddlewareService.prototype._onRequestError = function _onRequestError (error) {
this.middlewares.forEach(function (middleware) { return middleware.onRequestError &&
middleware.onRequestError(error); });
return Promise.reject(error);
};
var chain = [function (conf) { return this$1._onSync(this$1.originalAdapter.call(this$1.http, conf)); }, undefined];
var promise = Promise.resolve(config);
HttpMiddlewareService.prototype._onResponse = function _onResponse (response) {
return this.middlewares.reduceRight(
function (acc, middleware) { return (middleware.onResponse ? middleware.onResponse(acc) : acc); },
response);
};
this.middlewares.forEach(function (middleware) {
chain.unshift(
middleware.onRequest && (function (conf) { return middleware.onRequest(conf); }),
middleware.onRequestError && (function (error) { return middleware.onRequestError(error); })
);
});
HttpMiddlewareService.prototype._onResponseError = function _onResponseError (error) {
var this$1 = this;
this.middlewares.forEach(function (middleware) {
chain.push(
middleware.onResponse && (function (response) { return middleware.onResponse(response); }),
middleware.onResponseError && (function (error) { return middleware.onResponseError(error); })
);
});
for (var i = this.middlewares.length; i--;) {
var middleware = this$1.middlewares[i];
if (middleware.onResponseError) { middleware.onResponseError(error); }
while (chain.length) {
promise = promise.then(chain.shift(), chain.shift());
}
return Promise.reject(error);
return promise;
};
/**
* @param promise
* @returns {Promise}
* @private
*/
HttpMiddlewareService.prototype._onSync = function _onSync (promise) {
return this.middlewares.reduce(
function (acc, middleware) { return (middleware.onSync ? middleware.onSync(acc) : acc); },
promise
);
};
var index = {
HttpMiddleware: HttpMiddleware,
HttpMiddlewareService: HttpMiddlewareService,
version: '0.1.3',
version: '0.2.0',
};
module.exports = index;
/**
* axios-middleware v0.1.3
* axios-middleware v0.2.0
* (c) 2018 Émile Bergeron

@@ -19,3 +19,3 @@ * @license MIT

* Default implementation is a noop.
* @param error
* @param {Object} error
*/

@@ -26,2 +26,11 @@ HttpMiddleware.prototype.onRequestError = function onRequestError (error) { };

* Default implementation is the identity function.
* @param {Promise} promise
* @returns {Promise}
*/
HttpMiddleware.prototype.onSync = function onSync (promise) {
return promise;
};
/**
* Default implementation is the identity function.
* @param {Object} response

@@ -36,6 +45,13 @@ * @returns {Object}

* Default implementation is a noop.
* @param error
* @param {Object} error
*/
HttpMiddleware.prototype.onResponseError = function onResponseError (error) { };
/**
* @property {Array} middlewares stack
* @property {AxiosInstance} http
* @property {Function} originalAdapter
* @property {Number} _requestInterceptor
* @property {Number} _responseInterceptor
*/
var HttpMiddlewareService = function HttpMiddlewareService(axios) {

@@ -48,3 +64,4 @@ this.middlewares = [];

/**
* @param {Axios} axios
* @param {AxiosInstance} axios
* @returns {HttpMiddlewareService}
*/

@@ -58,13 +75,4 @@ HttpMiddlewareService.prototype.setHttp = function setHttp (axios) {

this.http = axios;
var interceptors = axios.interceptors;
this._requestInterceptor = interceptors.request.use(
function (config) { return this$1._onRequest(config); },
function (error) { return this$1._onRequestError(error); }
);
this._responseInterceptor = interceptors.response.use(
function (response) { return this$1._onResponse(response); },
function (error) { return this$1._onResponseError(error); }
);
this.originalAdapter = axios.defaults.adapter;
axios.defaults.adapter = function (config) { return this$1.adapter(config); };
}

@@ -74,7 +82,9 @@ return this;

/**
* @returns {HttpMiddlewareService}
*/
HttpMiddlewareService.prototype.unsetHttp = function unsetHttp () {
if (this.http) {
var interceptors = this.http.interceptors;
interceptors.request.eject(this._requestInterceptor);
interceptors.response.eject(this._responseInterceptor);
this.http.defaults.adapter = this.originalAdapter;
this.http = null;
}

@@ -93,4 +103,5 @@ return this;

/**
* Add a middleware or an array of middlewares to the stack.
* Adds a middleware or an array of middlewares to the stack.
* @param {HttpMiddleware|Array} middlewares
* @returns {HttpMiddlewareService}
*/

@@ -110,7 +121,9 @@ HttpMiddlewareService.prototype.register = function register (middlewares) {

});
return this;
};
/**
* Remove a middleware from the registered stack.
* Removes a middleware from the registered stack.
* @param {HttpMiddleware} middleware
* @returns {HttpMiddlewareService}
*/

@@ -122,43 +135,64 @@ HttpMiddlewareService.prototype.unregister = function unregister (middleware) {

}
return this;
};
/**
* Removes all the middleware from the stack.
* @returns {HttpMiddlewareService}
*/
HttpMiddlewareService.prototype.reset = function reset () {
this.middlewares.length = 0;
return this;
};
HttpMiddlewareService.prototype._onRequest = function _onRequest (config) {
return this.middlewares.reduce(
function (acc, middleware) { return (middleware.onRequest ? middleware.onRequest(acc) : acc); },
config);
};
/**
* @param config
* @returns {Promise}
*/
HttpMiddlewareService.prototype.adapter = function adapter (config) {
var this$1 = this;
HttpMiddlewareService.prototype._onRequestError = function _onRequestError (error) {
this.middlewares.forEach(function (middleware) { return middleware.onRequestError &&
middleware.onRequestError(error); });
return Promise.reject(error);
};
var chain = [function (conf) { return this$1._onSync(this$1.originalAdapter.call(this$1.http, conf)); }, undefined];
var promise = Promise.resolve(config);
HttpMiddlewareService.prototype._onResponse = function _onResponse (response) {
return this.middlewares.reduceRight(
function (acc, middleware) { return (middleware.onResponse ? middleware.onResponse(acc) : acc); },
response);
};
this.middlewares.forEach(function (middleware) {
chain.unshift(
middleware.onRequest && (function (conf) { return middleware.onRequest(conf); }),
middleware.onRequestError && (function (error) { return middleware.onRequestError(error); })
);
});
HttpMiddlewareService.prototype._onResponseError = function _onResponseError (error) {
var this$1 = this;
this.middlewares.forEach(function (middleware) {
chain.push(
middleware.onResponse && (function (response) { return middleware.onResponse(response); }),
middleware.onResponseError && (function (error) { return middleware.onResponseError(error); })
);
});
for (var i = this.middlewares.length; i--;) {
var middleware = this$1.middlewares[i];
if (middleware.onResponseError) { middleware.onResponseError(error); }
while (chain.length) {
promise = promise.then(chain.shift(), chain.shift());
}
return Promise.reject(error);
return promise;
};
/**
* @param promise
* @returns {Promise}
* @private
*/
HttpMiddlewareService.prototype._onSync = function _onSync (promise) {
return this.middlewares.reduce(
function (acc, middleware) { return (middleware.onSync ? middleware.onSync(acc) : acc); },
promise
);
};
var index_esm = {
HttpMiddleware: HttpMiddleware,
HttpMiddlewareService: HttpMiddlewareService,
version: '0.1.3',
version: '0.2.0',
};
export default index_esm;
export { HttpMiddleware, HttpMiddlewareService };
export default index_esm;
/**
* axios-middleware v0.1.3
* axios-middleware v0.2.0
* (c) 2018 Émile Bergeron

@@ -25,3 +25,3 @@ * @license MIT

* Default implementation is a noop.
* @param error
* @param {Object} error
*/

@@ -32,2 +32,11 @@ HttpMiddleware.prototype.onRequestError = function onRequestError (error) { };

* Default implementation is the identity function.
* @param {Promise} promise
* @returns {Promise}
*/
HttpMiddleware.prototype.onSync = function onSync (promise) {
return promise;
};
/**
* Default implementation is the identity function.
* @param {Object} response

@@ -42,6 +51,13 @@ * @returns {Object}

* Default implementation is a noop.
* @param error
* @param {Object} error
*/
HttpMiddleware.prototype.onResponseError = function onResponseError (error) { };
/**
* @property {Array} middlewares stack
* @property {AxiosInstance} http
* @property {Function} originalAdapter
* @property {Number} _requestInterceptor
* @property {Number} _responseInterceptor
*/
var HttpMiddlewareService = function HttpMiddlewareService(axios) {

@@ -54,3 +70,4 @@ this.middlewares = [];

/**
* @param {Axios} axios
* @param {AxiosInstance} axios
* @returns {HttpMiddlewareService}
*/

@@ -64,13 +81,4 @@ HttpMiddlewareService.prototype.setHttp = function setHttp (axios) {

this.http = axios;
var interceptors = axios.interceptors;
this._requestInterceptor = interceptors.request.use(
function (config) { return this$1._onRequest(config); },
function (error) { return this$1._onRequestError(error); }
);
this._responseInterceptor = interceptors.response.use(
function (response) { return this$1._onResponse(response); },
function (error) { return this$1._onResponseError(error); }
);
this.originalAdapter = axios.defaults.adapter;
axios.defaults.adapter = function (config) { return this$1.adapter(config); };
}

@@ -80,7 +88,9 @@ return this;

/**
* @returns {HttpMiddlewareService}
*/
HttpMiddlewareService.prototype.unsetHttp = function unsetHttp () {
if (this.http) {
var interceptors = this.http.interceptors;
interceptors.request.eject(this._requestInterceptor);
interceptors.response.eject(this._responseInterceptor);
this.http.defaults.adapter = this.originalAdapter;
this.http = null;
}

@@ -99,4 +109,5 @@ return this;

/**
* Add a middleware or an array of middlewares to the stack.
* Adds a middleware or an array of middlewares to the stack.
* @param {HttpMiddleware|Array} middlewares
* @returns {HttpMiddlewareService}
*/

@@ -116,7 +127,9 @@ HttpMiddlewareService.prototype.register = function register (middlewares) {

});
return this;
};
/**
* Remove a middleware from the registered stack.
* Removes a middleware from the registered stack.
* @param {HttpMiddleware} middleware
* @returns {HttpMiddlewareService}
*/

@@ -128,40 +141,61 @@ HttpMiddlewareService.prototype.unregister = function unregister (middleware) {

}
return this;
};
/**
* Removes all the middleware from the stack.
* @returns {HttpMiddlewareService}
*/
HttpMiddlewareService.prototype.reset = function reset () {
this.middlewares.length = 0;
return this;
};
HttpMiddlewareService.prototype._onRequest = function _onRequest (config) {
return this.middlewares.reduce(
function (acc, middleware) { return (middleware.onRequest ? middleware.onRequest(acc) : acc); },
config);
};
/**
* @param config
* @returns {Promise}
*/
HttpMiddlewareService.prototype.adapter = function adapter (config) {
var this$1 = this;
HttpMiddlewareService.prototype._onRequestError = function _onRequestError (error) {
this.middlewares.forEach(function (middleware) { return middleware.onRequestError &&
middleware.onRequestError(error); });
return Promise.reject(error);
};
var chain = [function (conf) { return this$1._onSync(this$1.originalAdapter.call(this$1.http, conf)); }, undefined];
var promise = Promise.resolve(config);
HttpMiddlewareService.prototype._onResponse = function _onResponse (response) {
return this.middlewares.reduceRight(
function (acc, middleware) { return (middleware.onResponse ? middleware.onResponse(acc) : acc); },
response);
};
this.middlewares.forEach(function (middleware) {
chain.unshift(
middleware.onRequest && (function (conf) { return middleware.onRequest(conf); }),
middleware.onRequestError && (function (error) { return middleware.onRequestError(error); })
);
});
HttpMiddlewareService.prototype._onResponseError = function _onResponseError (error) {
var this$1 = this;
this.middlewares.forEach(function (middleware) {
chain.push(
middleware.onResponse && (function (response) { return middleware.onResponse(response); }),
middleware.onResponseError && (function (error) { return middleware.onResponseError(error); })
);
});
for (var i = this.middlewares.length; i--;) {
var middleware = this$1.middlewares[i];
if (middleware.onResponseError) { middleware.onResponseError(error); }
while (chain.length) {
promise = promise.then(chain.shift(), chain.shift());
}
return Promise.reject(error);
return promise;
};
/**
* @param promise
* @returns {Promise}
* @private
*/
HttpMiddlewareService.prototype._onSync = function _onSync (promise) {
return this.middlewares.reduce(
function (acc, middleware) { return (middleware.onSync ? middleware.onSync(acc) : acc); },
promise
);
};
var index = {
HttpMiddleware: HttpMiddleware,
HttpMiddlewareService: HttpMiddlewareService,
version: '0.1.3',
version: '0.2.0',
};

@@ -168,0 +202,0 @@

/**
* axios-middleware v0.1.3
* axios-middleware v0.2.0
* (c) 2018 Émile Bergeron
* @license MIT
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.AxiosMiddleware=t()}(this,function(){"use strict";var e=function(){};e.prototype.onRequest=function(e){return e},e.prototype.onRequestError=function(e){},e.prototype.onResponse=function(e){return e},e.prototype.onResponseError=function(e){};var t=function(e){this.middlewares=[],this.setHttp(e)};return t.prototype.setHttp=function(e){var t=this;if(this.unsetHttp(),e){this.http=e;var r=e.interceptors;this._requestInterceptor=r.request.use(function(e){return t._onRequest(e)},function(e){return t._onRequestError(e)}),this._responseInterceptor=r.response.use(function(e){return t._onResponse(e)},function(e){return t._onResponseError(e)})}return this},t.prototype.unsetHttp=function(){if(this.http){var e=this.http.interceptors;e.request.eject(this._requestInterceptor),e.response.eject(this._responseInterceptor)}return this},t.prototype.has=function(e){return this.middlewares.indexOf(e)>-1},t.prototype.register=function(e){var t=this;Array.isArray(e)||(e=[e]),e.forEach(function(e){if(t.has(e))throw new Error("Middleware already registered");t.middlewares.push(e)})},t.prototype.unregister=function(e){var t=this.middlewares.indexOf(e);t>-1&&this.middlewares.splice(t,1)},t.prototype.reset=function(){this.middlewares.length=0},t.prototype._onRequest=function(e){return this.middlewares.reduce(function(e,t){return t.onRequest?t.onRequest(e):e},e)},t.prototype._onRequestError=function(e){return this.middlewares.forEach(function(t){return t.onRequestError&&t.onRequestError(e)}),Promise.reject(e)},t.prototype._onResponse=function(e){return this.middlewares.reduceRight(function(e,t){return t.onResponse?t.onResponse(e):e},e)},t.prototype._onResponseError=function(e){for(var t=this.middlewares.length;t--;){var r=this.middlewares[t];r.onResponseError&&r.onResponseError(e)}return Promise.reject(e)},{HttpMiddleware:e,HttpMiddlewareService:t,version:"0.1.3"}});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.AxiosMiddleware=e()}(this,function(){"use strict";var t=function(){};t.prototype.onRequest=function(t){return t},t.prototype.onRequestError=function(t){},t.prototype.onSync=function(t){return t},t.prototype.onResponse=function(t){return t},t.prototype.onResponseError=function(t){};var e=function(t){this.middlewares=[],this.setHttp(t)};return e.prototype.setHttp=function(t){var e=this;return this.unsetHttp(),t&&(this.http=t,this.originalAdapter=t.defaults.adapter,t.defaults.adapter=function(t){return e.adapter(t)}),this},e.prototype.unsetHttp=function(){return this.http&&(this.http.defaults.adapter=this.originalAdapter,this.http=null),this},e.prototype.has=function(t){return-1<this.middlewares.indexOf(t)},e.prototype.register=function(t){var e=this;return Array.isArray(t)||(t=[t]),t.forEach(function(t){if(e.has(t))throw new Error("Middleware already registered");e.middlewares.push(t)}),this},e.prototype.unregister=function(t){var e=this.middlewares.indexOf(t);return-1<e&&this.middlewares.splice(e,1),this},e.prototype.reset=function(){return this.middlewares.length=0,this},e.prototype.adapter=function(t){var e=this,r=[function(t){return e._onSync(e.originalAdapter.call(e.http,t))},void 0],n=Promise.resolve(t);for(this.middlewares.forEach(function(e){r.unshift(e.onRequest&&function(t){return e.onRequest(t)},e.onRequestError&&function(t){return e.onRequestError(t)})}),this.middlewares.forEach(function(e){r.push(e.onResponse&&function(t){return e.onResponse(t)},e.onResponseError&&function(t){return e.onResponseError(t)})});r.length;)n=n.then(r.shift(),r.shift());return n},e.prototype._onSync=function(t){return this.middlewares.reduce(function(t,e){return e.onSync?e.onSync(t):t},t)},{HttpMiddleware:t,HttpMiddlewareService:e,version:"0.2.0"}});
{
"name": "axios-middleware",
"version": "0.1.3",
"version": "0.2.0",
"description": "Simple axios HTTP middleware service",

@@ -49,20 +49,19 @@ "author": "Emile Bergeron <ber.emile@prismalstudio.com>",

"axios": "^0.17.1",
"axios-mock-adapter": "^1.11.0",
"babel-preset-env": "^1.6.1",
"cross-env": "^5.1.3",
"docsify-cli": "^4.2.0",
"eslint": "^4.16.0",
"eslint-config-airbnb-base": "^11.3.2",
"axios-mock-adapter": "^1.15.0",
"babel-preset-env": "^1.7.0",
"cross-env": "^5.1.6",
"docsify-cli": "^4.2.1",
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-friendly-formatter": "^3.0.0",
"eslint-loader": "^1.9.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-import": "^2.12.0",
"husky": "^0.14.3",
"jest": "^22.0.3",
"rimraf": "^2.6.0",
"rollup": "^0.50.0",
"rollup-plugin-buble": "^0.16.0",
"jest": "^23.0.0",
"rimraf": "^2.6.2",
"rollup": "^0.55.3",
"rollup-plugin-buble": "^0.19.2",
"rollup-plugin-replace": "^2.0.0",
"rollup-watch": "^4.3.1",
"uglify-js": "^3.1.2"
"uglify-js": "^3.3.27"
}
}
# axios-middleware
[![Build Status](https://travis-ci.org/emileber/axios-middleware.svg?branch=master)](https://travis-ci.org/emileber/axios-middleware)
[![devDependency Status](https://david-dm.org/emileber/axios-middleware/dev-status.svg)](https://david-dm.org/emileber/axios-middleware#info=devDependencies)
[![Dependency Status](https://beta.gemnasium.com/badges/github.com/emileber/axios-middleware.svg)](https://beta.gemnasium.com/projects/github.com/emileber/axios-middleware)
[![npm version](https://badge.fury.io/js/axios-middleware.svg)](https://www.npmjs.com/package/axios-middleware)

@@ -18,34 +19,7 @@ Simple [axios](https://github.com/axios/axios) HTTP middleware service.

### Create your middleware
A simple example using the [simplified middleware syntax](https://emileber.github.io/axios-middleware/#/simplified-syntax).
Here's a simple example of a locale middleware who sets a language header on each AJAX request.
```javascript
import { HttpMiddleware } from 'axios-middleware';
export default class LocaleMiddleware extends HttpMiddleware {
constructor(i18n) {
super();
this.i18n = i18n;
}
onRequest(config) {
config.headers = {
locale: this.i18n.lang,
...config.headers
};
return config;
}
}
```
### Use the middleware service
Simplest use-case:
```javascript
import axios from 'axios';
import { HttpMiddlewareService } from 'axios-middleware';
import i18n from './i18n';
import { LocaleMiddleware, OtherMiddleware } from './middlewares';

@@ -56,8 +30,19 @@ // Create a new service instance

// Then register your middleware instances.
service.register([
new LocaleMiddleware(i18n),
new OtherMiddleware()
]);
service.register({
onRequest(config) {
// handle the request config
return config;
},
onSync(promise) {
// handle the promsie
return promise;
},
onResponse(response) {
// handle the response
return response;
}
});
// We're good to go!
export default { service };
```

@@ -64,0 +49,0 @@

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