Skip to content

Commit

Permalink
fix(:moyai:): ensure first class citizen geocode request options are …
Browse files Browse the repository at this point in the history
…passed through

AFFECTS PACKAGES:
@esri/arcgis-rest-geocoder
  • Loading branch information
jgravois committed Oct 13, 2018
1 parent b2a6942 commit ad28f27
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
26 changes: 17 additions & 9 deletions packages/arcgis-rest-geocoder/src/geocode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { request, IParams } from "@esri/arcgis-rest-request";
import {
request,
IParams,
appendCustomParams
} from "@esri/arcgis-rest-request";

import {
IExtent,
Expand All @@ -27,6 +31,10 @@ export interface IGeocodeParams extends IParams {
}

export interface IGeocodeRequestOptions extends IEndpointRequestOptions {
/**
* use this if all your address info is contained in a single string.
*/
singleLine?: string;
address?: string;
address2?: string;
address3?: string;
Expand All @@ -53,25 +61,23 @@ export interface IGeocodeResponse {
}

/**
* Used to determine the [location](https://developers.arcgis.com/rest/geocode/api-reference/geocoding-find-address-candidates.htm) of a single address or point of interest.
* Used to determine the location of a single address or point of interest. See the [REST Documentation](https://developers.arcgis.com/rest/geocode/api-reference/geocoding-find-address-candidates.htm) for more information.
*
* ```js
* import { geocode } from '@esri/arcgis-rest-geocoder';
*
* geocode("LAX")
* .then((response) => {
* response.candidates[0].location; // => { x: -118.409, y: 33.943, spatialReference: { wkid: 4326 } }
* response.candidates[0].location; // => { x: -118.409, y: 33.943, spatialReference: ... }
* });
*
* geocode({
* params: {
* address: "1600 Pennsylvania Ave",
* postal: 20500,
* countryCode: "USA"
* }
* address: "1600 Pennsylvania Ave",
* postal: 20500,
* countryCode: "USA"
* })
* .then((response) => {
* response.candidates[0].location; // => { x: -77.036533, y: 38.898719, spatialReference: { wkid: 4326 } }
* response.candidates[0].location; // => { x: -77.036533, y: 38.898719, spatialReference: ... }
* });
* ```
*
Expand All @@ -94,6 +100,8 @@ export function geocode(
...options,
...address
};

appendCustomParams(address, options);
}

// add spatialReference property to individual matches
Expand Down
9 changes: 2 additions & 7 deletions packages/arcgis-rest-geocoder/src/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,16 @@ export function suggest(
): Promise<ISuggestResponse> {
const options: ISuggestRequestOptions = {
endpoint: worldGeocoder,
params: {},
params: { text: partialText },
...requestOptions
};

// is this the most concise way to mixin these optional parameters?
if (requestOptions && requestOptions.params) {
options.params = requestOptions.params;
}
options.params.text = partialText;

if (requestOptions && requestOptions.magicKey) {
options.params.magicKey = requestOptions.magicKey;
}

options.params.text = partialText;

return request(options.endpoint + "suggest", options);
}

Expand Down
27 changes: 27 additions & 0 deletions packages/arcgis-rest-geocoder/test/geocode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ describe("geocode", () => {
});
});

it("should a geocoding request with custom parameters", done => {
fetchMock.once("*", FindAddressCandidates);

geocode({ address: "1600 Pennsylvania Avenue", city: "Washington D.C." })
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(
`address=${encodeURIComponent("1600 Pennsylvania Avenue")}`
);
expect(options.body).toContain(
`city=${encodeURIComponent("Washington D.C.")}`
);
// the only property this lib tacks on
expect(response.spatialReference.wkid).toEqual(4326);
done();
})
.catch(e => {
fail(e);
});
});

it("should make a simple, single geocoding request with a custom parameter", done => {
fetchMock.once("*", FindAddressCandidates);

Expand Down
2 changes: 0 additions & 2 deletions packages/arcgis-rest-geocoder/test/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
* Apache-2.0 */

import { serviceInfo, getGeocodeService } from "../src/helpers";

import * as fetchMock from "fetch-mock";

import { SharingInfo } from "./mocks/responses";

const customGeocoderUrl =
Expand Down
25 changes: 23 additions & 2 deletions packages/arcgis-rest-geocoder/test/suggest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
* Apache-2.0 */

import { suggest } from "../src/suggest";

import * as fetchMock from "fetch-mock";

import { Suggest } from "./mocks/responses";

describe("geocode", () => {
Expand Down Expand Up @@ -74,4 +72,27 @@ describe("geocode", () => {
fail(e);
});
});

it("should make a request for suggestions with magic key and other parameters", done => {
fetchMock.once("*", Suggest);

suggest("LAX", { magicKey: "foo", params: { category: "Address,Postal" } })
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/suggest"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain("text=LAX");
expect(options.body).toContain("magicKey=foo");
expect(options.body).toContain("category=Address%2CPostal");
expect(response).toEqual(Suggest); // this introspects the entire response
done();
})
.catch(e => {
fail(e);
});
});
});

0 comments on commit ad28f27

Please sign in to comment.