@opencage/geosearch-core
Version:
Core client for OpenCage GeoSearch
3 lines (2 loc) • 3.44 kB
JavaScript
(function(l,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(l=typeof globalThis<"u"?globalThis:l||self,a(l.opencage={}))})(this,(function(l){"use strict";const a=n=>typeof n==="string",h=n=>{if(n.ok)return n;const e=new Error(`The HTTP status of the reponse: ${n.status} (${n.statusText})`);throw e.status=n.status,e.statusText=n.statusText,e},p=(n,e)=>{let t;return(...s)=>(t&&clearTimeout(t),new Promise(r=>{t=setTimeout(()=>r(n(...s)),e)}))},m=(n,e)=>{const t=new Set;return n.filter(s=>{const r=e(s);return t.has(r)?!1:t.add(r)})},y="opencage",b={results:[{formatted:". . ."}]},f=()=>{},d=({results:n},{onActive:e=f,onSelect:t=f,noResults:s="No results."}={})=>{const r=m(n,o=>o.formatted);return[{sourceId:y,getItems(){return r},getItemInputValue({item:o}){return o.formatted},onSelect:t,onActive:e,templates:{item({item:o}){return`${o.formatted}`},noResults(){return s},footer({createElement:o}){const i="text-decoration:none;color:#009966;";return o("div",{style:"display:flex;flex-direction:column;align-items:flex-end;"},o("div",{style:"display:flex;align-items:center;justify-content:center;font-family:sans-serif;font-size:0.7em;color:#009966;"},o("span",null,o("a",{href:"https://opencagedata.com/geosearch",target:"_blank",rel:"noreferrer"},o("img",{src:"https://assets.opencagedata.com/opencage-20x21.png",height:"21",width:"20",border:"0",alt:"OpenCage",style:"display:inline;"}))),o("span",null," Made by ",o("a",{href:"https://opencagedata.com/geosearch",target:"_blank",rel:"noreferrer",style:i},"OpenCage"),"."),o("span",null," © ",o("a",{href:"https://www.openstreetmap.org/copyright",target:"_blank",rel:"noreferrer",style:i},"OpenStreetMap"),","),o("span",null," ",o("a",{href:"https://opencagedata.com/credits",target:"_blank",rel:"noreferrer",style:i},"others"),".")))}}}]},R=(n,e)=>{let t=n;return e&&(e.limit&&(t=`${t}&limit=${encodeURIComponent(e.limit)}`),e.countrycode&&(t=`${t}&countrycode=${encodeURIComponent(e.countrycode)}`),e.language&&(t=`${t}&language=${encodeURIComponent(e.language)}`),e.bounds&&(t=`${t}&bounds=${encodeURIComponent(e.bounds)}`),e.proximity&&(t=`${t}&proximity=${encodeURIComponent(e.proximity)}`)),t},g=(n={debounce:300,noResults:"No results."},e={})=>{const t=()=>{};let s=null;const r=c=>{s=c.item,e.onSelect&&e.onSelect(c)},o=e.onActive||t,i=e.onSubmit||t,x=p(fetch,n.debounce);return{async getSources({query:c}){if(c==="")return s=null,[];if(!a(c))return console.debug("Not a string"),s=null,[];if(c.length<3)return console.debug("Query is too short"),s=null,d(b,{noResults:n.noResults,onActive:o,onSelect:r});if(s)return d({results:[s]},{noResults:n.noResults,onActive:o,onSelect:r});const S=R(`https://api.opencagedata.com/geosearch?q=${encodeURIComponent(c)}`,n),I={"OpenCage-Geosearch-Key":n.key};return x(S,{headers:I,mode:"cors"}).then(h).then(u=>{if(u.ok)return u.json();throw console.log(u.statusText),u.statusText}).then(u=>d(u,{noResults:n.noResults,onActive:o,onSelect:r})).catch(u=>(console.error(`[Opencage GeoSearch error]: ${u.message}`),console.error(`[error] status: ${u.status}`),console.error(`[error] statusText: ${u.statusText}`),[]))},onSubmit:i,onReset:()=>{s=null}}},T={OpenCageGeoSearchPlugin:g};l.OpenCageGeoSearchPlugin=g,l.default=T,Object.defineProperties(l,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
//# sourceMappingURL=opencage-geosearch-core.js.map