I am in the process of developing an AngularJS module that will simplify interactions with my product's REST API. Since my product is installed on-premise, each user will need to provide their own URL to access the API. Therefore, it is essential that the base URL be customizable.
I have been exploring how to set default headers using $http
in order to create a user-friendly API for configuring these properties. However, I have not made much progress so far. My goal is to achieve something similar to 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) {
// Setting the base URL during initialization
$myProduct.properties.baseUrl = 'customer.com/myProduct';
// Calling the authenticate function either during initialization or after submitting a login form
$myProduct.authenticate('username', 'password')
.then(/* perform additional API calls */);
})
My inquiries include:
- How can I expose the properties in my module in a user-friendly and easily configurable manner?
- How can I make the authenticate function accessible within my module so it can be invoked seamlessly from the customer's app.js?
If there are any relevant resources in the Angular documentation that I may have overlooked, please feel free to share them as well.