]> git.openstreetmap.org Git - nominatim-ui.git/commitdiff
config value Nominatim_API_Endpoint can also be a callback
authorMarc Tobias <mtmail@gmx.net>
Wed, 4 May 2022 23:45:17 +0000 (01:45 +0200)
committermtmail <mtmail@gmx.net>
Thu, 5 May 2022 12:33:01 +0000 (14:33 +0200)
dist/config.defaults.js
src/lib/api_utils.js

index 1ab8bd938a7b25a4e5b2866cb8233bfd24f67757..5846e92b71e4969ede8a21a43b5274a82039ba17 100644 (file)
@@ -5,6 +5,12 @@ let Nominatim_Config = {
 
   // Where Nominatim API runs. Remember to add port if needed and trailing slash.
   Nominatim_API_Endpoint: 'http://localhost/nominatim/',
+  // Alternatively provide a function callback
+  // Nominatim_API_Endpoint: function (endpoint) {
+  //   var url = 'http://localhost/nominatim/';
+  //   if (endpoint) { url += endpoint + '.php' };
+  //   return url;
+  // }
 
   // Additional request headers for Nominatim API.
   Nominatim_API_Endpoint_Headers: {},
index e6f8158ef8e4cbff9f87fa2c96cf966001b418ba..7510c126c3125d6d43d930789797c94aa310f9d2 100644 (file)
@@ -73,7 +73,7 @@ export async function fetch_content_into_element(url, dom_element) {
     await fetch(url)
       .then(response => response.text())
       .then(html => {
-        html = html.replace('Nominatim_API_Endpoint', Nominatim_Config.Nominatim_API_Endpoint);
+        html = html.replace('Nominatim_API_Endpoint', generate_nominatim_endpoint_url());
         dom_element.innerHTML = html;
         fetch_content_cache[url] = html;
       });
@@ -82,12 +82,25 @@ export async function fetch_content_into_element(url, dom_element) {
   }
 }
 
+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';
+}
+
 function generate_nominatim_api_url(endpoint_name, params) {
   // default value for /search
   if (params.dedupe === 1) delete params.dedupe;
 
   extend_parameters(params, Nominatim_Config.Nominatim_API_Endpoint_Params);
-  return Nominatim_Config.Nominatim_API_Endpoint + endpoint_name + '.php?'
+  return generate_nominatim_endpoint_url(endpoint_name)
+         + '?'
          + Object.keys(clean_up_parameters(params)).map((k) => {
            return encodeURIComponent(k) + '=' + encodeURIComponent(params[k]);
          }).join('&');