Skip to content

Commit

Permalink
fix(geocode): max sure user supplied request options are all passed t…
Browse files Browse the repository at this point in the history
…hrough

AFFECTS PACKAGES:
@esri/arcgis-rest-geocoder
  • Loading branch information
jgravois committed May 10, 2018
1 parent fd56e6f commit 3ffa710
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/arcgis-rest-geocoder/src/geocoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,20 @@ export interface IGetGeocodeServiceResponse {
*/
export function geocode(
address: string | IGeocodeRequestOptions
// requestOptions?: IGeocodeRequestOptions
): Promise<IGeocodeResponse> {
const options: IGeocodeRequestOptions = {
let options: IGeocodeRequestOptions = {
endpoint: worldGeocoder,
params: {}
};

if (typeof address === "string") {
options.params.singleLine = address;
} else {
options.params = { ...address.params };
options.endpoint = address.endpoint || worldGeocoder;
options = {
...options,
...address
};
}

// add spatialReference property to individual matches
Expand Down
28 changes: 28 additions & 0 deletions packages/arcgis-rest-geocoder/test/geocoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,34 @@ describe("geocode", () => {
});
});

it("should pass through all requestOptions when making a geocoding request", done => {
fetchMock.once("*", FindAddressCandidates);

geocode({
endpoint: customGeocoderUrl,
params: {
outSr: 3857,
address: "380 New York St",
postal: 92373
},
httpMethod: "GET"
})
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://foo.com/arcgis/rest/services/Custom/GeocodeServer/findAddressCandidates?f=json&outSr=3857&address=380%20New%20York%20St&postal=92373"
);
expect(options.method).toBe("GET");
// the only property this lib tacks on
expect(response.spatialReference.wkid).toEqual(4326);
done();
})
.catch(e => {
fail(e);
});
});

it("should make a reverse geocoding request", done => {
fetchMock.once("*", ReverseGeocode);

Expand Down

0 comments on commit 3ffa710

Please sign in to comment.