Closed tsak closed 3 years ago
@tsak Well, thats pretty common need and it is simple to build that into modelizer but its not there intentionally, because you can use Angular $http
interceptors directly, e.g.:
angular.module('app')
.factory('apiUrlHttpInterceptor', function () {
// Can be an injected constant, value, or taken from some service
var apiUrl = '/api/v1';
var shouldPrependApiUrl = function (reqConfig) {
if (!apiUrl) return false;
return !(/[\s\S]*.html/.test(reqConfig.url) ||
(reqConfig.url && reqConfig.url.indexOf(apiUrl) === 0));
};
return {
request: function (reqConfig) {
// Filter out requests for .html templates, etc
if (apiUrl && shouldPrependApiUrl(reqConfig)) {
reqConfig.url = apiUrl + reqConfig.url;
}
return reqConfig;
}
};
})
.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('apiUrlHttpInterceptor');
}]);
Makes sense?
There is another feature being discussed though, which is will allow to define multiple "contexts" for modelizer, for example, to work with different APIs - it would make sense for each context to have its own global prefix then.
I would think that it would make sense to implement this as part of the library itself instead of using an interceptor that might affect other services consuming $http
. As you've mentioned contexts it sounds to me that this functionality might be part of that.
I think it would be helpful to have a
modelizeProvider
.setGlobalUrlPrefix
(or similarly named function) that would allow one to prefix all request URLs.This would allow a more elegant solution for APIs that carry the version number in the URL, e.g.
/api/v1
or APIs from a different host (CORS, I know), the latter currently not possible as far as I have tested.Let me know if you think this makes sense. Happy to create a pull request.