@meanie/angular-http-buffer

2.0.2 • Public • Published

@meanie/angular-http-buffer

npm version node dependencies github issues codacy

An Angular service for buffering HTTP requests

Meanie

Installation

You can install this package using yarn or npm:

#yarn
yarn add @meanie/angular-http-buffer

#npm
npm install @meanie/angular-http-buffer --save

Include the script node_modules/@meanie/angular-http-buffer/release/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/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.

Sponsor

This package has been kindly sponsored by Hello Club, an all in one club and membership management solution complete with booking system, automated membership renewals, online payments and integrated access and light control. Check us out if you happen to belong to any kind of club or if you know someone who helps run a club!

License

(MIT License)

Copyright 2015-2020, Adam Reis

Package Sidebar

Install

npm i @meanie/angular-http-buffer

Weekly Downloads

4

Version

2.0.2

License

MIT

Unpacked Size

17.8 kB

Total Files

13

Last publish

Collaborators

  • adamreisnz