opencage-api-client
Version:
An OpenCageData Geocoder API client library for node (ESM, CJS) and browsers (UMD)
2 lines • 1.96 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("opencage",[],t):"object"==typeof exports?exports.opencage=t():e.opencage=t()}(this,()=>(()=>{"use strict";var e={};e.d=(t,o)=>{for(var n in o)e.o(o,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:o[n]})},e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),e.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var t={};e.r(t),e.d(t,{geocode:()=>p});class o extends Error{response;status;constructor(e){super(e),this.name="GeocodeError"}}function n(e){if(e.status>=200&&e.status<300)return e;let t=e.statusText||`HTTP error ${e.status}`,n=new o(t);throw n.status={code:e.status,message:t},n.response=e,n}function r(e){return e.json()}async function s(e,t,o,s){fetch(e,{method:"GET",headers:{"User-Agent":"OpenCageData Geocoding NodeJS API Client/2.0.0","Content-Type":"application/json",Accept:"application/json"},signal:s}).then(n).then(r).then(e=>{t(e)}).catch(e=>{o(e)})}function c(e,t){let n=new o(t),r={code:e,message:t};return n.status=r,n.response={status:r},n}function i(e){return void 0===e||""===e}async function p(e,t){return new Promise((o,n)=>{if(null==e)return void n(c(400,"missing or bad query"));let r=function(e,t){let o={...e},n="https://api.opencagedata.com/geocode/v1/json",r=!1;return i(e.proxyURL)&&i(t?.proxyURL)?(i(e.key)&&"undefined"!=typeof process&&(o.key=process.env.OPENCAGE_API_KEY),i(o.key)&&(r=!0)):(i(n=t?.proxyURL)&&(n=e.proxyURL),delete o.proxyURL),{missingKey:r,endpoint:n,query:o}}(e,t);if(r.missingKey)return void n(c(401,"missing API key"));let{query:p,endpoint:a}=r,u=null==p?"":Object.keys(p).map(e=>`${encodeURIComponent(e)}=${encodeURIComponent(p[e]||"")}`).join("&");s(`${a}?${u}`,o,n,t?.signal)})}return t})());
//# sourceMappingURL=opencage-api.min.js.map