1.2.2 • Published 9 years ago
meanie-angular-http-buffer v1.2.2
meanie-angular-http-buffer
An Angular service for buffering HTTP requests
Installation
You can install this package using npm
:
npm install meanie-angular-http-buffer --save
Include the script node_modules/meanie-angular-http-buffer/release/meanie-angular-http-buffer.js
in your build process, or add it via a <script>
tag to your index.html
:
<script src="node_modules/meanie-angular-http-buffer/release/meanie-angular-http-buffer.js"></script>
Add HttpBuffer.Service
as a dependency for your app.
Usage
Buffer failed requests in a http interceptor:
//Module dependencies
angular.module('App.MyModule', [
'HttpBuffer.Service'
])
//Config
.config(function($httpProvider) {
$httpProvider.interceptors.push('Buffered401Interceptor');
})
//Interceptor factory
.factory('Buffered401Interceptor', function($q, $rootScope, $httpBuffer) {
return {
responseError: function(response) {
//Intercept 401's
if (response.status === 401 && !response.config.ignore401intercept) {
var promise = $httpBuffer.store(response.config);
$rootScope.$broadcast('auth.401', response);
return promise;
}
//Return rejection
return $q.reject(response);
}
};
});
Later retry or reject them:
$rootScope.on('auth.401', function(event, response) {
//Use an Auth service to login again
Auth.doLogin().then(function() {
//Retry all buffered requests
$httpBuffer.retryAll();
}, function() {
//Reject all buffered requests
$httpBuffer.rejectAll();
});
});
Issues & feature requests
Please report any bugs, issues, suggestions and feature requests in the meanie-angular-http-buffer issue tracker.
Contributing
Pull requests are welcome! If you would like to contribute to Meanie, please check out the Meanie contributing guidelines.
Credits
- Based on the Http Buffer service from Witold Szczerba in his angular-http-auth module.
- Meanie logo designed by Quan-Lin Sim
License
(MIT License)
Copyright 2015-2017, Adam Reis