I have multiple API calls so I created a service however I would like to find a way to remove the duplication
myAppServices.factory('apiService', function ($http, $q) {
return {
getDocuments: function () {
return $http.get('/api/autodeskdocuments/1')
.then(function (response) {
if (typeof response.data == 'object') {
return response.data;
} else {
// invalid response
return $q.reject(response.data);
}
}, function (response) {
// something went wrong
return $q.reject(response.data);
});
},
getSharePoint: function () {
return $http.get('/api/sharepointheaderdata/1')
.then(function (response) {
if (typeof response.data == 'object') {
return response.data;
} else {
// invalid response
return $q.reject(response.data);
}
}, function (response) {
// something went wrong
return $q.reject(response.data);
});
}
};
});
As you can see both functions has some duplication with the .then(...) Any suggestions as to make this cleaner?