smeijer / leaflet-geosearch

A geocoding/address-lookup library supporting various api providers.
https://smeijer.github.io/leaflet-geosearch/
MIT License
1.02k stars 271 forks source link

feat: add Pelias provider #295

Closed missinglink closed 2 years ago

missinglink commented 2 years ago

This PR adds a new provider for Pelias.

Since Pelias is self-hosted and FOSS I've had to change the constructor a little added a 'host' option to allow the user to configure the location of a running Pelias server. The default value for this is http://localhost:4000 which matches the default port bound by Docker projects.

As such you'll need a locally running instance to test, I'll follow up with a small PR which adds the GeocodeEarth provider, this is a hosted version of Pelias run by the core team which provides the same API but with an additional api_key param.

I'm new to TypeScript so please let me know what I broke :P

Screenshot 2021-10-08 at 16 51 51
missinglink commented 2 years ago

Should I maybe refactor the constructor to only accept a single variable like in https://github.com/smeijer/leaflet-geosearch/blob/develop/src/providers/openStreetMapProvider.ts#L28

smeijer commented 2 years ago

@all-contributors please add @missinglink for code, tests

allcontributors[bot] commented 2 years ago

@smeijer

I've put up a pull request to add @missinglink! :tada:

missinglink commented 2 years ago

Actioned feedback via rebase, diff:

diff --git a/docs/providers/pelias.mdx b/docs/providers/pelias.mdx
index afc68ce..93ed5e8 100644
--- a/docs/providers/pelias.mdx
+++ b/docs/providers/pelias.mdx
@@ -24,10 +24,10 @@ See the [Pelias documentation][2] for more detailed information about the availa
 ```js
 import { PeliasProvider } from 'leaflet-geosearch';

-// Pelias servers are self-hosted so you'll need to configure the 'host' string
+// Pelias servers are self-hosted so you'll need to configure the 'options.host' string
 // to identify where requests to your running pelias/api server instance should be sent.
 // note: you SHOULD include the scheme, domain and port but NOT any path or parameters.
-const provider = new PeliasProvider('http://localhost:4000');
+const provider = new PeliasProvider({ host: 'http://localhost:4000' });

 // add to leaflet
 import { GeoSearchControl } from 'leaflet-geosearch';
diff --git a/src/providers/peliasProvider.ts b/src/providers/peliasProvider.ts
index cf06880..9f6af08 100644
--- a/src/providers/peliasProvider.ts
+++ b/src/providers/peliasProvider.ts
@@ -11,6 +11,10 @@ export interface RequestResult {
   features: RawResult[];
 }

+export type PeliasProviderOptions = {
+  host?: string;
+} & ProviderOptions;
+
 export interface RawResult {
   type: 'Feature';
   geometry: {
@@ -90,16 +94,16 @@ export default class PeliasProvider extends AbstractProvider<
   RequestResult,
   RawResult
 > {
-  // Pelias servers are self-hosted so you'll need to configure the 'host' string
+  // Pelias servers are self-hosted so you'll need to configure the 'options.host' string
   // to identify where requests to your running pelias/api server instance should be sent.
   // note: you SHOULD include the scheme, domain and port but NOT any path or parameters.
   // If you're using the Docker setup (https://github.com/pelias/docker)
   // then the default host of 'http://localhost:4000' will work out of the box.
   host: string;

-  constructor(host = 'http://localhost:4000', options: ProviderOptions = {}) {
+  constructor(options: PeliasProviderOptions = {}) {
     super(options);
-    this.host = host;
+    this.host = options.host || 'http://localhost:4000';
   }

   /**
smeijer commented 2 years ago

Sweet! Thanks so much.

smeijer commented 2 years ago

I've published https://github.com/smeijer/leaflet-geosearch/releases/tag/v3.6.0, which includes this pull.