Skip to content

Commit

Permalink
Add package version to maps/places/routes requests via custom user ag…
Browse files Browse the repository at this point in the history
…ent header (#72)

* Add package version to maps/places/routes requests via custom user agent header.

* Fix map unit tests and added pretest command

* Added navigator.userAgent to custom user agent for maps requests
  • Loading branch information
cgalvan authored Jul 18, 2024
1 parent 5a42cbb commit 09d10d5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
index.html
google.html

# generated version file
/src/version.ts

# production
/build
/dist
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
});

Expand Down
11 changes: 11 additions & 0 deletions src/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions test/maps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 09d10d5

Please sign in to comment.