- document.getElementById('loading').style.display = 'block';
- await fetch(api_url)
- .then(response => response.json())
- .then(data => {
- callback(data);
- document.getElementById('loading').style.display = 'none';
- });
-
-
- fetch(generate_nominatim_api_url('status', {format: 'json'}))
- .then(response => response.json())
- .then(data => {
- let last_updated = {
- api_request_url: api_url,
- api_request_url_debug: api_url + '&debug=1',
- date: data.data_updated
- };
- last_updated_store.set(last_updated)
- });
+ const mock_api_error = (new URLSearchParams(window.location.search)).get('mock_api_error');
+
+ api_request_progress('start');
+ if (endpoint_name !== 'status') last_api_request_url_store.set(null);
+
+ try {
+ await fetch(api_url, { headers: Nominatim_Config.Nominatim_API_Endpoint_Headers || {} })
+ .then(async (response) => {
+ if ((!((response.status >= 200 && response.status < 300) || response.status === 404))
+ || mock_api_error === 'fetch'
+ ) {
+ error_store.set(`Error fetching data from ${api_url} (${response.statusText})`);
+ return undefined;
+ }
+
+ // Parse JSON here instead of returning a promise so we can catch possible
+ // errors.
+ var data;
+ try {
+ if (mock_api_error === 'parse') {
+ data = JSON.parse('{');
+ } else {
+ data = await response.json();
+ }
+ } catch (err) {
+ // e.g. 'JSON.parse: unexpected non-whitespace character after JSON data at line 1'
+ error_store.set(`Error parsing JSON data from ${api_url} (${err})`);
+ return undefined;
+ }
+ return data;
+ })
+ .then((data) => {
+ if (data) {
+ if (data.error) {
+ error_store.set(data.error.message);
+ }
+ callback(data);
+ }
+ api_request_progress('finish');
+ });
+ } catch (error) {
+ error_store.set(`Error fetching data from ${api_url} (${error})`);
+ api_request_progress('finish');
+ }
+
+ if (endpoint_name !== 'status') last_api_request_url_store.set(api_url);
+}
+
+var fetch_content_cache = {};
+export async function fetch_content_into_element(url, dom_element) {
+ if (!window.location.protocol.match(/^http/)) {
+ dom_element.innerHTML = `Cannot display data from ${url} here. `
+ + 'Browser security prevents loading content from file:// URLs.';
+ return;
+ }
+
+ if (fetch_content_cache[url]) {
+ dom_element.innerHTML = fetch_content_cache[url];
+ return;
+ }
+ try {
+ await fetch(url)
+ .then(response => response.text())
+ .then(html => {
+ html = html.replace('Nominatim_API_Endpoint', generate_nominatim_endpoint_url());
+ dom_element.innerHTML = html;
+ fetch_content_cache[url] = html;
+ });
+ } catch (error) {
+ dom_element.innerHTML = `Error fetching content from ${url} (${error})`;
+ }
+}
+
+function generate_nominatim_endpoint_url(endpoint_name) {
+ var conf_endpoint = Nominatim_Config.Nominatim_API_Endpoint;
+
+ if (typeof conf_endpoint === 'function') {
+ return conf_endpoint(endpoint_name);
+ }
+
+ if (!endpoint_name) return conf_endpoint;
+
+ return conf_endpoint + endpoint_name + '.php';