Skip to content

Commit 6c72d49

Browse files
author
Max Pothier
committed
prettify the examples with new option
1 parent 0f4a695 commit 6c72d49

File tree

8 files changed

+77
-70
lines changed

8 files changed

+77
-70
lines changed

examples/international_address_autocomplete.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ const client = clientBuilder.buildInternationalAddressAutocompleteClient();
2424
const country = "CAN";
2525

2626
const summaryLookup = new Lookup({search: "123 Anson", country});
27-
summaryLookup.addCustomParameter("max_results", 1);
27+
// uncomment the following line to add a custom parameter
28+
// summaryLookup.addCustomParameter("max_results", 1);
2829

29-
(async () => {
30-
await handleRequest(summaryLookup, "Response of summary results");
31-
})();
30+
await handleRequest(summaryLookup, "Response of summary results");
3231

33-
// const detailedLookup = new Lookup({addressId: summaryLookup.result[0].addressId, country});
34-
// await handleRequest(detailedLookup, "Response using an address ID to get detailed results");
32+
const detailedLookup = new Lookup({addressId: summaryLookup.result[0].addressId, country});
33+
await handleRequest(detailedLookup, "Response using an address ID to get detailed results");
3534

3635
function logSuggestions(response, message) {
3736
console.log("*** " + message + " ***");

examples/international_street.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const SmartySDK = require("../dist/cjs/index.cjs");
1+
const SmartySDK = require("smartystreets-javascript-sdk");
22
const SmartyCore = SmartySDK.core;
33
const Lookup = SmartySDK.internationalStreet.Lookup;
44

@@ -23,26 +23,25 @@ let client = clientBuilder.buildInternationalStreetClient();
2323
// https://www.smarty.com/docs/cloud/international-street-api#http-input-fields
2424

2525
let lookup1 = new Lookup("CA", "262 Browndale Cr, Richmond Hill, ON");
26+
// uncomment the following line to add a custom parameter
2627
// lookup1.addCustomParameter("input_id", 1234);
27-
// lookup1.addCustomParameter("geocode", true);
28-
29-
// let lookup2 = new Lookup();
30-
// lookup2.geocode = false;
31-
// lookup2.organization = "John Doe";
32-
// lookup2.address1 = "Rua Padre Antonio D'Angelo 121";
33-
// lookup2.address2 = "Casa Verde";
34-
// lookup2.locality = "Sao Paulo";
35-
// lookup2.administrativeArea = "SP";
36-
// lookup2.country = "Brazil";
37-
// lookup2.postalCode = "02516-050";
38-
39-
(async () => {
40-
await handleRequest(lookup1);
41-
})();
42-
// await handleRequest(lookup2)
28+
29+
let lookup2 = new Lookup();
30+
lookup2.inputId = "ID-8675309";
31+
lookup2.geocode = false;
32+
lookup2.organization = "John Doe";
33+
lookup2.address1 = "Rua Padre Antonio D'Angelo 121";
34+
lookup2.address2 = "Casa Verde";
35+
lookup2.locality = "Sao Paulo";
36+
lookup2.administrativeArea = "SP";
37+
lookup2.country = "Brazil";
38+
lookup2.postalCode = "02516-050";
39+
40+
await handleRequest(lookup1)
41+
await handleRequest(lookup2)
4342

4443
function displayResult(result) {
45-
// console.log(result);
44+
console.log(result.result[0].components);
4645
}
4746

4847
function handleError(error) {

examples/us_autocomplete_pro.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ let client = clientBuilder.buildUsAutocompleteProClient();
2424

2525
// *** Simple Lookup ***
2626
let lookup = new Lookup("4770 Lincoln");
27+
// uncomment the following line to add a custom parameter
28+
// lookup.addCustomParameter("max_results", 3);
2729

28-
lookup.addCustomParameter("max_results", 3);
29-
30-
(async () => {
31-
await handleRequest(lookup, "Simple Lookup");
32-
})();
30+
await handleRequest(lookup, "Simple Lookup");
3331

3432
// *** Using Filter and Prefer ***
3533
lookup = new Lookup("4770 Lincoln");
@@ -40,14 +38,14 @@ lookup.preferStates = ["IL"];
4038
lookup.preferRatio = 33;
4139
lookup.source = "all";
4240

43-
// await handleRequest(lookup, "Using Filter and Prefer");
41+
await handleRequest(lookup, "Using Filter and Prefer");
4442

4543
// *** Using 'selected' to Expand Secondaries ***
4644
lookup = new Lookup("4770 Lincoln");
4745

4846
lookup.selected = "4770 N Lincoln Ave Ste 2 (3) Chicago, IL 60625";
4947

50-
// await handleRequest(lookup, "Using 'selected' to Expand Secondaries")
48+
await handleRequest(lookup, "Using 'selected' to Expand Secondaries")
5149

5250
// ************************************************
5351

@@ -64,4 +62,4 @@ async function handleRequest(lookup, lookupType) {
6462
} catch(err) {
6563
console.log(err)
6664
}
67-
}
65+
}

examples/us_enrichment.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const SmartySDK = require("../dist/cjs/index.cjs");
1+
const SmartySDK = require("smartystreets-javascript-sdk");
22
const SmartyCore = SmartySDK.core;
33
const Lookup = SmartySDK.usEnrichment.Lookup;
44

@@ -22,10 +22,9 @@ let client = clientBuilder.buildUsEnrichmentClient();
2222
// Documentation for input fields can be found at:
2323
// https://www.smarty.com/docs/us-street-api#input-fields
2424

25-
let lookup = new Lookup();
26-
lookup.smartyKey = "334968275";
27-
28-
lookup.addCustomParameter("include", "group_financial");
25+
let lookup = new Lookup("334968275");
26+
// uncomment the following line to add a custom parameter
27+
// lookup.addCustomParameter("include", "group_financial");
2928

3029
handleResponse(lookup).then();
3130

examples/us_extract.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
const SmartySDK = require("../dist/cjs/index.cjs");
1+
const SmartySDK = require("smartystreets-javascript-sdk");
22
const SmartyCore = SmartySDK.core;
33
const Lookup = SmartySDK.usExtract.Lookup;
44

55
// for Server-to-server requests, use this code:
6-
let authId = process.env.SMARTY_AUTH_ID;
7-
let authToken = process.env.SMARTY_AUTH_TOKEN;
8-
const credentials = new SmartyCore.StaticCredentials(authId, authToken);
6+
// let authId = process.env.SMARTY_AUTH_ID;
7+
// let authToken = process.env.SMARTY_AUTH_TOKEN;
8+
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);
9+
10+
// for client-side requests (browser/mobile), use this code:
11+
let key = process.env.SMARTY_EMBEDDED_KEY;
12+
const credentials = new SmartyCore.SharedCredentials(key);
913

1014
let clientBuilder = new SmartyCore.ClientBuilder(credentials);
1115
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
@@ -15,28 +19,23 @@ let client = clientBuilder.buildUsExtractClient();
1519
// Documentation for input fields can be found at:
1620
// https://www.smarty.com/docs/cloud/us-extract-api#http-request-input-fields
1721

18-
let lookup = new Lookup(`There are addresses everywhere.
19-
1109 Ninth 85007
20-
Smarty can find them.
21-
3785 Las Vegs Av.
22-
Los Vegas, Nevada
23-
That is all.`);
22+
let lookup = new Lookup("If you work at 1600 Pennsylvania Ave NW, Washington DC you're gonna have a hard time.");
2423
lookup.aggressive = true;
24+
lookup.addressesHaveLineBreaks = false;
2525
lookup.addressesPerLine = 1;
2626

27-
lookup.addCustomParameter("addr_line_breaks", false);
27+
// uncomment the following line to add a custom parameter
28+
// lookup.addCustomParameter("addr_line_breaks", false);
2829

29-
(async () => {
30-
await handleRequest(lookup);
31-
})();
30+
await handleRequest(lookup);
3231

3332
function logResult(response) {
3433
console.log(response.result);
3534
}
3635

3736
async function handleRequest(lookup) {
3837
try {
39-
const response = await client.send(lookup);
38+
const response = await client.send(lookup);
4039
logResult(response);
4140
} catch(err) {
4241
console.log(err);

examples/us_reverse_geo.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Lookup = SmartySDK.usReverseGeo.Lookup;
88
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);
99

1010
// for client-side requests (browser/mobile), use this code:
11-
let key = "your key";
11+
let key = process.env.SMARTY_EMBEDDED_KEY;
1212
const credentials = new SmartyCore.SharedCredentials(key);
1313

1414
// The appropriate license values to be used for your subscriptions
@@ -19,12 +19,10 @@ let clientBuilder = new SmartyCore.ClientBuilder(credentials).withLicenses(["us-
1919
let client = clientBuilder.buildUsReverseGeoClient();
2020

2121
let lookup1 = new Lookup(40.27644, -111.65747);
22+
// uncomment the following line to add a custom parameter
23+
// lookup1.addCustomParameter("source", "all");
2224

23-
lookup1.addCustomParameter("source", "all");
24-
25-
(async () => {
26-
await handleResponse(lookup1);
27-
})();
25+
await handleResponse(lookup1);
2826

2927
function displayResult(result) {
3028
console.log(result.response.results[0].address);

examples/us_street.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ lookup2.maxCandidates = 5;
4545
let lookup3 = new Lookup();
4646
lookup3.inputId = "8675309";
4747
lookup3.street = "1600 Amphitheatre Parkway Mountain View, CA 94043";
48-
lookup3.maxCandidates = 1;
48+
49+
// uncomment the following line to add a custom parameter
50+
// lookup3.addCustomParameter("max_candidates", 1);
4951

5052
// NOTE: batches are not supported when using SharedCredentials.
5153
let batch = new SmartyCore.Batch();

examples/us_zipcode.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const SmartySDK = require("../dist/cjs/index.cjs");
1+
const SmartySDK = require("smartystreets-javascript-sdk");
22
const SmartyCore = SmartySDK.core;
33
const Lookup = SmartySDK.usZipcode.Lookup;
44

@@ -8,7 +8,7 @@ const Lookup = SmartySDK.usZipcode.Lookup;
88
// const credentials = new SmartyCore.StaticCredentials(authId, authToken);
99

1010
// for client-side requests (browser/mobile), use this code:
11-
let key = "your key";
11+
let key = process.env.SMARTY_EMBEDDED_KEY;
1212
const credentials = new SmartyCore.SharedCredentials(key);
1313

1414
let clientBuilder = new SmartyCore.ClientBuilder(credentials);
@@ -21,20 +21,33 @@ let client = clientBuilder.buildUsZipcodeClient();
2121

2222
let lookup1 = new Lookup();
2323
lookup1.inputId = "01189998819991197253"; // Optional ID from your system
24-
lookup1.city = "Provo";
25-
lookup1.state = "Utah";
24+
lookup1.zipCode = "49786";
2625

27-
lookup1.addCustomParameter("zipcode", "84601");
26+
let lookup2 = new Lookup();
27+
lookup2.inputId = "dfc33cb6-829e-4fea-aa1b-b6d6580f0817";
28+
lookup2.city = "Provo";
29+
lookup2.state = "UT";
30+
lookup2.zipCode = "84604";
2831

29-
(async () => {
30-
await handleResponse(lookup1);
31-
})();
32+
let lookup3 = new Lookup();
33+
lookup3.city = "Phoenix";
34+
lookup3.state = "AZ";
3235

36+
// uncomment the following line to add a custom parameter
37+
// lookup3.addCustomParameter("input_id", 1234);
38+
39+
let batch = new SmartyCore.Batch();
40+
batch.add(lookup1);
41+
batch.add(lookup2);
42+
batch.add(lookup3);
43+
44+
await handleResponse(batch);
3345

3446
function viewResults(response) {
35-
response.lookups.map((lookup, i) => {
36-
console.log("zipcodes count: ", lookup.result[0].zipcodes.length)
37-
})
47+
response.lookups.map(lookup => lookup.result.map(candidate => {
48+
candidate.cities.map(city => console.log(city.city));
49+
// candidate.zipcodes.map(zipcode => console.log(zipcode.zipcode));
50+
}));
3851
}
3952

4053
async function handleResponse(lookup) {

0 commit comments

Comments
 (0)