diff --git a/.gitignore b/.gitignore index d641a2e..3afdc6f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,9 @@ index.html google.html +# generated version file +/src/version.ts + # production /build /dist diff --git a/package.json b/package.json index 12735ed..4eaffd4 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,9 @@ "lint": "eslint .", "lint:fix": "eslint --fix .", "typedoc": "typedoc", + "pretest": "node -p \"'export const PACKAGE_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts", "test": "jest --collectCoverage --collectCoverageFrom=src/**/*.{ts,js}", + "prebuild": "node -p \"'export const PACKAGE_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts", "build": "npm-run-all build:*", "build:ts": "npm-run-all build-ts:*", "build:bundle": "rollup -c", diff --git a/src/index.ts b/src/index.ts index ed87a5e..5cbe4a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,6 +31,7 @@ import { } from "./places"; import { MigrationInfoWindow } from "./infoWindow"; import { addListener, addListenerOnce, removeListener } from "./events"; +import { PACKAGE_VERSION } from "./version"; // Dynamically load the MapLibre and MapLibre Geocoder stylesheets so that our migration adapter is the only thing our users need to import // Without this, many MapLibre rendering features won't work (e.g. markers and info windows won't be visible) @@ -86,6 +87,7 @@ const migrationInit = async function () { const client = new LocationClient({ region: region, // Region containing Amazon Location resource + customUserAgent: `migration-adapter-${PACKAGE_VERSION}`, // Append tag with adapter version to the default user agent ...authHelper.getLocationClientConfig(), // Configures the client to use API keys when making supported requests }); diff --git a/src/maps.ts b/src/maps.ts index 062c54f..e1884ee 100644 --- a/src/maps.ts +++ b/src/maps.ts @@ -12,6 +12,7 @@ import { MigrationLatLng, MigrationLatLngBounds, } from "./googleCommon"; +import { PACKAGE_VERSION } from "./version"; /* This migration map class is a thin wrapper replacement for google.maps.Map, which @@ -64,6 +65,16 @@ class MigrationMap { maplibreOptions.pitch = options.tilt; } + // Add our custom user agent header with our package version + maplibreOptions.transformRequest = (url: string) => { + return { + url: url, + headers: { + "X-Amz-User-Agent": `${navigator.userAgent} migration-adapter-${PACKAGE_VERSION}`, + }, + }; + }; + this.#map = new Map(maplibreOptions); // Add NavigationControl if zoomControl is true or not passed in (Google by default adds zoom control to map), diff --git a/test/maps.test.ts b/test/maps.test.ts index 68c9c77..63e87ee 100644 --- a/test/maps.test.ts +++ b/test/maps.test.ts @@ -95,6 +95,7 @@ test("should set migration map options", () => { maxZoom: 18, pitch: 45, bearing: 90, + transformRequest: expect.any(Function), }; expect(testMap).not.toBeNull(); expect(Map).toHaveBeenCalledTimes(1); @@ -117,6 +118,7 @@ test("should set migration map options with control position not available in Ma style: undefined, center: [testLng, testLat], zoom: 9, + transformRequest: expect.any(Function), }; expect(testMap).not.toBeNull(); expect(Map).toHaveBeenCalledTimes(1);