From c7486f397a83c241c7ba80dd3a01cdf58700ee8c Mon Sep 17 00:00:00 2001 From: Max Pothier Date: Fri, 15 Nov 2024 14:30:54 -0700 Subject: [PATCH 1/8] adds custom parameters for us_street --- src/InputData.js | 4 ++++ src/us_street/Lookup.js | 6 ++++++ src/util/buildInputData.js | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/src/InputData.js b/src/InputData.js index 4680ea6..1115738 100644 --- a/src/InputData.js +++ b/src/InputData.js @@ -8,6 +8,10 @@ class InputData { if (this.lookupFieldIsPopulated(lookupField)) this.data[apiField] = this.formatData(this.lookup[lookupField]); } + addCustomParameter(key, value) { + this.data[key] = value; + } + formatData(field) { if (Array.isArray(field)) return field.join(";"); else return field; diff --git a/src/us_street/Lookup.js b/src/us_street/Lookup.js index 8a5040e..f08f104 100644 --- a/src/us_street/Lookup.js +++ b/src/us_street/Lookup.js @@ -19,7 +19,13 @@ class Lookup { this.inputId = inputId; this.format = format; this.result = []; + this.customParameters = {}; } + + addCustomParameter(key, value) { + this.customParameters[key] = value; + } + } module.exports = Lookup; diff --git a/src/util/buildInputData.js b/src/util/buildInputData.js index 586698b..d23a8e7 100644 --- a/src/util/buildInputData.js +++ b/src/util/buildInputData.js @@ -3,9 +3,17 @@ const InputData = require("../InputData"); module.exports = (lookup, keyTranslationFormat) => { let inputData = new InputData(lookup); + const hasCustomParameters = Object.keys(lookup.customParameters).length > 0; + for (let key in keyTranslationFormat) { inputData.add(key, keyTranslationFormat[key]); } + if (hasCustomParameters) { + for (let key in lookup.customParameters) { + inputData.addCustomParameter(key, lookup.customParameters[key]); + } + } + return inputData.data; }; From 4be7582350b2c23245b023126aec6585ce34d806 Mon Sep 17 00:00:00 2001 From: Max Pothier Date: Mon, 18 Nov 2024 11:59:09 -0700 Subject: [PATCH 2/8] add custom params to zipcode api --- examples/us_zipcode.js | 32 +++++++++++--------------------- src/us_zipcode/Lookup.js | 5 +++++ 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/examples/us_zipcode.js b/examples/us_zipcode.js index 86cdac7..0e74205 100644 --- a/examples/us_zipcode.js +++ b/examples/us_zipcode.js @@ -1,4 +1,4 @@ -const SmartySDK = require("smartystreets-javascript-sdk"); +const SmartySDK = require("../dist/cjs/index.cjs"); const SmartyCore = SmartySDK.core; const Lookup = SmartySDK.usZipcode.Lookup; @@ -8,7 +8,7 @@ const Lookup = SmartySDK.usZipcode.Lookup; // const credentials = new SmartyCore.StaticCredentials(authId, authToken); // for client-side requests (browser/mobile), use this code: -let key = process.env.SMARTY_EMBEDDED_KEY; +let key = "your key"; const credentials = new SmartyCore.SharedCredentials(key); let clientBuilder = new SmartyCore.ClientBuilder(credentials); @@ -21,30 +21,20 @@ let client = clientBuilder.buildUsZipcodeClient(); let lookup1 = new Lookup(); lookup1.inputId = "01189998819991197253"; // Optional ID from your system -lookup1.zipCode = "49786"; +lookup1.city = "Provo"; +lookup1.state = "Utah"; -let lookup2 = new Lookup(); -lookup2.inputId = "dfc33cb6-829e-4fea-aa1b-b6d6580f0817"; -lookup2.city = "Provo"; -lookup2.state = "UT"; -lookup2.zipCode = "84604"; +lookup1.addCustomParameter("zipcode", "84601"); -let lookup3 = new Lookup(); -lookup3.city = "Phoenix"; -lookup3.state = "AZ"; +(async () => { + await handleResponse(lookup1); +})(); -let batch = new SmartyCore.Batch(); -batch.add(lookup1); -batch.add(lookup2); -batch.add(lookup3); - -await handleResponse(batch); function viewResults(response) { - response.lookups.map(lookup => lookup.result.map(candidate => { - candidate.cities.map(city => console.log(city.city)); - // candidate.zipcodes.map(zipcode => console.log(zipcode.zipcode)); - })); + response.lookups.map((lookup, i) => { + console.log("zipcodes count: ", lookup.result[0].zipcodes.length) + }) } async function handleResponse(lookup) { diff --git a/src/us_zipcode/Lookup.js b/src/us_zipcode/Lookup.js index c4d8633..d74c4b4 100644 --- a/src/us_zipcode/Lookup.js +++ b/src/us_zipcode/Lookup.js @@ -10,6 +10,11 @@ class Lookup { this.zipCode = zipCode; this.inputId = inputId; this.result = []; + this.customParameters = {}; + } + + addCustomParameter(key, value) { + this.customParameters[key] = value; } } From bd3e35eac04e48ff7d05571571f09c14d6e023e4 Mon Sep 17 00:00:00 2001 From: Max Pothier Date: Mon, 18 Nov 2024 16:00:10 -0700 Subject: [PATCH 3/8] custom parameters for extract api --- examples/us_extract.js | 28 ++++++++++++++++------------ src/us_extract/Lookup.js | 5 +++++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/examples/us_extract.js b/examples/us_extract.js index 533b1fe..3ade32c 100644 --- a/examples/us_extract.js +++ b/examples/us_extract.js @@ -1,15 +1,11 @@ -const SmartySDK = require("smartystreets-javascript-sdk"); +const SmartySDK = require("../dist/cjs/index.cjs"); const SmartyCore = SmartySDK.core; const Lookup = SmartySDK.usExtract.Lookup; // for Server-to-server requests, use this code: -// let authId = process.env.SMARTY_AUTH_ID; -// let authToken = process.env.SMARTY_AUTH_TOKEN; -// const credentials = new SmartyCore.StaticCredentials(authId, authToken); - -// for client-side requests (browser/mobile), use this code: -let key = process.env.SMARTY_EMBEDDED_KEY; -const credentials = new SmartyCore.SharedCredentials(key); +let authId = process.env.SMARTY_AUTH_ID; +let authToken = process.env.SMARTY_AUTH_TOKEN; +const credentials = new SmartyCore.StaticCredentials(authId, authToken); let clientBuilder = new SmartyCore.ClientBuilder(credentials); // .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API @@ -19,12 +15,20 @@ let client = clientBuilder.buildUsExtractClient(); // Documentation for input fields can be found at: // https://www.smarty.com/docs/cloud/us-extract-api#http-request-input-fields -let lookup = new Lookup("If you work at 1600 Pennsylvania Ave NW, Washington DC you're gonna have a hard time."); +let lookup = new Lookup(`There are addresses everywhere. + 1109 Ninth 85007 + Smarty can find them. + 3785 Las Vegs Av. + Los Vegas, Nevada + That is all.`); lookup.aggressive = true; -lookup.addressesHaveLineBreaks = false; lookup.addressesPerLine = 1; -await handleRequest(lookup); +lookup.addCustomParameter("addr_line_breaks", false); + +(async () => { + await handleRequest(lookup); +})(); function logResult(response) { console.log(response.result); @@ -32,7 +36,7 @@ function logResult(response) { async function handleRequest(lookup) { try { - const response = await client.send(lookup); + const response = await client.send(lookup); logResult(response); } catch(err) { console.log(err); diff --git a/src/us_extract/Lookup.js b/src/us_extract/Lookup.js index 11de6ec..adf43d4 100644 --- a/src/us_extract/Lookup.js +++ b/src/us_extract/Lookup.js @@ -18,6 +18,11 @@ class Lookup { this.aggressive = undefined; this.addressesHaveLineBreaks = undefined; this.addressesPerLine = undefined; + this.customParameters = {}; + } + + addCustomParameter(key, value) { + this.customParameters[key] = value; } } From f19c524e2f833495334e7effbc56db7fb9aed099 Mon Sep 17 00:00:00 2001 From: Max Pothier Date: Mon, 18 Nov 2024 16:13:00 -0700 Subject: [PATCH 4/8] custom params for rev geo --- examples/us_reverse_geo.js | 10 +++++++--- src/us_reverse_geo/Lookup.js | 5 +++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/examples/us_reverse_geo.js b/examples/us_reverse_geo.js index 1bc76c3..e34515e 100644 --- a/examples/us_reverse_geo.js +++ b/examples/us_reverse_geo.js @@ -8,7 +8,7 @@ const Lookup = SmartySDK.usReverseGeo.Lookup; // const credentials = new SmartyCore.StaticCredentials(authId, authToken); // for client-side requests (browser/mobile), use this code: -let key = process.env.SMARTY_EMBEDDED_KEY; +let key = "your key"; const credentials = new SmartyCore.SharedCredentials(key); // The appropriate license values to be used for your subscriptions @@ -18,9 +18,13 @@ let clientBuilder = new SmartyCore.ClientBuilder(credentials).withLicenses(["us- // .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API let client = clientBuilder.buildUsReverseGeoClient(); -let lookup1 = new Lookup(40.27644, -111.65747, "all"); +let lookup1 = new Lookup(40.27644, -111.65747); -await handleResponse(lookup1); +lookup1.addCustomParameter("source", "all"); + +(async () => { + await handleResponse(lookup1); +})(); function displayResult(result) { console.log(result.response.results[0].address); diff --git a/src/us_reverse_geo/Lookup.js b/src/us_reverse_geo/Lookup.js index 67b6e26..abb36da 100644 --- a/src/us_reverse_geo/Lookup.js +++ b/src/us_reverse_geo/Lookup.js @@ -11,6 +11,11 @@ class Lookup { this.longitude = longitude.toFixed(8); this.source = source; this.response = new Response(); + this.customParameters = {}; + } + + addCustomParameter(key, value) { + this.customParameters[key] = value; } } From 285e26904b3762fff5af12440b3819e2e59eb835 Mon Sep 17 00:00:00 2001 From: Max Pothier Date: Mon, 18 Nov 2024 16:48:28 -0700 Subject: [PATCH 5/8] custom params for enrichment --- examples/us_enrichment.js | 7 +++++-- src/us_enrichment/Lookup.js | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/us_enrichment.js b/examples/us_enrichment.js index e3778df..5bfdd64 100644 --- a/examples/us_enrichment.js +++ b/examples/us_enrichment.js @@ -1,4 +1,4 @@ -const SmartySDK = require("smartystreets-javascript-sdk"); +const SmartySDK = require("../dist/cjs/index.cjs"); const SmartyCore = SmartySDK.core; const Lookup = SmartySDK.usEnrichment.Lookup; @@ -22,7 +22,10 @@ let client = clientBuilder.buildUsEnrichmentClient(); // Documentation for input fields can be found at: // https://www.smarty.com/docs/us-street-api#input-fields -let lookup = new Lookup("334968275"); +let lookup = new Lookup(); +lookup.smartyKey = "334968275"; + +lookup.addCustomParameter("include", "group_financial"); handleResponse(lookup).then(); diff --git a/src/us_enrichment/Lookup.js b/src/us_enrichment/Lookup.js index e61f07b..b1ebe89 100644 --- a/src/us_enrichment/Lookup.js +++ b/src/us_enrichment/Lookup.js @@ -7,7 +7,12 @@ class Lookup { this.dataSubset = dataSubset; this.response = {}; + this.customParameters = {}; }; + + addCustomParameter(key, value) { + this.customParameters[key] = value; + } } module.exports = Lookup; \ No newline at end of file From f5477275c6e9e57030450f4e6d3c337c79e92d98 Mon Sep 17 00:00:00 2001 From: Max Pothier Date: Tue, 19 Nov 2024 16:46:49 -0700 Subject: [PATCH 6/8] custom params for the rest of the apis --- .../international_address_autocomplete.js | 10 ++++-- examples/international_street.js | 35 ++++++++++--------- examples/us_autocomplete_pro.js | 10 ++++-- .../Lookup.js | 5 +++ src/international_street/Lookup.js | 5 +++ src/us_autocomplete_pro/Lookup.js | 7 +++- src/util/buildInputData.js | 2 +- 7 files changed, 50 insertions(+), 24 deletions(-) diff --git a/examples/international_address_autocomplete.js b/examples/international_address_autocomplete.js index a222573..58e30ed 100644 --- a/examples/international_address_autocomplete.js +++ b/examples/international_address_autocomplete.js @@ -24,10 +24,14 @@ const client = clientBuilder.buildInternationalAddressAutocompleteClient(); const country = "CAN"; const summaryLookup = new Lookup({search: "123 Anson", country}); -await handleRequest(summaryLookup, "Response of summary results"); +summaryLookup.addCustomParameter("max_results", 1); -const detailedLookup = new Lookup({addressId: summaryLookup.result[0].addressId, country}); -await handleRequest(detailedLookup, "Response using an address ID to get detailed results"); +(async () => { + await handleRequest(summaryLookup, "Response of summary results"); +})(); + +// const detailedLookup = new Lookup({addressId: summaryLookup.result[0].addressId, country}); +// await handleRequest(detailedLookup, "Response using an address ID to get detailed results"); function logSuggestions(response, message) { console.log("*** " + message + " ***"); diff --git a/examples/international_street.js b/examples/international_street.js index 8062d63..c29db8c 100644 --- a/examples/international_street.js +++ b/examples/international_street.js @@ -1,4 +1,4 @@ -const SmartySDK = require("smartystreets-javascript-sdk"); +const SmartySDK = require("../dist/cjs/index.cjs"); const SmartyCore = SmartySDK.core; const Lookup = SmartySDK.internationalStreet.Lookup; @@ -23,23 +23,26 @@ let client = clientBuilder.buildInternationalStreetClient(); // https://www.smarty.com/docs/cloud/international-street-api#http-input-fields let lookup1 = new Lookup("CA", "262 Browndale Cr, Richmond Hill, ON"); - -let lookup2 = new Lookup(); -lookup2.inputId = "ID-8675309"; -lookup2.geocode = false; -lookup2.organization = "John Doe"; -lookup2.address1 = "Rua Padre Antonio D'Angelo 121"; -lookup2.address2 = "Casa Verde"; -lookup2.locality = "Sao Paulo"; -lookup2.administrativeArea = "SP"; -lookup2.country = "Brazil"; -lookup2.postalCode = "02516-050"; - -await handleRequest(lookup1) -await handleRequest(lookup2) +// lookup1.addCustomParameter("input_id", 1234); +// lookup1.addCustomParameter("geocode", true); + +// let lookup2 = new Lookup(); +// lookup2.geocode = false; +// lookup2.organization = "John Doe"; +// lookup2.address1 = "Rua Padre Antonio D'Angelo 121"; +// lookup2.address2 = "Casa Verde"; +// lookup2.locality = "Sao Paulo"; +// lookup2.administrativeArea = "SP"; +// lookup2.country = "Brazil"; +// lookup2.postalCode = "02516-050"; + +(async () => { + await handleRequest(lookup1); +})(); +// await handleRequest(lookup2) function displayResult(result) { - console.log(result.result[0].components); + // console.log(result); } function handleError(error) { diff --git a/examples/us_autocomplete_pro.js b/examples/us_autocomplete_pro.js index 2107091..5366425 100644 --- a/examples/us_autocomplete_pro.js +++ b/examples/us_autocomplete_pro.js @@ -25,7 +25,11 @@ let client = clientBuilder.buildUsAutocompleteProClient(); // *** Simple Lookup *** let lookup = new Lookup("4770 Lincoln"); -await handleRequest(lookup, "Simple Lookup"); +lookup.addCustomParameter("max_results", 3); + +(async () => { + await handleRequest(lookup, "Simple Lookup"); +})(); // *** Using Filter and Prefer *** lookup = new Lookup("4770 Lincoln"); @@ -36,14 +40,14 @@ lookup.preferStates = ["IL"]; lookup.preferRatio = 33; lookup.source = "all"; -await handleRequest(lookup, "Using Filter and Prefer"); +// await handleRequest(lookup, "Using Filter and Prefer"); // *** Using 'selected' to Expand Secondaries *** lookup = new Lookup("4770 Lincoln"); lookup.selected = "4770 N Lincoln Ave Ste 2 (3) Chicago, IL 60625"; -await handleRequest(lookup, "Using 'selected' to Expand Secondaries") +// await handleRequest(lookup, "Using 'selected' to Expand Secondaries") // ************************************************ diff --git a/src/international_address_autocomplete/Lookup.js b/src/international_address_autocomplete/Lookup.js index 5af6fcb..ea4df48 100644 --- a/src/international_address_autocomplete/Lookup.js +++ b/src/international_address_autocomplete/Lookup.js @@ -8,6 +8,11 @@ class Lookup { this.maxResults = maxResults; this.includeOnlyLocality = includeOnlyLocality; this.includeOnlyPostalCode = includeOnlyPostalCode; + this.customParameters = {}; + } + + addCustomParameter(key, value) { + this.customParameters[key] = value; } } diff --git a/src/international_street/Lookup.js b/src/international_street/Lookup.js index 7292c26..4357ccb 100644 --- a/src/international_street/Lookup.js +++ b/src/international_street/Lookup.js @@ -35,6 +35,11 @@ class Lookup { this.ensureEnoughInfo = this.ensureEnoughInfo.bind(this); this.ensureValidData = this.ensureValidData.bind(this); + this.customParameters = {}; + } + + addCustomParameter(key, value) { + this.customParameters[key] = value; } ensureEnoughInfo() { diff --git a/src/us_autocomplete_pro/Lookup.js b/src/us_autocomplete_pro/Lookup.js index bdeaafc..f316c96 100644 --- a/src/us_autocomplete_pro/Lookup.js +++ b/src/us_autocomplete_pro/Lookup.js @@ -22,7 +22,12 @@ class Lookup { this.preferZIPCodes = []; this.preferRatio = undefined; this.preferGeolocation = undefined; - this.source = undefined + this.source = undefined; + this.customParameters = {}; + } + + addCustomParameter(key, value) { + this.customParameters[key] = value; } } diff --git a/src/util/buildInputData.js b/src/util/buildInputData.js index d23a8e7..de254e8 100644 --- a/src/util/buildInputData.js +++ b/src/util/buildInputData.js @@ -3,7 +3,7 @@ const InputData = require("../InputData"); module.exports = (lookup, keyTranslationFormat) => { let inputData = new InputData(lookup); - const hasCustomParameters = Object.keys(lookup.customParameters).length > 0; + const hasCustomParameters = Object.keys(lookup.customParameters ?? {}).length > 0; for (let key in keyTranslationFormat) { inputData.add(key, keyTranslationFormat[key]); From 0f4a695741735af0ef72e41168940b613795f427 Mon Sep 17 00:00:00 2001 From: Max Pothier Date: Wed, 20 Nov 2024 10:12:53 -0700 Subject: [PATCH 7/8] fix test --- tests/international_address_autocomplete/test_Lookup.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/international_address_autocomplete/test_Lookup.js b/tests/international_address_autocomplete/test_Lookup.js index 0674a09..7d47618 100644 --- a/tests/international_address_autocomplete/test_Lookup.js +++ b/tests/international_address_autocomplete/test_Lookup.js @@ -48,6 +48,7 @@ describe("An International Address Autocomplete lookup", function () { maxResults: 5, includeOnlyLocality: undefined, includeOnlyPostalCode: undefined, + customParameters: {}, }; let lookup = new Lookup(); expect(lookup).to.deep.equal(defaultLookup); From 6c72d494bd6d47f2edcc623d5991d4bcaeb3a7ef Mon Sep 17 00:00:00 2001 From: Max Pothier Date: Wed, 20 Nov 2024 12:24:41 -0700 Subject: [PATCH 8/8] prettify the examples with new option --- .../international_address_autocomplete.js | 11 +++--- examples/international_street.js | 35 +++++++++---------- examples/us_autocomplete_pro.js | 14 ++++---- examples/us_enrichment.js | 9 +++-- examples/us_extract.js | 29 ++++++++------- examples/us_reverse_geo.js | 10 +++--- examples/us_street.js | 4 ++- examples/us_zipcode.js | 35 +++++++++++++------ 8 files changed, 77 insertions(+), 70 deletions(-) diff --git a/examples/international_address_autocomplete.js b/examples/international_address_autocomplete.js index 58e30ed..d256227 100644 --- a/examples/international_address_autocomplete.js +++ b/examples/international_address_autocomplete.js @@ -24,14 +24,13 @@ const client = clientBuilder.buildInternationalAddressAutocompleteClient(); const country = "CAN"; const summaryLookup = new Lookup({search: "123 Anson", country}); -summaryLookup.addCustomParameter("max_results", 1); +// uncomment the following line to add a custom parameter +// summaryLookup.addCustomParameter("max_results", 1); -(async () => { - await handleRequest(summaryLookup, "Response of summary results"); -})(); +await handleRequest(summaryLookup, "Response of summary results"); -// const detailedLookup = new Lookup({addressId: summaryLookup.result[0].addressId, country}); -// await handleRequest(detailedLookup, "Response using an address ID to get detailed results"); +const detailedLookup = new Lookup({addressId: summaryLookup.result[0].addressId, country}); +await handleRequest(detailedLookup, "Response using an address ID to get detailed results"); function logSuggestions(response, message) { console.log("*** " + message + " ***"); diff --git a/examples/international_street.js b/examples/international_street.js index c29db8c..c3f4478 100644 --- a/examples/international_street.js +++ b/examples/international_street.js @@ -1,4 +1,4 @@ -const SmartySDK = require("../dist/cjs/index.cjs"); +const SmartySDK = require("smartystreets-javascript-sdk"); const SmartyCore = SmartySDK.core; const Lookup = SmartySDK.internationalStreet.Lookup; @@ -23,26 +23,25 @@ let client = clientBuilder.buildInternationalStreetClient(); // https://www.smarty.com/docs/cloud/international-street-api#http-input-fields let lookup1 = new Lookup("CA", "262 Browndale Cr, Richmond Hill, ON"); +// uncomment the following line to add a custom parameter // lookup1.addCustomParameter("input_id", 1234); -// lookup1.addCustomParameter("geocode", true); - -// let lookup2 = new Lookup(); -// lookup2.geocode = false; -// lookup2.organization = "John Doe"; -// lookup2.address1 = "Rua Padre Antonio D'Angelo 121"; -// lookup2.address2 = "Casa Verde"; -// lookup2.locality = "Sao Paulo"; -// lookup2.administrativeArea = "SP"; -// lookup2.country = "Brazil"; -// lookup2.postalCode = "02516-050"; - -(async () => { - await handleRequest(lookup1); -})(); -// await handleRequest(lookup2) + +let lookup2 = new Lookup(); +lookup2.inputId = "ID-8675309"; +lookup2.geocode = false; +lookup2.organization = "John Doe"; +lookup2.address1 = "Rua Padre Antonio D'Angelo 121"; +lookup2.address2 = "Casa Verde"; +lookup2.locality = "Sao Paulo"; +lookup2.administrativeArea = "SP"; +lookup2.country = "Brazil"; +lookup2.postalCode = "02516-050"; + +await handleRequest(lookup1) +await handleRequest(lookup2) function displayResult(result) { - // console.log(result); + console.log(result.result[0].components); } function handleError(error) { diff --git a/examples/us_autocomplete_pro.js b/examples/us_autocomplete_pro.js index 5366425..52a8321 100644 --- a/examples/us_autocomplete_pro.js +++ b/examples/us_autocomplete_pro.js @@ -24,12 +24,10 @@ let client = clientBuilder.buildUsAutocompleteProClient(); // *** Simple Lookup *** let lookup = new Lookup("4770 Lincoln"); +// uncomment the following line to add a custom parameter +// lookup.addCustomParameter("max_results", 3); -lookup.addCustomParameter("max_results", 3); - -(async () => { - await handleRequest(lookup, "Simple Lookup"); -})(); +await handleRequest(lookup, "Simple Lookup"); // *** Using Filter and Prefer *** lookup = new Lookup("4770 Lincoln"); @@ -40,14 +38,14 @@ lookup.preferStates = ["IL"]; lookup.preferRatio = 33; lookup.source = "all"; -// await handleRequest(lookup, "Using Filter and Prefer"); +await handleRequest(lookup, "Using Filter and Prefer"); // *** Using 'selected' to Expand Secondaries *** lookup = new Lookup("4770 Lincoln"); lookup.selected = "4770 N Lincoln Ave Ste 2 (3) Chicago, IL 60625"; -// await handleRequest(lookup, "Using 'selected' to Expand Secondaries") +await handleRequest(lookup, "Using 'selected' to Expand Secondaries") // ************************************************ @@ -64,4 +62,4 @@ async function handleRequest(lookup, lookupType) { } catch(err) { console.log(err) } -} +} \ No newline at end of file diff --git a/examples/us_enrichment.js b/examples/us_enrichment.js index 5bfdd64..1a07371 100644 --- a/examples/us_enrichment.js +++ b/examples/us_enrichment.js @@ -1,4 +1,4 @@ -const SmartySDK = require("../dist/cjs/index.cjs"); +const SmartySDK = require("smartystreets-javascript-sdk"); const SmartyCore = SmartySDK.core; const Lookup = SmartySDK.usEnrichment.Lookup; @@ -22,10 +22,9 @@ let client = clientBuilder.buildUsEnrichmentClient(); // Documentation for input fields can be found at: // https://www.smarty.com/docs/us-street-api#input-fields -let lookup = new Lookup(); -lookup.smartyKey = "334968275"; - -lookup.addCustomParameter("include", "group_financial"); +let lookup = new Lookup("334968275"); +// uncomment the following line to add a custom parameter +// lookup.addCustomParameter("include", "group_financial"); handleResponse(lookup).then(); diff --git a/examples/us_extract.js b/examples/us_extract.js index 3ade32c..80bc579 100644 --- a/examples/us_extract.js +++ b/examples/us_extract.js @@ -1,11 +1,15 @@ -const SmartySDK = require("../dist/cjs/index.cjs"); +const SmartySDK = require("smartystreets-javascript-sdk"); const SmartyCore = SmartySDK.core; const Lookup = SmartySDK.usExtract.Lookup; // for Server-to-server requests, use this code: -let authId = process.env.SMARTY_AUTH_ID; -let authToken = process.env.SMARTY_AUTH_TOKEN; -const credentials = new SmartyCore.StaticCredentials(authId, authToken); +// let authId = process.env.SMARTY_AUTH_ID; +// let authToken = process.env.SMARTY_AUTH_TOKEN; +// const credentials = new SmartyCore.StaticCredentials(authId, authToken); + +// for client-side requests (browser/mobile), use this code: +let key = process.env.SMARTY_EMBEDDED_KEY; +const credentials = new SmartyCore.SharedCredentials(key); let clientBuilder = new SmartyCore.ClientBuilder(credentials); // .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API @@ -15,20 +19,15 @@ let client = clientBuilder.buildUsExtractClient(); // Documentation for input fields can be found at: // https://www.smarty.com/docs/cloud/us-extract-api#http-request-input-fields -let lookup = new Lookup(`There are addresses everywhere. - 1109 Ninth 85007 - Smarty can find them. - 3785 Las Vegs Av. - Los Vegas, Nevada - That is all.`); +let lookup = new Lookup("If you work at 1600 Pennsylvania Ave NW, Washington DC you're gonna have a hard time."); lookup.aggressive = true; +lookup.addressesHaveLineBreaks = false; lookup.addressesPerLine = 1; -lookup.addCustomParameter("addr_line_breaks", false); +// uncomment the following line to add a custom parameter +// lookup.addCustomParameter("addr_line_breaks", false); -(async () => { - await handleRequest(lookup); -})(); +await handleRequest(lookup); function logResult(response) { console.log(response.result); @@ -36,7 +35,7 @@ function logResult(response) { async function handleRequest(lookup) { try { - const response = await client.send(lookup); + const response = await client.send(lookup); logResult(response); } catch(err) { console.log(err); diff --git a/examples/us_reverse_geo.js b/examples/us_reverse_geo.js index e34515e..01ab814 100644 --- a/examples/us_reverse_geo.js +++ b/examples/us_reverse_geo.js @@ -8,7 +8,7 @@ const Lookup = SmartySDK.usReverseGeo.Lookup; // const credentials = new SmartyCore.StaticCredentials(authId, authToken); // for client-side requests (browser/mobile), use this code: -let key = "your key"; +let key = process.env.SMARTY_EMBEDDED_KEY; const credentials = new SmartyCore.SharedCredentials(key); // The appropriate license values to be used for your subscriptions @@ -19,12 +19,10 @@ let clientBuilder = new SmartyCore.ClientBuilder(credentials).withLicenses(["us- let client = clientBuilder.buildUsReverseGeoClient(); let lookup1 = new Lookup(40.27644, -111.65747); +// uncomment the following line to add a custom parameter +// lookup1.addCustomParameter("source", "all"); -lookup1.addCustomParameter("source", "all"); - -(async () => { - await handleResponse(lookup1); -})(); +await handleResponse(lookup1); function displayResult(result) { console.log(result.response.results[0].address); diff --git a/examples/us_street.js b/examples/us_street.js index 4a22e2b..e09211e 100644 --- a/examples/us_street.js +++ b/examples/us_street.js @@ -45,7 +45,9 @@ lookup2.maxCandidates = 5; let lookup3 = new Lookup(); lookup3.inputId = "8675309"; lookup3.street = "1600 Amphitheatre Parkway Mountain View, CA 94043"; -lookup3.maxCandidates = 1; + +// uncomment the following line to add a custom parameter +// lookup3.addCustomParameter("max_candidates", 1); // NOTE: batches are not supported when using SharedCredentials. let batch = new SmartyCore.Batch(); diff --git a/examples/us_zipcode.js b/examples/us_zipcode.js index 0e74205..5167f5e 100644 --- a/examples/us_zipcode.js +++ b/examples/us_zipcode.js @@ -1,4 +1,4 @@ -const SmartySDK = require("../dist/cjs/index.cjs"); +const SmartySDK = require("smartystreets-javascript-sdk"); const SmartyCore = SmartySDK.core; const Lookup = SmartySDK.usZipcode.Lookup; @@ -8,7 +8,7 @@ const Lookup = SmartySDK.usZipcode.Lookup; // const credentials = new SmartyCore.StaticCredentials(authId, authToken); // for client-side requests (browser/mobile), use this code: -let key = "your key"; +let key = process.env.SMARTY_EMBEDDED_KEY; const credentials = new SmartyCore.SharedCredentials(key); let clientBuilder = new SmartyCore.ClientBuilder(credentials); @@ -21,20 +21,33 @@ let client = clientBuilder.buildUsZipcodeClient(); let lookup1 = new Lookup(); lookup1.inputId = "01189998819991197253"; // Optional ID from your system -lookup1.city = "Provo"; -lookup1.state = "Utah"; +lookup1.zipCode = "49786"; -lookup1.addCustomParameter("zipcode", "84601"); +let lookup2 = new Lookup(); +lookup2.inputId = "dfc33cb6-829e-4fea-aa1b-b6d6580f0817"; +lookup2.city = "Provo"; +lookup2.state = "UT"; +lookup2.zipCode = "84604"; -(async () => { - await handleResponse(lookup1); -})(); +let lookup3 = new Lookup(); +lookup3.city = "Phoenix"; +lookup3.state = "AZ"; +// uncomment the following line to add a custom parameter +// lookup3.addCustomParameter("input_id", 1234); + +let batch = new SmartyCore.Batch(); +batch.add(lookup1); +batch.add(lookup2); +batch.add(lookup3); + +await handleResponse(batch); function viewResults(response) { - response.lookups.map((lookup, i) => { - console.log("zipcodes count: ", lookup.result[0].zipcodes.length) - }) + response.lookups.map(lookup => lookup.result.map(candidate => { + candidate.cities.map(city => console.log(city.city)); + // candidate.zipcodes.map(zipcode => console.log(zipcode.zipcode)); + })); } async function handleResponse(lookup) {