@qrvey/map-search
Version:
**@qrvey/map-search** is a .....
254 lines (244 loc) • 8.96 kB
JavaScript
;
var clientLocation = require('@aws-sdk/client-location');
var exponentialBackoff = require('exponential-backoff');
var createSearchClient = require('@azure-rest/maps-search');
var crypto = require('crypto');
var credentialProviderNode = require('@aws-sdk/credential-provider-node');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var createSearchClient__default = /*#__PURE__*/_interopDefault(createSearchClient);
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
// src/services/mapSearch.service.ts
var MapSearchService = class {
/**
* @param connector - A connector that implements the MapConnector interface.
*/
constructor(connector) {
this.connector = connector;
}
/**
* Forward geocode using the injected connector.
* Note: It returns the best match according to params.
*/
async forwardGeocode(params) {
const { query, locationType } = params;
const rawData = await this.connector.forwardGeocode(
query,
locationType
);
return this.connector.transform(rawData, locationType);
}
};
// ../../node_modules/@azure/core-auth/dist/esm/azureKeyCredential.js
var AzureKeyCredential = class {
/**
* The value of the key to be used in authentication
*/
get key() {
return this._key;
}
/**
* Create an instance of an AzureKeyCredential for use
* with a service client.
*
* @param key - The initial value of the key to use in authentication
*/
constructor(key) {
if (!key) {
throw new Error("key must be a non-empty string");
}
this._key = key;
}
/**
* Change the value of the key.
*
* Updates will take effect upon the next request after
* updating the key value.
*
* @param newKey - The new key value to be used
*/
update(newKey) {
this._key = newKey;
}
};
var _a;
typeof ((_a = globalThis === null || globalThis === void 0 ? void 0 : globalThis.crypto) === null || _a === void 0 ? void 0 : _a.randomUUID) === "function" ? globalThis.crypto.randomUUID.bind(globalThis.crypto) : crypto.randomUUID;
// ../../node_modules/@azure/core-util/dist/esm/checkEnvironment.js
var _a2;
var _b;
var _c;
var _d;
typeof self === "object" && typeof (self === null || self === void 0 ? void 0 : self.importScripts) === "function" && (((_a2 = self.constructor) === null || _a2 === void 0 ? void 0 : _a2.name) === "DedicatedWorkerGlobalScope" || ((_b = self.constructor) === null || _b === void 0 ? void 0 : _b.name) === "ServiceWorkerGlobalScope" || ((_c = self.constructor) === null || _c === void 0 ? void 0 : _c.name) === "SharedWorkerGlobalScope");
typeof Deno !== "undefined" && typeof Deno.version !== "undefined" && typeof Deno.version.deno !== "undefined";
typeof Bun !== "undefined" && typeof Bun.version !== "undefined";
typeof globalThis.process !== "undefined" && Boolean(globalThis.process.version) && Boolean((_d = globalThis.process.versions) === null || _d === void 0 ? void 0 : _d.node);
var AzureConnector = class {
constructor(authConfiguration) {
var _a3, _b2;
const apiKey = (_b2 = (_a3 = authConfiguration == null ? void 0 : authConfiguration.apiKey) != null ? _a3 : process.env.MAP_SERVICE_API_KEY) != null ? _b2 : "";
const credential = new AzureKeyCredential(apiKey);
this.client = createSearchClient__default.default(credential);
}
/**
* Azure Location forward geocode method
*/
async forwardGeocode(query, locationType) {
return await exponentialBackoff.backOff(async () => {
const response = await this.client.path("/geocode").get({
queryParameters: {
query: `${query} (${locationType})`,
top: 1,
view: "Unified"
}
});
if (createSearchClient.isUnexpected(response)) {
throw new Error("Failed to fetch Azure geocode data.");
}
return response.body;
});
}
/**
* Transform Azure Location response to map search response
*/
transform(rawData, locationType) {
var _a3, _b2, _c2, _d2, _e, _f;
const place = (_a3 = rawData.features) == null ? void 0 : _a3[0];
if (!place) return {};
const { geometry, properties } = place;
const address = properties == null ? void 0 : properties.address;
return {
type: locationType,
country: (_b2 = address == null ? void 0 : address.countryRegion) == null ? void 0 : _b2.ISO,
geometry: {
type: "Point",
coordinates: (geometry == null ? void 0 : geometry.coordinates) || []
},
label: address == null ? void 0 : address.formattedAddress,
municipality: address == null ? void 0 : address.locality,
postalcode: address == null ? void 0 : address.postalCode,
region: (_d2 = (_c2 = address == null ? void 0 : address.adminDistricts) == null ? void 0 : _c2[0]) == null ? void 0 : _d2.shortName,
street: address == null ? void 0 : address.addressLine,
subregion: (_f = (_e = address == null ? void 0 : address.adminDistricts) == null ? void 0 : _e[1]) == null ? void 0 : _f.shortName,
resultbbox: (place == null ? void 0 : place.bbox) || []
};
}
};
// src/helpers/getMapConnector.ts
function getMapConnector() {
const provider = process.env.PLATFORM_CLOUD;
switch (provider) {
case CLOUD_PROVIDERS.AMAZON:
return new AmazonConnector();
case CLOUD_PROVIDERS.AZURE:
return new AzureConnector();
default:
throw new Error("Unsupported provider");
}
}
// src/helpers/constants.ts
var LOCATION_INDEX_NAME = "Qrvey_LocationIndex";
var CLOUD_PROVIDERS = {
AMAZON: "AWS",
AZURE: "AZURE",
GOOGLE: "GOOGLE",
MAPBOX: "MAPBOX"
};
var GEOPROPERTIES_MAPPING = {
AMAZON: {
country: "CountryType",
state: "RegionType",
county: "SubRegionType",
city: "MunicipalityType",
postal_code: "PostalCodeType",
street: [
"AddressType",
"StreetType",
"IntersectionType",
"PointOfInterestType"
]
},
MAPBOX: {
country: "country",
state: "region",
county: "district",
city: "place",
postal_code: "postcode",
street: "address"
}
};
var AmazonConnector = class {
constructor(authConfiguration) {
var _a3;
const region = (_a3 = authConfiguration == null ? void 0 : authConfiguration.region) != null ? _a3 : process.env.AMAZON_LOCATION_REGION;
this.client = new clientLocation.Location(__spreadValues({
region,
credentials: credentialProviderNode.defaultProvider()
}, authConfiguration != null ? authConfiguration : {}));
}
/**
* Amazon Location forward geocode method
*/
async forwardGeocode(query, locationType) {
locationType = locationType != null ? locationType : "country";
let placeType = GEOPROPERTIES_MAPPING.AMAZON[locationType];
if (!Array.isArray(placeType)) {
placeType = [placeType];
}
return await exponentialBackoff.backOff(
() => this.client.searchPlaceIndexForText({
IndexName: LOCATION_INDEX_NAME,
Text: query,
FilterCategories: locationType === "postal_code" ? void 0 : placeType,
MaxResults: 1
})
);
}
/**
* Transform Amazon Location response to map search response
*/
transform(rawData, locationType) {
var _a3, _b2;
const results = rawData.Results;
const Summary = rawData.Summary;
const place = (_a3 = results == null ? void 0 : results[0]) == null ? void 0 : _a3.Place;
if (!place) return {};
return {
type: locationType,
country: place.Country,
geometry: {
type: "Point",
coordinates: ((_b2 = place.Geometry) == null ? void 0 : _b2.Point) || []
},
label: place.Label,
municipality: place.Municipality,
postalcode: place.PostalCode,
region: place.Region,
street: `${place.Street} ${place.AddressNumber}`,
subregion: place.SubRegion,
resultbbox: Summary == null ? void 0 : Summary.ResultBBox
};
}
};
exports.AmazonConnector = AmazonConnector;
exports.AzureConnector = AzureConnector;
exports.CLOUD_PROVIDERS = CLOUD_PROVIDERS;
exports.GEOPROPERTIES_MAPPING = GEOPROPERTIES_MAPPING;
exports.LOCATION_INDEX_NAME = LOCATION_INDEX_NAME;
exports.MapSearchService = MapSearchService;
exports.getMapConnector = getMapConnector;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map