From 0e410dc16e0dd2961affb09ff7efbfb9b6c4999a Mon Sep 17 00:00:00 2001 From: Bruno Caimar Date: Thu, 26 Oct 2023 13:04:38 -0300 Subject: [PATCH] chore: change postal and postalExt to string (#1135) * chore: change postal and postalExt to string Fix #1094 * chore: postalCode-change to accept string or number --- packages/arcgis-rest-geocoding/src/bulk.ts | 4 +- packages/arcgis-rest-geocoding/src/geocode.ts | 16 +++-- .../arcgis-rest-geocoding/test/bulk.test.ts | 45 +++++++++++++ .../test/geocode.test.ts | 64 +++++++++++++++++++ 4 files changed, 123 insertions(+), 6 deletions(-) diff --git a/packages/arcgis-rest-geocoding/src/bulk.ts b/packages/arcgis-rest-geocoding/src/bulk.ts index f9009362db..a70d9408ff 100644 --- a/packages/arcgis-rest-geocoding/src/bulk.ts +++ b/packages/arcgis-rest-geocoding/src/bulk.ts @@ -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; } diff --git a/packages/arcgis-rest-geocoding/src/geocode.ts b/packages/arcgis-rest-geocoding/src/geocode.ts index 90bfb7d60b..96c3c20c7f 100644 --- a/packages/arcgis-rest-geocoding/src/geocode.ts +++ b/packages/arcgis-rest-geocoding/src/geocode.ts @@ -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"; @@ -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. @@ -83,7 +84,7 @@ export interface IGeocodeResponse { * * geocode({ * address: "1600 Pennsylvania Ave", - * postal: 20500, + * postal: "20500", * countryCode: "USA" * }) * .then((response) => { @@ -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 diff --git a/packages/arcgis-rest-geocoding/test/bulk.test.ts b/packages/arcgis-rest-geocoding/test/bulk.test.ts index f529eb512b..5ad17c6aed 100644 --- a/packages/arcgis-rest-geocoding/test/bulk.test.ts +++ b/packages/arcgis-rest-geocoding/test/bulk.test.ts @@ -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); diff --git a/packages/arcgis-rest-geocoding/test/geocode.test.ts b/packages/arcgis-rest-geocoding/test/geocode.test.ts index ecb435241e..a9c4397d0c 100644 --- a/packages/arcgis-rest-geocoding/test/geocode.test.ts +++ b/packages/arcgis-rest-geocoding/test/geocode.test.ts @@ -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); + }); + }); });