UNPKG

universal-geocoder

Version:

Universal geocoding abstraction server-side and client-side with multiple built-in providers

115 lines 5.04 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { GeoPluginGeocoded, ProviderHelpers, defaultProviderOptions, } from "./.."; import { ResponseError } from "../../error"; import AdminLevel, { ADMIN_LEVEL_CODES } from "../../AdminLevel"; var GeoPluginProvider = /** @class */ (function () { function GeoPluginProvider(_externalLoader, options) { if (options === void 0) { options = defaultProviderOptions; } this.externalLoader = _externalLoader; this.options = __assign(__assign({}, defaultProviderOptions), options); } GeoPluginProvider.prototype.geocode = function (query, callback, errorCallback) { var _this = this; var geocodeQuery = ProviderHelpers.getGeocodeQueryFromParameter(query); if (geocodeQuery.getText()) { throw new Error("The GeoPlugin provider does not support location geocoding, only IP geolocation."); } if (["127.0.0.1", "::1"].includes(geocodeQuery.getIp() || "")) { var geocoded_1 = GeoPluginGeocoded.create({ locality: "localhost", country: "localhost", }); if (!callback) { return new Promise(function (resolve) { return resolve([geocoded_1]); }); } return callback([geocoded_1]); } this.externalLoader.setOptions({ protocol: this.options.useSsl ? "https" : "http", host: "www.geoplugin.net", pathname: "json.gp", }); var params = { ip: geocodeQuery.getIp() || "", }; if (!callback) { return new Promise(function (resolve, reject) { return _this.executeRequest(params, function (results) { return resolve(results); }, {}, {}, function (error) { return reject(error); }); }); } return this.executeRequest(params, callback, {}, {}, errorCallback); }; // eslint-disable-next-line class-methods-use-this GeoPluginProvider.prototype.geodecode = function ( // eslint-disable-next-line @typescript-eslint/no-unused-vars latitudeOrQuery, // eslint-disable-next-line @typescript-eslint/no-unused-vars longitudeOrCallback, // eslint-disable-next-line @typescript-eslint/no-unused-vars callbackOrErrorCallback, // eslint-disable-next-line @typescript-eslint/no-unused-vars errorCallback) { throw new Error("The GeoPlugin provider does not support reverse geocoding."); }; GeoPluginProvider.prototype.executeRequest = function (params, callback, headers, body, errorCallback) { this.externalLoader.executeRequest(params, function (data) { if (![200, 206].includes(data.geoplugin_status)) { var errorMessage_1 = "An error has occurred. Status: " + data.geoplugin_status + "."; if (errorCallback) { errorCallback(new ResponseError(errorMessage_1, data)); return; } setTimeout(function () { throw new Error(errorMessage_1); }); return; } callback([GeoPluginProvider.mapToGeocoded(data)]); }, headers, body, errorCallback); }; GeoPluginProvider.mapToGeocoded = function (result) { var latitude = parseFloat(result.geoplugin_latitude); var longitude = parseFloat(result.geoplugin_longitude); var locality = result.geoplugin_city || undefined; var region = result.geoplugin_region || undefined; var country = result.geoplugin_countryName || undefined; var countryCode = result.geoplugin_countryCode || undefined; var timezone = result.geoplugin_timezone || undefined; var adminLevels = []; var attribution = result.geoplugin_credit || undefined; if (result.geoplugin_regionName) { adminLevels.push(AdminLevel.create({ level: ADMIN_LEVEL_CODES.STATE_CODE, name: result.geoplugin_regionName, code: result.geoplugin_regionCode || undefined, })); } var geocoded = GeoPluginGeocoded.create({ coordinates: { latitude: latitude, longitude: longitude, }, locality: locality, region: region, adminLevels: adminLevels, country: country, countryCode: countryCode, timezone: timezone, attribution: attribution, }); return geocoded; }; return GeoPluginProvider; }()); export default GeoPluginProvider; //# sourceMappingURL=GeoPluginProvider.js.map