From 636e1653be0b06ff89c4450894e4c62914a9a3c4 Mon Sep 17 00:00:00 2001 From: Ian Wagner Date: Fri, 7 Jul 2023 17:17:51 +0900 Subject: [PATCH] Support base URL configuration (ex: for EU endpoints) --- CHANGELOG.md | 4 ++++ README.md | 7 +++++++ package.json | 2 +- src/index.ts | 9 +++++++-- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afa1271..f2f95cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,3 +7,7 @@ ## v0.5.0 - Adds arrow key-based navigation! + +## v0.6.0 + +- Support changing the base URL (ex: for EU endpoints) diff --git a/README.md b/README.md index f6dcd23..442a8c0 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ export class MapLibreSearchControlOptions { minWaitPeriodMs = 100; layers: PeliasLayer[] = null; onResultSelected?: (feature: PeliasGeoJSONFeature) => void; + baseUrl: string | null = null; } ``` @@ -150,6 +151,12 @@ results, use `['coarse']` for the best performance. A callback to be invoked whenever a result is selected by the user. This is invoked with a single argument, the `PeliasFeature` for the result. This allows you take an action (such as autofilling your own form). +### `baseUrl` + +An optional override to the base API URL. This defaults to the primary Stadia Maps API endpoint. If you want +to use our [EU endpoints](https://docs.stadiamaps.com/eu-gdpr-endpoints/) to ensure traffic is handled by EU servers, +set the `baseUrl` to `https://api-eu.stadiamaps.com`. + ## Development - `dev` - starts dev server diff --git a/package.json b/package.json index 8b7fc65..d01cc58 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@stadiamaps/maplibre-search-box", - "version": "0.5.1", + "version": "0.6.0", "homepage": "https://docs.stadiamaps.com/", "repository": "https://github.com/stadiamaps/maplibre-search-box", "license": "BSD-3-Clause", diff --git a/src/index.ts b/src/index.ts index 490de99..b3961d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import type { IControl, Map } from "maplibre-gl"; import { type AutocompleteRequest, + Configuration, GeocodingApi, PeliasGeoJSONFeature, PeliasLayer, @@ -18,6 +19,7 @@ export class MapLibreSearchControlOptions { minWaitPeriodMs = 100; layers: PeliasLayer[] = null; onResultSelected?: (feature: PeliasGeoJSONFeature) => void; + baseUrl: string | null = null; } export class MapLibreSearchControl implements IControl { @@ -28,17 +30,20 @@ export class MapLibreSearchControl implements IControl { private input: HTMLInputElement | null = null; private clearButton: HTMLElement | null = null; private loadingSpinner: HTMLElement | null = null; - private api = new GeocodingApi(); + private api: GeocodingApi; private lastRequestAt = 0; private lastRequestString = ""; private resultFeatures: PeliasGeoJSONFeature[] = []; private selectedResultIndex: number | null = null; - private originalInput: string = ""; + private originalInput = ""; options = new MapLibreSearchControlOptions(); constructor(options: Partial = {}) { this.options = Object.assign(new MapLibreSearchControlOptions(), options); + this.api = new GeocodingApi( + new Configuration({ basePath: options.baseUrl }) + ); } onAdd(map: Map): HTMLElement {