angular-http-case-converter

A module providing simple way to convert request params and response body of backend requests

Usage no npm install needed!

<script type="module">
  import angularHttpCaseConverter from 'https://cdn.skypack.dev/angular-http-case-converter';
</script>

README

ee.$http.CaseConverter Build Status Built with Grunt

Module provides a way to convert requests and responses on the fly in a drop-in manner.

Table of Contents generated with DocToc

How it works?

Module attaches http interceptors which will transform request/response parts. It is configurable which requests/responses will be processed. By default all requests with parameters and all JSON responses are processed but you may use eeHttpCaseConverterProvider to change the semantics of conditions if you need to.

How to use?

Decide what type of conversion you want to use. Currently the package provides modules:

  • ee.$http.CaseConverter.request.camelToSnake that converts request params case from camel case used in the AngularJS application to snake case (a.k.a. underscore notation) used in the backend which is a default for many popular backend REST API solutions such as Symfony FOSRestBundle and Django Rest Framework.

  • ee.$http.CaseConverter.response.snakeToCamel that converts response JSON objects from snake case to camel case.

All you have to do is to depend your main module on the chosen package modules:

var myApp = angular.module('app', [
    'ee.$http.CaseConverter.request.camelToSnake',
    'ee.$http.CaseConverter.response.snakeToCamel',
])

The most basic filtering of requests is already provided. By default

  • requests with params defined have params processed,
  • POST and PUT requests (only those may have data) with data defined have data processed
  • each response returned as application/json is processed.

You may adjust those defaults by providing requestUrlFilter or responseUrlFilter function to eeHttpCaseConverterProvider. The function should return true for URLs which should be processed and false otherwise:

myApp.config(function (eeHttpCaseConverterProvider) {
    eeHttpCaseConverterProvider.responseUrlFilter = function (url) {
        // Your custom logic to decide whether process responses or not. Should return a boolean.
    }

    eeHttpCaseConverterProvider.requestUrlFilter = function (url) {
        // Your custom logic to decide whether process requests or not. Should return a boolean.
    }
})

If URL filtering is not enough You may also use eeHttpCaseConverterProvider to define custom conditions under which processing takes place. If you wish only certain requests/responses to be process use:

myApp.config(function (eeHttpCaseConverterProvider) {
    eeHttpCaseConverterProvider.requestConfig = {
        camelToSnake: {
            params: function (requestConfig) {
                // Your custom logic to decide whether to process `params` or not. Should return a boolean.
            },
            data: function (requestConfig) {
                // Your custom logic to decide whether to process `data` or not. Should return a boolean.
            }
        }
    }
    eeHttpCaseConverterProvider.responseConfig = {
        snakeToCamel: function (response) {
            // Your custom logic to decide whether to process `response.data` or not. Should return a boolean.
        }
    }
})

License

The module is available under the MIT license (see LICENSE for details).