Skip to content

angular-hu/bower-urlbuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Publish repo for angular-hu-urlbuilder

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

httpu.urlbuilder

Allows specifiying how an URL is built. In other words, allows you to customize how the params option in a $http request is converted to a queryString other than the default angular way. Gives you the foundation to implement jquery url builder and other backend specific serializations

Installation

Get it from bower or directly download it.

bower install --save angular-hu-urlbuilder

Add the dependency in the HTML

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

Add the httpu.urlbuilder dependency to your App Module

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

The huURLBuilderInterceptor and huURLBuilderFactory dependencies are now available

Usage

angular.module('MyApp')
//Add the interceptor that will make the serialization.
.config(function($httpProvider) {
  $httpProvider.interceptors.push('huURLBuilderInterceptor');
})
.factory('pirateBuildUrl', function(huURLBuilderFactory) {
  //this is how a `params` object is serialized
 //you must call the `addKeyValue` with *all* your final key, value pairs.
 //this serializer converts params to Pirate Language
 function pirateSerializer(params, addKeyValue) {
    //params is the $http request `params` option property
    angular.forEach(params, function(value, key) {
      addKeyValue('ARR' + key, value);
    });
  }

  return huURLBuilderFactory(pirateSerializer);
})
.run(function($http) {

  //Make a request
  $http.get('http://myapi.com/things', {
    params: {
      id: 5
    },
    //specify the url builder
    buildUrl: 'pirateBuildUrl'
  });
  // GET http://myapi.com/things?ARRid=5
});

Use cases

  • You have a backend that doesn't understand the current param serialization from angular, and you have to code the serialized url directly in the url used for the request, losing the power of having it as a plain object.

LICENSE

The MIT License (MIT)

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