Skip to content

Commit

Permalink
fix(:bathtub:): remove magicKey from suggest, surface it more promine…
Browse files Browse the repository at this point in the history
…ntly in geocode

AFFECTS PACKAGES:
@esri/arcgis-rest-geocoder

ISSUES CLOSED: #459
  • Loading branch information
jgravois committed Feb 14, 2019
1 parent 9b2b67f commit fb2ba9a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 85 deletions.
31 changes: 14 additions & 17 deletions packages/arcgis-rest-geocoder/src/geocode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@ import {

import { worldGeocoder, IEndpointRequestOptions } from "./helpers";

export interface IGeocodeParams extends IParams {
/**
* You can create an autocomplete experience by making a call to suggest with partial text and then passing through the magicKey and complete address that are returned to geocode.
* ```js
* import { suggest, geocode } from '@esri/arcgis-rest-geocoder';
* suggest("LAX")
* .then((response) => {
* geocode({
* singleLine: response.suggestions[1].text,
* magicKey: response.suggestions[0].magicKey
* })
* })
* ```
*/
magicKey?: string;
}

export interface IGeocodeRequestOptions extends IEndpointRequestOptions {
/**
* use this if all your address info is contained in a single string.
Expand All @@ -51,6 +34,20 @@ export interface IGeocodeRequestOptions extends IEndpointRequestOptions {
postal?: number;
postalExt?: number;
countryCode?: string;
/**
* You can create an autocomplete experience by making a call to suggest with partial text and then passing through the magicKey and complete address that are returned to geocode.
* ```js
* import { suggest, geocode } from '@esri/arcgis-rest-geocoder';
* suggest("LAX")
* .then((response) => {
* geocode({
* singleLine: response.suggestions[1].text,
* magicKey: response.suggestions[0].magicKey
* })
* })
* ```
*/
magicKey?: string;
}

export interface IGeocodeResponse {
Expand Down
25 changes: 2 additions & 23 deletions packages/arcgis-rest-geocoder/src/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@ import { request, cleanUrl } from "@esri/arcgis-rest-request";

import { worldGeocoder, IEndpointRequestOptions } from "./helpers";

export interface ISuggestRequestOptions extends IEndpointRequestOptions {
/**
* You can create an autocomplete experience by making a call to suggest with partial text and then passing through the magicKey and complete address that are returned to geocode.
* ```js
* import { suggest, geocode } from '@esri/arcgis-rest-geocoder';
* suggest("LAX")
* .then((response) => {
* geocode({
* singleLine: response.suggestions[1].text,
* magicKey: response.suggestions[1].magicKey
* })
* })
* ```
*/
magicKey?: string;
}

export interface ISuggestResponse {
suggestions: Array<{
text: string;
Expand All @@ -44,20 +27,16 @@ export interface ISuggestResponse {
*/
export function suggest(
partialText: string,
requestOptions?: ISuggestRequestOptions
requestOptions?: IEndpointRequestOptions
): Promise<ISuggestResponse> {
const options: ISuggestRequestOptions = {
const options: IEndpointRequestOptions = {
endpoint: worldGeocoder,
params: {},
...requestOptions
};

options.params.text = partialText;

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

return request(`${cleanUrl(options.endpoint)}/suggest`, options);
}

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

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

suggest("LAX", { magicKey: "foo" })
.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(response).toEqual(Suggest); // this introspects the entire response
done();
})
.catch(e => {
fail(e);
});
});

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

Expand All @@ -72,27 +50,4 @@ 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 fb2ba9a

Please sign in to comment.