UNPKG

@openinc/parse-server-opendash

Version:
56 lines (55 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.get = get; exports.getGeoCodingResult = getGeoCodingResult; const config_1 = require("../features/config"); async function get(path, params, qs2 = "") { const GRAPHHOPPER_HOST = config_1.ConfigInstance.getInstance().get("GEO_GRAPHHOPPER_HOST"); if (!GRAPHHOPPER_HOST) { throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, `Feature not enabled [GEO_GRAPHHOPPER-1]`); } const GRAPHHOPPER_API_KEY = config_1.ConfigInstance.getInstance().get("GEO_GRAPHHOPPER_API_KEY"); if (!GRAPHHOPPER_API_KEY) { throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, `Feature not enabled [GEO_GRAPHHOPPER-2]`); } const qs = new URLSearchParams(); qs.set("key", GRAPHHOPPER_API_KEY); qs.set("locale", "de"); for (const [key, value] of Object.entries(params)) { qs.set(key, value); } const url = GRAPHHOPPER_HOST + path + "?" + qs.toString() + qs2; console.log(url); const response = await fetch(url); if (!response.ok) { throw new Error("Bad Statuscode"); } return (await response.json()); } function getGeoCodingResult(hits) { return hits.map((hit) => ({ title: getGeoCodingTitle(hit), description: getGeoCodingDescription(hit), latitude: hit.point.lat, longitude: hit.point.lng, })); } function getGeoCodingTitle(hit) { if (hit.name && hit.housenumber) { return hit.name + " " + hit.housenumber; } return hit.name; } function getGeoCodingDescription(hit) { const country = hit.country ? `, ${hit.country}` : ""; if (hit.city && hit.postcode) { return hit.postcode + " " + hit.city + country; } if (hit.city) { return hit.city + country; } if (hit.postcode) { return hit.postcode + country; } return ""; }