What is angular-api?
It is a module that helps in creating a service to deal with all your api calls within the angular application. It uses the module routes to attach handlers to your api routes.
The module exposes the Initiator class that can be used to add your base api url along with routes and route handlers. If a route handler is not specified, a standard route handler is used.
The module returns the Initiator class with the following methods defined on it.
Add a route with a handler on an Initiator instance. If the handler is not defined, the _standardRouteHandler is used. Look at the example below for handler function's signature.
Returns a constructor function to be used by module.service as a constructor for the service. The serve function is basically a closure.
How to use it?
angular-api is simple to use.
Create a module that returns a constructor function for services to be created with.
// filename: api.js// Require modulesvar ApiInititor = ;// Create an instance of the Initiator, passing in your api's base urlvar API = '';// Add some routesAPI; // Use built-in standard handlerAPI; // Use built-in standard handler// :reqType is important, that way, you can specify what type of request it is// and the built-in standard handler takes care of making of a request of that type.// Define a route with custom handlerAPI;// Create Api Service constructor using the 'serve' function// The 'serve' function returns a constructor function to be used by angular for instantiation// of a servicemoduleexports = API;
// filename: app.js// Create Angular Appvar angular = ;var api = ;var app = angular;app;// Don't forget to pass in $http, $q in that order// Use the Api Service to make a request to one of the defined routesapp;
The current practice is to return a promise that gets resolved when an http response is received. To keep things consistent, return a promise in all your custom handlers using this.q or just this.http which itself returns a promise.
The initiator code, i.e adding routes can go in a separate angular module and added as a dependency to your angular application.
What is left?
- Adding authentication for auth based routes.