Skip to content

angular-hu/bower-composite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Publish repo for angular-hu-composite

This repo is for distribution on bower. The source for this module is in the main angular-hu-composite repo. Please file issues and pull requests against that repo.

httpu.composite

Exposes a factory that creates an interceptor that groups interceptors, to be traversed for specific backends. Implements the composite pattern.

Installation

Get it from bower or directly download it.

bower install --save angular-hu-composite

Add the dependency in the HTML

<script type="text/javascript" src="bower_components/angular-hu-composite/composite.js"></script>

Add the httpu.composite dependency to your App Module

angular.module('MyApp', ['httpu.composite']);

The huComposite dependency is now available

Usage

angular.module('MyApp')
//Create an interceptor of interceptors
.factory('myAPIInterceptors', function(huComposite) {
  return huComposite([
    'myOAuthInterceptor', //you can also register interceptors via an anonymous factory
    'myRetryInterceptor',
    'myStatisticsInterceptor'
  ], {
    //these interceptors will only be called when `myAPI` property is available in the request config
    flag: 'myAPI'
    //OR when the request URL satisfies the following RegExp
    backend: /^\/api/
  });
})
// Add the new interceptor to the interceptors chain
.config(function($httpProvider) {
  $httpProvider.interceptors.push('myAPIInterceptors');
})
//Now, these interceptors will only be called when the request config includes a truthy value for `myAPI` parameter
.run(function($http) {
  //This request will traverse your interceptors
  $http.get('/api', {
    myAPI: {  //Set it to `true` or to your API specific parameters managed in your interceptors
      param: 'value'
    }
  });

  //This request will traverse your interceptors,
  $http.get('/api');

  //Other requests (including server templates) without myAPI param or to other backend, will not traverse them
  $http.get('http://lwitter.com/lweets');
});

Use cases

  • Your app access several Backends, and have some backend specific interceptors (Authorization, error management...) for each one.
  • You have interceptors that implements some plug&play feature, and belongs to a certain module.
  • You download your templates from your server, and query a different API backend using several interceptors.

LICENSE

The MIT License (MIT)

Copyright (c) 2015 Telefónica I+D - http://www.tid.es