Skip to content
This repository was archived by the owner on Feb 28, 2022. It is now read-only.

Commit 5e75d38

Browse files
committed
fix: bugs found durint tests writings on api and nominatim services
1 parent 25ad4c3 commit 5e75d38

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

src/api/api.service.js

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
103103

104104
// ------------------ INTERNAL CALL SERVER (API) -----------------
105105

106+
function isElement(obj) {
107+
try {
108+
//Using W3 DOM2 (works for FF, Opera and Chrom)
109+
return obj instanceof HTMLElement;
110+
}
111+
catch(e){
112+
//Browsers not supporting W3 DOM2 don't have HTMLElement and
113+
//an exception is thrown and we end up here. Testing some
114+
//properties that all elements have. (works on IE7)
115+
return (typeof obj==="object") &&
116+
(obj.nodeType===1) && (typeof obj.style === "object") &&
117+
(typeof obj.ownerDocument ==="object");
118+
}
119+
}
106120

107121
/**
108122
* @ngdoc method
@@ -112,8 +126,8 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
112126
* @comment
113127
* ```
114128
var options = {
115-
method: 'GET' // POST, DELETE, PUT
116-
path: '/0.6/changesets' //without the /api,
129+
method: 'GET', // POST, DELETE, PUT
130+
path: '/0.6/changesets', //without the /api
117131
data: content //if you need a payload
118132
};
119133
osmAPI.xhr(options);
@@ -133,19 +147,33 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
133147
}
134148
promise = this._oauth.xhr(options);
135149
} else {
136-
let fct = options.method.toLowerCase();
137-
if (options.config === undefined) {
138-
options.config = {};
139-
}
140-
options.config.headers = {Authorization: this.getAuthorization()};
141-
promise = $http[fct](options.path, options.config);
150+
options.url = this.url + options.path;
151+
options.headers = {
152+
Authorization: this.getAuthorization()
153+
};
154+
promise = $http(options);
142155
}
143156
promise.then(function (data) {
157+
var d;
158+
var t = function (d) {
159+
if (!d) {
160+
return d;
161+
}
162+
if (d.startsWith) {
163+
if (d.startsWith('<?xml')) {
164+
return osmUtilsService.xml2js(d);
165+
}
166+
} else if (isElement(d)) {
167+
return osmUtilsService.x2js.dom2js(d);
168+
}
169+
return d;
170+
};
144171
if (hasOauth) {
145-
deferred.resolve(osmUtilsService.x2js.dom2js(data));
172+
d = data;
146173
} else {
147-
deferred.resolve(osmUtilsService.xml2js(data.data));
174+
d = data.data;
148175
}
176+
deferred.resolve(t(d));
149177
}, function (error) {
150178
deferred.reject(error);
151179
});
@@ -200,10 +228,13 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
200228
* @returns {Promise} $http response
201229
*/
202230
this.put = function (method, content, config) {
231+
if (!config) {
232+
config = {};
233+
}
203234
var _config = angular.copy(config);
204-
config.method = 'PUT';
205-
config.path = method;
206-
config.data = content;
235+
_config.method = 'PUT';
236+
_config.path = method;
237+
_config.data = osmUtilsService.js2xml(content);
207238
return this.xhr(_config);
208239
};
209240
/**
@@ -216,9 +247,12 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
216247
* @returns {Promise} $http response
217248
*/
218249
this.delete = function (method, config) {
250+
if (!config) {
251+
config = {};
252+
}
219253
var _config = angular.copy(config);
220-
config.method = 'DELETE';
221-
config.path = method;
254+
_config.method = 'DELETE';
255+
_config.path = method;
222256
return this.xhr(_config);
223257
};
224258

@@ -266,7 +300,10 @@ function osmAPI($base64, $http, $q, osmSettingsService, osmUtilsService, options
266300
if (changesets.length > 0) {
267301
osmSettingsService.setChangeset(changesets[0].id);
268302
deferred.resolve(changesets[0].id);
269-
}else{
303+
} else if (changesets._id) {
304+
osmSettingsService.setChangeset(changesets._id);
305+
deferred.resolve(changesets._id);
306+
} else {
270307
osmSettingsService.setChangeset();
271308
deferred.resolve();
272309
}

src/nominatim/nominatim.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function osmNominatim($http, options) {
2020
//?X-Requested-With=overpass-turbo&format=json&q=vern-sur-seiche
2121
//params['accept-language'] = 'fr';
2222
var params;
23-
if (typeof query === 'string') {
23+
if (typeof query === 'string' || !query) {
2424
params = {
2525
format: 'json',
2626
q: query

0 commit comments

Comments
 (0)