Skip to content

Commit

Permalink
Add response types to geocoders
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Dec 15, 2024
1 parent 6f7ccf5 commit 2232046
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 27 deletions.
4 changes: 2 additions & 2 deletions spec/arcgis.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { mockFetchRequest } from './mockFetchRequest';
import { ArcGis } from '../src/geocoders/arcgis';
import { ArcGis, ArcGisResponse } from '../src/geocoders/arcgis';

describe('L.Control.Geocoder.ArcGis', () => {
afterEach(() => vi.clearAllMocks());
Expand All @@ -24,7 +24,7 @@ describe('L.Control.Geocoder.ArcGis', () => {
}
}
]
},
} satisfies ArcGisResponse,
() => geocoder.geocode('Innsbruck')
);

Expand Down
4 changes: 2 additions & 2 deletions spec/google.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { mockFetchRequest } from './mockFetchRequest';
import { Google } from '../src/geocoders/google';
import { Google, GoogleResponse } from '../src/geocoders/google';
import { GeocodingResult } from '../src/geocoders/api';

describe('L.Control.Geocoder.Google', () => {
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('L.Control.Geocoder.Google', () => {
}
],
status: 'OK'
},
} satisfies GoogleResponse,
() => geocoder.geocode('Innsbruck')
);

Expand Down
4 changes: 2 additions & 2 deletions spec/here.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { mockFetchRequest } from './mockFetchRequest';
import { HERE } from '../src/geocoders/here';
import { HERE, HEREv2Response } from '../src/geocoders/here';
import { HEREv2 } from '../src/geocoders/here';
import { GeocodingResult } from '../src/geocoders/api';

Expand Down Expand Up @@ -162,7 +162,7 @@ describe('L.Control.Geocoder.HEREv2', () => {
]
}
]
},
} satisfies HEREv2Response,
() => geocoder.geocode('Innsbruck')
);

Expand Down
4 changes: 2 additions & 2 deletions spec/mapbox.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { mockFetchRequest } from './mockFetchRequest';
import { Mapbox } from '../src/geocoders/mapbox';
import { Mapbox, MapboxResponse } from '../src/geocoders/mapbox';

describe('L.Control.Geocoder.Mapbox', () => {
afterEach(() => vi.clearAllMocks());
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('L.Control.Geocoder.Mapbox', () => {
],
attribution:
'NOTICE: © 2018 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare.'
},
} satisfies MapboxResponse,
() => geocoder.geocode('Milwaukee Ave')
);

Expand Down
6 changes: 3 additions & 3 deletions spec/nominatim.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { mockFetchRequest } from './mockFetchRequest';
import { Nominatim } from '../src/geocoders/nominatim';
import { Nominatim, NominatimResponse } from '../src/geocoders/nominatim';

describe('L.Control.Geocoder.Nominatim', () => {
afterEach(() => vi.clearAllMocks());
Expand Down Expand Up @@ -32,7 +32,7 @@ describe('L.Control.Geocoder.Nominatim', () => {
country_code: 'at'
}
}
],
] satisfies NominatimResponse,
() => geocoder.geocode('innsbruck')
);

Expand Down Expand Up @@ -70,7 +70,7 @@ describe('L.Control.Geocoder.Nominatim', () => {
country_code: 'at'
},
boundingbox: ['46.9624854', '47.4499229', '10.9896868', '11.7051742']
},
} satisfies NominatimResponse[number],
() => geocoder.reverse({ lat: 47.3, lng: 11.3 }, 131000)
);

Expand Down
4 changes: 2 additions & 2 deletions spec/pelias.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { mockFetchRequest } from './mockFetchRequest';
import { Openrouteservice } from '../src/geocoders/pelias';
import { Openrouteservice, PeliasResponse } from '../src/geocoders/pelias';

describe('L.Control.Geocoder.Openrouteservice', () => {
afterEach(() => vi.clearAllMocks());
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('L.Control.Geocoder.Openrouteservice', () => {
}
],
bbox: [10.9896885523, 46.9624806033, 11.7051690163, 47.4499185397]
},
} satisfies PeliasResponse,
() => geocoder.geocode('innsbruck')
);

Expand Down
4 changes: 2 additions & 2 deletions spec/photon.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { mockFetchRequest } from './mockFetchRequest';
import { Photon } from '../src/geocoders/photon';
import { Photon, PhotonResponse } from '../src/geocoders/photon';
import { GeocodingResult } from '../src/geocoders/api';

describe('L.Control.Geocoder.Photon', () => {
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('L.Control.Geocoder.Photon', () => {
}
],
type: 'FeatureCollection'
},
} satisfies PhotonResponse,
() => geocoder.geocode('Innsbruck')
);

Expand Down
37 changes: 36 additions & 1 deletion src/geocoders/arcgis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { IGeocoder, GeocoderOptions, geocodingParams, GeocodingResult, reversePa

export interface ArcGisOptions extends GeocoderOptions {}



/**
* Implementation of the [ArcGIS geocoder](https://developers.arcgis.com/features/geocoding/)
*/
Expand All @@ -27,7 +29,10 @@ export class ArcGis implements IGeocoder {
f: 'json'
});

const data = await getJSON<any>(this.options.serviceUrl + '/findAddressCandidates', params);
const data = await getJSON<ArcGisResponse>(
this.options.serviceUrl + '/findAddressCandidates',
params
);
const results: GeocodingResult[] = [];
if (data.candidates && data.candidates.length) {
for (let i = 0; i <= data.candidates.length - 1; i++) {
Expand Down Expand Up @@ -81,3 +86,33 @@ export class ArcGis implements IGeocoder {
export function arcgis(options?: Partial<ArcGisOptions>) {
return new ArcGis(options);
}

/**
* @internal
*/
export interface ArcGisResponse {
spatialReference: {
wkid: number;
latestWkid: number;
};
candidates: Candidate[];
}

interface Candidate {
address: string;
location: {
x: number;
y: number;
};
score: number;
attributes: {
Addr_Type: string;
};
extent: {
xmin: number;
ymin: number;
xmax: number;
ymax: number;
};
}

41 changes: 40 additions & 1 deletion src/geocoders/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Google implements IGeocoder {
key: this.options.apiKey,
address: query
});
const data = await getJSON<any>(this.options.serviceUrl, params);
const data = await getJSON<GoogleResponse>(this.options.serviceUrl, params);
const results: GeocodingResult[] = [];
if (data.results && data.results.length) {
for (let i = 0; i <= data.results.length - 1; i++) {
Expand Down Expand Up @@ -78,3 +78,42 @@ export class Google implements IGeocoder {
export function google(options?: Partial<GoogleOptions>) {
return new Google(options);
}

/**
* @internal
*/
export interface GoogleResponse {
results: Result[];
status: string;
}

interface Result {
address_components: AddressComponent[];
formatted_address: string;
geometry: Geometry;
place_id: string;
types: string[];
}

interface AddressComponent {
long_name: string;
short_name: string;
types: string[];
}

interface Geometry {
bounds: Bounds;
location: Location;
location_type: string;
viewport: Bounds;
}

interface Bounds {
northeast: Location;
southwest: Location;
}

interface Location {
lat: number;
lng: number;
}
90 changes: 89 additions & 1 deletion src/geocoders/here.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class HEREv2 implements IGeocoder {
}

async getJSON(url: string, params: any): Promise<GeocodingResult[]> {
const data = await getJSON<any>(url, params);
const data = await getJSON<HEREv2Response>(url, params);
const results: GeocodingResult[] = [];

if (data.items && data.items.length) {
Expand Down Expand Up @@ -173,3 +173,91 @@ export function here(options?: Partial<HereOptions>) {
return new HERE(options);
}
}

/**
* @internal
*/
export interface HEREv2Response {
items: Item[];
}

interface Item {
title: string;
id: string;
ontologyId: string;
resultType: string;
address: Address;
mapView?: MapView;
position: Position;
access: Position[];
distance: number;
categories: Category[];
references: Reference[];
foodTypes: Category[];
contacts: Contact[];
openingHours: OpeningHour[];
}

interface MapView {
east: number;
north: number;
south: number;
west: number;
}

interface Position {
lat: number;
lng: number;
}

interface Address {
label: string;
countryCode: string;
countryName: string;
stateCode: string;
state: string;
county: string;
city: string;
district: string;
street: string;
postalCode: string;
houseNumber: string;
}

interface Category {
id: string;
name: string;
primary?: boolean;
}

interface Contact {
phone: Email[];
fax: Email[];
www: Email[];
email: Email[];
}

interface Email {
value: string;
}

interface OpeningHour {
text: string[];
isOpen: boolean;
structured: Structured[];
}

interface Structured {
start: string;
duration: string;
recurrence: string;
}

interface Reference {
supplier: Supplier;
id: string;
}

interface Supplier {
id: string;
}
Loading

0 comments on commit 2232046

Please sign in to comment.