diff --git a/src/api/api.js b/src/api/api.js index 8ad45cf..0117271 100644 --- a/src/api/api.js +++ b/src/api/api.js @@ -11,10 +11,10 @@ var osmAPIModule = angular.module('osm.api', [ this.options = { url: 'http://api.openstreetmap.org/api' }; - this.$get = function osmAPIFactory($http, $q, osmx2js) { - return new osmAPI($http, $q, osmx2js, this.options); + this.$get = ($http, osmx2js) => { + return new osmAPI($http, osmx2js, this.options); }; - this.$get.$inject = ['$http', '$q', 'osmx2js']; + this.$get.$inject = ['$http', 'osmx2js']; }); export default osmAPIModule; diff --git a/src/api/api.service.js b/src/api/api.service.js index 0becfa9..d4ea9ab 100644 --- a/src/api/api.service.js +++ b/src/api/api.service.js @@ -7,14 +7,12 @@ class OSMAPI { /** * @param {Object} $http angular http - * @param {Object} $q angular promise * @param {Object} osmx2js service * @param {Object} options to get set url of the API */ - constructor($http, $q, osmx2js, options) { + constructor($http, osmx2js, options) { this.url = options.url; this.$http = $http; - this.$q = $q; this.osmx2js = osmx2js; this._oauth = null; } @@ -53,7 +51,6 @@ class OSMAPI { * @return {Promise} the adapter response */ xhr(options) { - let deferred = this.$q.defer(); return this._oauth.xhr(options); } @@ -77,15 +74,8 @@ class OSMAPI { * @returns {Promise} $http response with XML as string */ get(method, config) { - var deferred = this.$q.defer(); - var self = this; - var url = this.url + method; - this.$http.get(url, config).then(function (data) { - deferred.resolve(self.osmx2js.xml2js(data.data)); - }, function (error) { - deferred.reject(error); - }); - return deferred.promise; + return this.$http.get(this.url + method, config) + .then((data) => this.osmx2js.xml2js(data.data)); } /** * send a put request @@ -131,8 +121,6 @@ class OSMAPI { * @returns {Promise} $http response */ createChangeset(comment, author) { - var self = this; - var deferred = this.$q.defer(); var changeset = {osm: { changeset: { tag: [ @@ -141,43 +129,33 @@ class OSMAPI { ] } }}; - this.put('/0.6/changeset/create', changeset).then(function (data) { - deferred.resolve(data); - }); - return deferred.promise; + return this.put('/0.6/changeset/create', changeset); } /** * @returns {Promise} $http response with the last changeset id * or undefined if no changeset was opened */ getLastOpenedChangesetId() { - var self = this; - var deferred = this.$q.defer(); var config = { params:{user: this._oauth.getUserID(), open: true} }; - this.get('/0.6/changesets', config).then(function (data) { + return this.get('/0.6/changesets', config).then((data) => { var changesets = data.osm.changeset; if (changesets.length > 0) { - deferred.resolve(changesets[0].id); + return changesets[0].id; } else if (changesets._id) { - deferred.resolve(changesets._id); + return changesets._id; } else { - deferred.resolve(); + return; } }); - return deferred.promise; } /** * @returns {Promise} $http.put response of * /0.6/changeset/CHANGESET_ID/close */ closeChangeset(id) { - var self = this; - return this.put(`/0.6/changeset/${id}/close`) - .then(function (data) { - return data; - }); + return this.put(`/0.6/changeset/${id}/close`); } @@ -454,5 +432,5 @@ class OSMAPI { } -OSMAPI.$inject = ['$http', '$q', 'osmx2js']; +OSMAPI.$inject = ['$http', 'osmx2js']; export default OSMAPI; diff --git a/src/api/api.spec.js b/src/api/api.spec.js index 9d46eaa..346081a 100644 --- a/src/api/api.spec.js +++ b/src/api/api.spec.js @@ -6,7 +6,7 @@ ngDescribe({ modules: ['osm.api'], - inject: ['osmAPI', 'osmx2js', '$q', '$http', '$httpBackend', '$rootScope'], + inject: ['osmAPI', 'osmx2js', '$http', '$httpBackend', '$rootScope'], tests: function (deps) { function setHTTPAdapter(deps) { var adapter = { @@ -48,7 +48,7 @@ ngDescribe({ it('should internal xhr works as expected', function() { var backend = { xhr: function(options) { - return deps.$q.when({data: 'data'}); + return Promise.resolve({data: 'data'}); } }; deps.osmAPI.setAuthAdapter(backend);