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