opencage-api-client
Version:
An OpenCageData Geocoder API client library for node (ESM, CJS) and browsers (UMD)
1 lines • 5.68 kB
Source Map (JSON)
{"version":3,"file":"helpers/geocodeHelpers.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/helpers/geocodeHelpers.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 { GeocodingRequest } from '../types/GeocodingRequest';\nimport { GeocodeError, GeocodeErrorStatus } from '../errors/GeocodeError';\nimport { GeocodingOptions } from '../types/GeocodingOptions';\n\nconst OPENCAGEDATA_JSON_URL = 'https://api.opencagedata.com/geocode/v1/json';\n\n/**\n * @private\n * @description Returns a {GeocodeError} object with status (validation error)\n * @param code {number} HTTP status code\n * @param message {string} error message\n * @returns {GeocodeError}\n *\n */\nexport function buildValidationError(code: number, message: string) {\n const error = new GeocodeError(message);\n const status: GeocodeErrorStatus = {\n code,\n message,\n };\n error.status = status;\n error.response = { status };\n return error;\n}\n\n/**\n * @private\n * returns true is `param` is not defined or empty\n * @param {String} param object property as a string\n * @return {Boolean} returns value\n */\nexport function isUndefinedOrEmpty(param: string | null | undefined): boolean {\n return typeof param === 'undefined' || param === '';\n}\n\n/**\n * @private\n * returns true is `param` is not defined or null\n * @param {String} param object property as a string\n * @return {Boolean} returns value\n */\nexport function isUndefinedOrNull(\n param: GeocodingRequest | null | undefined\n): boolean {\n return typeof param === 'undefined' || param === null;\n}\n\n/**\n * @private\n *\n * @param input\n * @returns the request query string\n */\nexport function buildQueryString(input: any): string {\n if (isUndefinedOrNull(input)) {\n return '';\n }\n return Object.keys(input)\n .map(\n (key) =>\n `${encodeURIComponent(key)}=${encodeURIComponent(input[key] || '')}`\n )\n .join('&');\n}\n\n/**\n * @private\n * Builds the query params including key and proxy URL\n *\n * @param {GeocodingRequest} input\n * @param {GeocodingOptions} options\n * @returns {Object} {\n * missingKey: boolean,\n * endpoint: string,\n * query: GeocodingRequest copy of the input object with the proxyURL removed\n * }\n */\nexport function buildQuery(\n input: GeocodingRequest,\n options?: GeocodingOptions\n) {\n const query = { ...input };\n let endpoint = OPENCAGEDATA_JSON_URL;\n let missingKey = false;\n if (\n !isUndefinedOrEmpty(input.proxyURL) ||\n !isUndefinedOrEmpty(options?.proxyURL)\n ) {\n endpoint = options?.proxyURL as string;\n if (isUndefinedOrEmpty(endpoint)) {\n endpoint = input.proxyURL as string;\n }\n delete query.proxyURL;\n } else {\n if (isUndefinedOrEmpty(input.key) && typeof process !== 'undefined') {\n query.key = process.env.OPENCAGE_API_KEY;\n }\n if (isUndefinedOrEmpty(query.key)) {\n missingKey = true;\n }\n }\n return {\n missingKey,\n endpoint,\n query,\n };\n}\n"],"names":["__webpack_require__","definition","key","Object","obj","prop","Symbol","OPENCAGEDATA_JSON_URL","buildValidationError","code","message","error","GeocodeError","status","isUndefinedOrEmpty","param","isUndefinedOrNull","buildQueryString","input","encodeURIComponent","buildQuery","options","query","endpoint","missingKey","process"],"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;;;ICN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