@deepzua/nominatim-js
Version:
Unofficial JS SDK for the Nominatim Open Street Map service that allows geocoding and reverse geocoding
62 lines • 3.15 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NominatimJS = void 0;
const cross_fetch_1 = require("cross-fetch");
const PLACES_TYPES = {
node: "N",
way: "W",
relation: "R"
};
;
class NominatimJS {
static normalizeParams(params) {
return Object.assign(Object.assign({}, params), { format: params.format || 'json', "accept-language": params["accept-language"] || params.accept_language });
}
static stringifyOsmId(osmId) {
return `${PLACES_TYPES[osmId.type]}${osmId.id}`;
}
static search(rawParams) {
return __awaiter(this, void 0, void 0, function* () {
const params = NominatimJS.normalizeParams(rawParams);
const countryCodes = params.countrycodes || (params.countryCodesArray ? params.countryCodesArray.join(',') : undefined);
const url = new URL(rawParams.endpoint || `${NominatimJS.NOMINATIM_ENDPOINT}search`);
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
if (countryCodes) {
url.searchParams.append('countrycodes', countryCodes);
}
return (0, cross_fetch_1.default)(url.toJSON())
.then(res => res.json());
});
}
static lookup(osmIds, rawParams) {
return __awaiter(this, void 0, void 0, function* () {
const params = NominatimJS.normalizeParams(rawParams);
const url = new URL(rawParams.endpoint || `${NominatimJS.NOMINATIM_ENDPOINT}lookup`);
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
url.searchParams.append('osm_ids', osmIds.map(NominatimJS.stringifyOsmId).join(','));
return (0, cross_fetch_1.default)(url.toJSON())
.then(res => res.json());
});
}
static reverse(rawParams) {
return __awaiter(this, void 0, void 0, function* () {
const params = NominatimJS.normalizeParams(rawParams);
const url = new URL(rawParams.endpoint || `${NominatimJS.NOMINATIM_ENDPOINT}reverse`);
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
return (0, cross_fetch_1.default)(url.toJSON())
.then(res => res.json());
});
}
}
exports.NominatimJS = NominatimJS;
NominatimJS.NOMINATIM_ENDPOINT = 'https://nominatim.openstreetmap.org/';
//# sourceMappingURL=nominatim-js.js.map