Last active
August 29, 2015 14:07
-
-
Save fesor/8c27d4890f384ecdcc74 to your computer and use it in GitHub Desktop.
ApiClientFactory PoC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('app', []) | |
.constant('server', {endpoint: 'http://test.com'}) | |
.config(function(server, apiClientFactoryProvider) { | |
apiClientFactoryProvider.register({ | |
'api': { | |
endpoint: server.endpoint, // will be concatinated with url | |
// global interceptors are bad, right? Heres a local one! | |
// Fully compatible with global interceptors | |
interceptors: ['myAuthInterceptor', 'noInternetConnectionInterceptor'] | |
}, | |
'twitter': { | |
interceptors: ['noInternetConnectionInterceptor'] | |
} | |
}); | |
apiClientProvider('facebook'); | |
}) | |
// registered api services can be injected via it's name + prefix | |
.factory('someService', function (twitterClient) { | |
var endpoint = 'http://api.twitter.com/v3'; | |
return { | |
foo: function () { | |
return otherServiceApi({ | |
method: 'GET' | |
url: endpoint + '/foo/' | |
}); | |
}, | |
bar: function (data) { | |
return otherServiceApi.post(endpoint + '/bar/', data, { | |
responseTransformer: angular.noop // config compatible with $http | |
}); | |
} | |
}; | |
}) | |
.factory('multiApiService', function (apiClientFactory) { | |
var facebook = apiClientFactory.get('facebook), | |
twitter = apiClientFactory.get('twitter'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment