Skip to content

Commit

Permalink
chore: change postal and postalExt to string (#1135)
Browse files Browse the repository at this point in the history
* chore: change postal and postalExt to string

Fix #1094

* chore: postalCode-change to accept string or number
  • Loading branch information
BrunoCaimar authored Oct 26, 2023
1 parent 9ec89a2 commit 0e410dc
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/arcgis-rest-geocoding/src/bulk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export interface IAddressBulk {
* The World Geocoding Service considers US states regions.
*/
region?: string;
postal?: number;
postalExt?: number;
postal?: string | number;
postalExt?: string | number;
countryCode?: string;
}

Expand Down
16 changes: 12 additions & 4 deletions packages/arcgis-rest-geocoding/src/geocode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
appendCustomParams,
IExtent,
ISpatialReference,
IPoint
IPoint,
warn
} from "@esri/arcgis-rest-request";

import { ARCGIS_ONLINE_GEOCODING_URL, IEndpointOptions } from "./helpers.js";
Expand All @@ -30,8 +31,8 @@ export interface IGeocodeOptions extends IEndpointOptions {
* The World Geocoding Service expects US states to be passed in as a 'region'.
*/
region?: string;
postal?: number;
postalExt?: number;
postal?: string | number;
postalExt?: string | 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.
Expand Down Expand Up @@ -83,7 +84,7 @@ export interface IGeocodeResponse {
*
* geocode({
* address: "1600 Pennsylvania Ave",
* postal: 20500,
* postal: "20500",
* countryCode: "USA"
* })
* .then((response) => {
Expand Down Expand Up @@ -124,6 +125,13 @@ export function geocode(
],
{ params: { ...address.params } }
);

if (options.params.postal && typeof options.params.postal === "number") {
warn(
"The postal code should be a string. " +
"Issues can arise when using it as a number, especially if they start with zero."
);
}
}

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

it("should send a bulk geocoding request with postal params correctly", (done) => {
fetchMock.once("*", GeocodeAddresses);
const addresses = [
{
OBJECTID: 1,
address: "380 New York St. Redlands",
postal: 92373
},
{
OBJECTID: 2,
address: "1205 Williston Rd",
postal: "05403"
}
];

bulkGeocode({
addresses,
endpoint:
"https://customer.gov/arcgis/rest/services/CompositeGeocoder/GeocodeServer/",
params: {
outSR: 4326,
forStorage: true
}
})
// tslint:disable-next-line
.then((response) => {
expect(fetchMock.called()).toEqual(true);
const [url, options] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://customer.gov/arcgis/rest/services/CompositeGeocoder/GeocodeServer/geocodeAddresses"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain("outSR=4326");
expect(options.body).toContain("forStorage=true");
expect(options.body).toContain(
`addresses=${encodeURIComponent(
'{"records":[{"attributes":{"OBJECTID":1,"address":"380 New York St. Redlands","postal":92373}},{"attributes":{"OBJECTID":2,"address":"1205 Williston Rd","postal":"05403"}}]}'
)}`
);
expect(response.spatialReference.latestWkid).toEqual(4326);
done();
});
});

it("should support rawResponse", (done) => {
fetchMock.once("*", GeocodeAddresses);

Expand Down
64 changes: 64 additions & 0 deletions packages/arcgis-rest-geocoding/test/geocode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,68 @@ describe("geocode", () => {
fail(e);
});
});

it("should make a single geocoding request with a postal code as a string", (done) => {
fetchMock.once("*", FindAddressCandidates);

geocode({
params: {
address: "1205 Williston Rd",
postal: "05403"
}
})
.then((response) => {
expect(fetchMock.called()).toEqual(true);
const [url, options] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://geocode-api.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("1205 Williston Rd")}`
);
expect(options.body).toContain("postal=05403");

expect(response.spatialReference.wkid).toEqual(4326);
expect(response.geoJson.features.length).toBeGreaterThan(0);
done();
})
.catch((e) => {
fail(e);
});
});

it("should make a single geocoding request with a postal code as a number", (done) => {
fetchMock.once("*", FindAddressCandidates);

geocode({
params: {
address: "380 New York St, Redlands, California",
postal: 92373
}
})
.then((response) => {
expect(fetchMock.called()).toEqual(true);
const [url, options] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://geocode-api.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(
"380 New York St, Redlands, California"
)}`
);
expect(options.body).toContain("postal=92373");

expect(response.spatialReference.wkid).toEqual(4326);
expect(response.geoJson.features.length).toBeGreaterThan(0);
done();
})
.catch((e) => {
fail(e);
});
});
});

0 comments on commit 0e410dc

Please sign in to comment.