]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/lib/api_utils.js
config value Nominatim_API_Endpoint can also be a callback
[nominatim-ui.git] / src / lib / api_utils.js
1 import { last_api_request_url_store, error_store } from './stores.js';
2
3 function api_request_progress(status) {
4   var loading_el = document.getElementById('loading');
5   if (!loading_el) return; // might not be on page yet
6
7   loading_el.style.display = (status === 'start') ? 'block' : null;
8 }
9
10 export async function fetch_from_api(endpoint_name, params, callback) {
11   var api_url = generate_nominatim_api_url(endpoint_name, params);
12
13   // For the test suite:
14   // If httpbin_status URL parameter is set we call https://httpbin.org/#/Status_codes
15   var tmp_params = new URLSearchParams(window.location.search);
16   if (tmp_params && tmp_params.get('httpbin_status')) {
17     api_url = 'https://httpbin.org/status/' + parseInt(tmp_params.get('httpbin_status'), 10);
18   }
19
20   api_request_progress('start');
21   if (endpoint_name !== 'status') last_api_request_url_store.set(null);
22
23   try {
24     await fetch(api_url, { headers: Nominatim_Config.Nominatim_API_Endpoint_Headers || {} })
25       .then(async (response) => {
26         if (!((response.status >= 200 && response.status < 300) || response.status === 404)) {
27           error_store.set(`Error fetching data from ${api_url} (${response.statusText})`);
28           return undefined;
29         }
30
31         // Parse JSON here instead of returning a promise so we can catch possible
32         // errors.
33         var data;
34         try {
35           data = await response.json();
36         } catch (err) {
37           // e.g. 'JSON.parse: unexpected non-whitespace character after JSON data at line 1'
38           error_store.set(`Error parsing JSON data from ${api_url} (${err})`);
39           return undefined;
40         }
41         return data;
42       })
43       .then((data) => {
44         if (data) {
45           if (data.error) {
46             error_store.set(data.error.message);
47           }
48           callback(data);
49         }
50         api_request_progress('finish');
51       });
52   } catch (error) {
53     error_store.set(`Error fetching data from ${api_url} (${error})`);
54     api_request_progress('finish');
55   }
56
57   if (endpoint_name !== 'status') last_api_request_url_store.set(api_url);
58 }
59
60 var fetch_content_cache = {};
61 export async function fetch_content_into_element(url, dom_element) {
62   if (!window.location.protocol.match(/^http/)) {
63     dom_element.innerHTML = `Cannot display data from ${url} here. `
64       + 'Browser security prevents loading content from file:// URLs.';
65     return;
66   }
67
68   if (fetch_content_cache[url]) {
69     dom_element.innerHTML = fetch_content_cache[url];
70     return;
71   }
72   try {
73     await fetch(url)
74       .then(response => response.text())
75       .then(html => {
76         html = html.replace('Nominatim_API_Endpoint', generate_nominatim_endpoint_url());
77         dom_element.innerHTML = html;
78         fetch_content_cache[url] = html;
79       });
80   } catch (error) {
81     dom_element.innerHTML = `Error fetching content from ${url} (${error})`;
82   }
83 }
84
85 function generate_nominatim_endpoint_url(endpoint_name) {
86   var conf_endpoint = Nominatim_Config.Nominatim_API_Endpoint;
87
88   if (typeof conf_endpoint === 'function') {
89     return conf_endpoint(endpoint_name);
90   }
91
92   if (!endpoint_name) return conf_endpoint;
93
94   return conf_endpoint + endpoint_name + '.php';
95 }
96
97 function generate_nominatim_api_url(endpoint_name, params) {
98   // default value for /search
99   if (params.dedupe === 1) delete params.dedupe;
100
101   extend_parameters(params, Nominatim_Config.Nominatim_API_Endpoint_Params);
102   return generate_nominatim_endpoint_url(endpoint_name)
103          + '?'
104          + Object.keys(clean_up_parameters(params)).map((k) => {
105            return encodeURIComponent(k) + '=' + encodeURIComponent(params[k]);
106          }).join('&');
107 }
108
109 function extend_parameters(params, params2) {
110   var param_names = Object.keys(params2);
111   for (var i = 0; i < param_names.length; i += 1) {
112     params[param_names[i]] = params2[param_names[i]];
113   }
114 }
115
116 function clean_up_parameters(params) {
117   // `&a=&b=&c=1` => '&c=1'
118   var param_names = Object.keys(params);
119   for (var i = 0; i < param_names.length; i += 1) {
120     var val = params[param_names[i]];
121     if (typeof (val) === 'undefined' || val === '' || val === null) {
122       delete params[param_names[i]];
123     }
124   }
125   return params;
126 }
127
128 export function update_html_title(title) {
129   document.title = [title, Nominatim_Config.Page_Title]
130     .filter((val) => val && val.length > 1)
131     .join(' | ');
132 }
133