I'm writing an AngularJS module to provide some convenience for interacting with my product's REST API. Since my product is deployed on premise, each user will have to supply their own URL to interact with the API. So, the base URL needs to be configurable.
I've been looking at $http
's way of setting default headers to figure out how to make a nice API for configuring such properties, but haven't had much luck. What I'm trying to achieve is something like this:
in my api.js
file:
angular.module('myProduct', [])
properties.baseUrl = 'someDefault';
properties.authenticationToken;
authenticate = function(user, pass) {
properties.authenticationToken = $http.get(properties.baseUrl + '/login');
}
in the customer's app.js
file:
angular.controller('myController', ['myProduct'], function($myProduct) {
// during initialization
$myProduct.properties.baseUrl = 'customer.com/myProduct';
// either during initialization, or after submitting a login form
$myProduct.authenticate('username', 'password')
.then(/* do some other API call */);
})
My questions are:
- How to expose the properties in my module in a nice, configurable way?
- How to expose the authenticate function in my module so it can be called from the customer's app.js in a nice way?
Any links to the Angular documentation I might have missed are also appreciated.