UNPKG

opencage-api-client

Version:

An OpenCageData Geocoder API client library for node (ESM, CJS) and browsers (UMD)

1 lines 4.21 kB
{"version":3,"file":"fetch.cjs","sources":["webpack://opencage-api-client/webpack/runtime/define_property_getters","webpack://opencage-api-client/webpack/runtime/has_own_property","webpack://opencage-api-client/webpack/runtime/make_namespace_object","webpack://opencage-api-client/./src/fetch.ts"],"sourcesContent":["__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n }\n }\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","import { GeocodeError } from './errors/GeocodeError';\nimport { version } from './version';\n\nconst USER_AGENT = `OpenCageData Geocoding NodeJS API Client/${version}`;\n\n/**\n * @private\n * @description checks the response status and throws an error if the status is not ok\n * @param response {Response} the response object\n * @returns {Response} the response object\n * @throws {GeocodeError} the error object\n */\nexport function checkFetchStatus(response: Response) {\n if (response.status >= 200 && response.status < 300) {\n return response;\n }\n // console.debug('request failed with status', response.status);\n // console.debug('request failed with status text', response.statusText);\n const message = response.statusText || `HTTP error ${response.status}`;\n const error = new GeocodeError(message);\n error.status = {\n code: response.status,\n message,\n };\n error.response = response;\n throw error;\n}\n\nexport function parseJSON(response: Response) {\n return response.json();\n}\n\n/**\n * fetches the url and returns a promise\n * @param {String} url the url to fetch\n * @param {Function} resolve the resolve function\n * @param {Function} reject the reject function\n */\nexport async function fetchUrl(\n url: string,\n resolve: any,\n reject: any,\n signal?: AbortSignal\n) {\n fetch(url, {\n method: 'GET',\n headers: {\n 'User-Agent': USER_AGENT,\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n },\n signal,\n })\n .then(checkFetchStatus)\n .then(parseJSON)\n .then((data) => {\n // console.debug('request succeeded with JSON response', data);\n resolve(data);\n })\n .catch((error) => {\n // console.debug('request failed', { error });\n reject(error);\n });\n}\n"],"names":["__webpack_require__","definition","key","Object","obj","prop","Symbol","USER_AGENT","version","checkFetchStatus","response","message","error","GeocodeError","parseJSON","fetchUrl","url","resolve","reject","signal","fetch","data"],"mappings":";;;IAAAA,oBAAoB,CAAC,GAAG,CAAC,UAASC;QACjC,IAAI,IAAIC,OAAOD,WACR,IAAGD,oBAAoB,CAAC,CAACC,YAAYC,QAAQ,CAACF,oBAAoB,CAAC,CAAC,UAASE,MACzEC,OAAO,cAAc,CAAC,UAASD,KAAK;YAAE,YAAY;YAAM,KAAKD,UAAU,CAACC,IAAI;QAAC;IAGzF;;;ICNAF,oBAAoB,CAAC,GAAG,CAACI,KAAKC,OAAUF,OAAO,SAAS,CAAC,cAAc,CAAC,IAAI,CAACC,KAAKC;;;ICClFL,oBAAoB,CAAC,GAAG,CAAC;QACxB,IAAG,AAAkB,eAAlB,OAAOM,UAA0BA,OAAO,WAAW,EACrDH,OAAO,cAAc,CAAC,UAASG,OAAO,WAAW,EAAE;YAAE,OAAO;QAAS;QAEtEH,OAAO,cAAc,CAAC,UAAS,cAAc;YAAE,OAAO;QAAK;IAC5D;;;;;;;;;;;ACHA,MAAMI,aAAa,CAAC,yCAAyC,EAAEC,qCAAAA,OAAOA,EAAE;AASjE,SAASC,iBAAiBC,QAAkB;IACjD,IAAIA,SAAS,MAAM,IAAI,OAAOA,SAAS,MAAM,GAAG,KAC9C,OAAOA;IAIT,MAAMC,UAAUD,SAAS,UAAU,IAAI,CAAC,WAAW,EAAEA,SAAS,MAAM,EAAE;IACtE,MAAME,QAAQ,IAAIC,iCAAAA,YAAYA,CAACF;IAC/BC,MAAM,MAAM,GAAG;QACb,MAAMF,SAAS,MAAM;QACrBC;IACF;IACAC,MAAM,QAAQ,GAAGF;IACjB,MAA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