@opencage/geosearch-core
Version:
Core client for OpenCage GeoSearch
204 lines (203 loc) • 6.7 kB
JavaScript
const h = (n) => typeof n === "string", m = (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 (...r) => (t && clearTimeout(t), new Promise((s) => {
t = setTimeout(() => s(n(...r)), e);
}));
}, y = (n, e) => {
const t = /* @__PURE__ */ new Set();
return n.filter((r) => {
const s = e(r);
return t.has(s) ? !1 : t.add(s);
});
}, R = "opencage", b = ". . .", x = { results: [{ formatted: b }] }, i = () => {
}, c = ({ results: n }, { onActive: e = i, onSelect: t = i, noResults: r = "No results." } = {}) => {
const s = y(n, (o) => o.formatted);
return [
{
sourceId: R,
//
// ---- getItems
// type: (params: { query: string, state: AutocompleteState, ...setters }) => Item[] | Promise<Item[]>
getItems() {
return s;
},
//
// ---- getItemUrl
// type: (params: { item: Item, state: AutocompleteState }) => string | undefined
// getItemUrl() {
// }
//
// ---- getItemInputValue
// type: (params: { item, state: AutocompleteState }) => string` | defaults to `({ state }) => state.query
getItemInputValue({ item: o }) {
return o.formatted;
},
//
// ---- onSelect
// type: (params: { state: AutocompleteState, ...setters, event: Event, item: TItem, itemInputValue: string, itemUrl: string, source: AutocompleteSource }) => void` | defaults to `({ setIsOpen }) => setIsOpen(false)
onSelect: t,
//
// ---- onActive
// type: (params: { state: AutocompleteState, ...setters, event: Event, item: TItem, itemInputValue: string, itemUrl: string, source: AutocompleteSource }) => void
onActive: e,
// ----
templates: {
item({ item: o }) {
return `${o.formatted}`;
},
noResults() {
return r;
},
footer({ createElement: o }) {
const a = "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: a
},
"OpenCage"
),
"."
),
o(
"span",
null,
" © ",
o(
"a",
{
href: "https://www.openstreetmap.org/copyright",
target: "_blank",
rel: "noreferrer",
style: a
},
"OpenStreetMap"
),
","
),
o(
"span",
null,
" ",
o(
"a",
{
href: "https://opencagedata.com/credits",
target: "_blank",
rel: "noreferrer",
style: a
},
"others"
),
"."
)
)
);
}
}
// ...
}
];
}, $ = (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;
}, I = (n = { debounce: 300, noResults: "No results." }, e = {}) => {
const t = () => {
};
let r = null;
const s = (l) => {
r = l.item, e.onSelect && e.onSelect(l);
}, o = e.onActive || t, a = e.onSubmit || t, d = p(fetch, n.debounce);
return {
async getSources({ query: l }) {
if (l === "")
return r = null, [];
if (!h(l))
return console.debug("Not a string"), r = null, [];
if (l.length < 3)
return console.debug("Query is too short"), r = null, c(x, {
noResults: n.noResults,
onActive: o,
onSelect: s
});
if (r)
return c(
{ results: [r] },
{ noResults: n.noResults, onActive: o, onSelect: s }
);
const f = $(
`https://api.opencagedata.com/geosearch?q=${encodeURIComponent(l)}`,
n
), g = {
"OpenCage-Geosearch-Key": n.key
};
return d(f, { headers: g, mode: "cors" }).then(m).then((u) => {
if (u.ok) return u.json();
throw console.log(u.statusText), u.statusText;
}).then(
(u) => c(u, {
noResults: n.noResults,
onActive: o,
onSelect: s
})
).catch((u) => (console.error(`[Opencage GeoSearch error]: ${u.message}`), console.error(`[error] status: ${u.status}`), console.error(`[error] statusText: ${u.statusText}`), []));
},
// ---- onSubmit
// type: (params: { state: AutocompleteState, event: Event, ...setters }) => void
onSubmit: a,
// ---- onReset
// type: (params: { state: AutocompleteState, event: Event, ...setters: AutocompleteSetters }) => void
onReset: () => {
r = null;
}
};
}, S = { OpenCageGeoSearchPlugin: I };
export {
I as OpenCageGeoSearchPlugin,
S as default
};
//# sourceMappingURL=opencage-geosearch-core.esm.js.map