Skip to content

Commit 621ebb4

Browse files
committed
Restore JavaScript examples and split Makefile into separate TS/JS targets
Keep both .mjs and .ts example files so consumers have samples in both languages. Adds make examples-ts, make examples-js, and make examples (runs both).
1 parent 9700ba9 commit 621ebb4

11 files changed

+585
-3
lines changed

Makefile

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,21 @@ compile: build
2323
cover: node_modules
2424
npm run test -- --reporter=spec
2525

26-
examples: build
27-
@echo "Running examples..."
26+
examples-ts: build
27+
@echo "Running TypeScript examples..."
28+
@npx tsx examples/us_street.ts || true
29+
@npx tsx examples/us_street_iana_timezone.ts || true
30+
@npx tsx examples/us_zipcode.ts || true
31+
@npx tsx examples/us_autocomplete_pro.ts || true
32+
@npx tsx examples/us_extract.ts || true
33+
@npx tsx examples/us_reverse_geo.ts || true
34+
@npx tsx examples/us_enrichment.ts || true
35+
@npx tsx examples/international_street.ts || true
36+
@npx tsx examples/international_address_autocomplete.ts || true
37+
@npx tsx examples/international_postal_code.ts || true
38+
39+
examples-js: build
40+
@echo "Running JavaScript examples..."
2841
@node examples/us_street.mjs || true
2942
@node examples/us_street_iana_timezone.mjs || true
3043
@node examples/us_zipcode.mjs || true
@@ -36,6 +49,8 @@ examples: build
3649
@node examples/international_address_autocomplete.mjs || true
3750
@node examples/international_postal_code.mjs || true
3851

52+
examples: examples-ts examples-js
53+
3954
integrate: examples
4055

4156
version:
@@ -45,4 +60,4 @@ version:
4560
publish: test build version
4661
npm publish
4762

48-
.PHONY: test fmt clean build compile cover examples integrate version publish
63+
.PHONY: test fmt clean build compile cover examples examples-ts examples-js integrate version publish
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { ClientBuilder, BasicAuthCredentials, LookupInternationalAddressAutocomplete } from "smartystreets-javascript-sdk";
2+
3+
// for client-side requests (browser/mobile), use this code:
4+
// import { SharedCredentials } from "smartystreets-javascript-sdk";
5+
// const key: string = process.env.SMARTY_EMBEDDED_KEY!;
6+
// const credentials = new SharedCredentials(key);
7+
8+
// for Server-to-server requests, use this code:
9+
const authId = process.env.SMARTY_AUTH_ID!;
10+
const authToken = process.env.SMARTY_AUTH_TOKEN!;
11+
const credentials = new BasicAuthCredentials(authId, authToken);
12+
13+
// The appropriate license values to be used for your subscriptions
14+
// can be found on the Subscription page of the account dashboard.
15+
// https://www.smarty.com/docs/cloud/licensing
16+
const clientBuilder = new ClientBuilder(credentials);
17+
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
18+
19+
const client = clientBuilder.buildInternationalAddressAutocompleteClient();
20+
21+
// Documentation for input fields can be found at:
22+
// www.smarty.com/docs/cloud/international-address-autocomplete-api#pro-http-request-input-fields
23+
const country = "CAN";
24+
25+
function logSuggestions(response: LookupInternationalAddressAutocomplete, message: string): void {
26+
console.log("*** " + message + " ***");
27+
28+
response.result.forEach((suggestion) => {
29+
if (suggestion.addressText) {
30+
console.log("Entries: ", suggestion.entries);
31+
console.log("Address Text: ", suggestion.addressText);
32+
console.log("Address ID: ", suggestion.addressId);
33+
} else {
34+
console.log("Street: ", suggestion.street);
35+
console.log("Locality: ", suggestion.locality);
36+
console.log("Administrative Area: ", suggestion.administrativeArea);
37+
console.log("Postal Code: ", suggestion.postalCode);
38+
console.log("Country: ", suggestion.countryIso3);
39+
}
40+
});
41+
console.log("\n");
42+
}
43+
44+
async function handleRequest(lookup: LookupInternationalAddressAutocomplete, lookupType: string): Promise<void> {
45+
try {
46+
const results = await client.send(lookup);
47+
logSuggestions(results, lookupType);
48+
} catch (err: unknown) {
49+
console.error(err);
50+
}
51+
}
52+
53+
async function main(): Promise<void> {
54+
const summaryLookup = new LookupInternationalAddressAutocomplete({
55+
search: "123 Anson",
56+
country,
57+
maxGroupResults: 50,
58+
geolocation: true,
59+
});
60+
61+
await handleRequest(summaryLookup, "Response of summary results");
62+
63+
const detailedLookup = new LookupInternationalAddressAutocomplete({
64+
addressId: summaryLookup.result[0].addressId,
65+
country,
66+
});
67+
await handleRequest(detailedLookup, "Response using an address ID to get detailed results");
68+
}
69+
70+
main();
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { ClientBuilder, BasicAuthCredentials, LookupInternationalPostalCode } from "smartystreets-javascript-sdk";
2+
3+
// for client-side requests (browser/mobile), use this code:
4+
// import { SharedCredentials } from "smartystreets-javascript-sdk";
5+
// const key: string = process.env.SMARTY_EMBEDDED_KEY!;
6+
// const credentials = new SharedCredentials(key);
7+
8+
// for Server-to-server requests, use this code:
9+
const authId = process.env.SMARTY_AUTH_ID!;
10+
const authToken = process.env.SMARTY_AUTH_TOKEN!;
11+
const credentials = new BasicAuthCredentials(authId, authToken);
12+
13+
// The appropriate license values to be used for your subscriptions
14+
// can be found on the Subscription page of the account dashboard.
15+
// https://www.smarty.com/docs/cloud/licensing
16+
const clientBuilder = new ClientBuilder(credentials);
17+
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
18+
const client = clientBuilder.buildInternationalPostalCodeClient();
19+
20+
// Documentation for input fields can be found at:
21+
// https://www.smarty.com/docs/cloud/international-postal-code-api#input-fields
22+
23+
function displayResult(lookup: LookupInternationalPostalCode, message: string): void {
24+
console.log("*** " + message + " ***");
25+
if (lookup.result && lookup.result.length > 0) {
26+
lookup.result.forEach((result) => {
27+
console.log("Input ID:", result.inputId);
28+
console.log("Administrative Area:", result.administrativeArea);
29+
console.log("Super Administrative Area:", result.superAdministrativeArea);
30+
console.log("Sub Administrative Area:", result.subAdministrativeArea);
31+
console.log("Locality:", result.locality);
32+
console.log("Dependent Locality:", result.dependentLocality);
33+
console.log("Dependent Locality Name:", result.dependentLocalityName);
34+
console.log("Double Dependent Locality:", result.doubleDependentLocality);
35+
console.log("Postal Code:", result.postalCode);
36+
console.log("Postal Code Extra:", result.postalCodeExtra);
37+
console.log("Country ISO3:", result.countryIso3);
38+
console.log("---");
39+
});
40+
} else {
41+
console.log("No results found");
42+
}
43+
console.log("\n");
44+
}
45+
46+
async function handleResponse(lookup: LookupInternationalPostalCode, lookupType: string): Promise<void> {
47+
try {
48+
const result = await client.send(lookup);
49+
displayResult(result, lookupType);
50+
} catch (err: unknown) {
51+
console.error("ERROR:", err);
52+
}
53+
}
54+
55+
async function main(): Promise<void> {
56+
// Lookup by postal code and country
57+
const lookup1 = new LookupInternationalPostalCode("Australia", "2776");
58+
// uncomment the following line to add a custom parameter
59+
// lookup1.addCustomParameter("input_id", 1234);
60+
61+
// Lookup by locality, administrative area, and country
62+
const lookup2 = new LookupInternationalPostalCode("Brazil", undefined, "SP", "Sao Paulo", "ID-8675309");
63+
64+
await handleResponse(lookup1, "Postal code lookup");
65+
await handleResponse(lookup2, "Locality and administrative area lookup");
66+
}
67+
68+
main();

examples/international_street.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { ClientBuilder, BasicAuthCredentials, LookupInternationalStreet } from "smartystreets-javascript-sdk";
2+
3+
// for client-side requests (browser/mobile), use this code:
4+
// import { SharedCredentials } from "smartystreets-javascript-sdk";
5+
// const key: string = process.env.SMARTY_EMBEDDED_KEY!;
6+
// const credentials = new SharedCredentials(key);
7+
8+
// for Server-to-server requests, use this code:
9+
const authId = process.env.SMARTY_AUTH_ID!;
10+
const authToken = process.env.SMARTY_AUTH_TOKEN!;
11+
const credentials = new BasicAuthCredentials(authId, authToken);
12+
13+
// The appropriate license values to be used for your subscriptions
14+
// can be found on the Subscription page of the account dashboard.
15+
// https://www.smarty.com/docs/cloud/licensing
16+
const clientBuilder = new ClientBuilder(credentials);
17+
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
18+
19+
const client = clientBuilder.buildInternationalStreetClient();
20+
21+
// Documentation for input fields can be found at:
22+
// https://www.smarty.com/docs/cloud/international-street-api#http-input-fields
23+
24+
function displayResult(result: LookupInternationalStreet): void {
25+
console.log(result.result[0].components);
26+
}
27+
28+
async function handleRequest(lookup: LookupInternationalStreet): Promise<void> {
29+
try {
30+
const result = await client.send(lookup);
31+
displayResult(result);
32+
} catch (err: unknown) {
33+
console.error("ERROR:", err);
34+
}
35+
}
36+
37+
async function main(): Promise<void> {
38+
const lookup1 = new LookupInternationalStreet("CA", "262 Browndale Cr, Richmond Hill, ON");
39+
// uncomment the following line to add a custom parameter
40+
// lookup1.addCustomParameter("input_id", 1234);
41+
42+
const lookup2 = new LookupInternationalStreet();
43+
lookup2.inputId = "ID-8675309";
44+
lookup2.organization = "John Doe";
45+
lookup2.address1 = "Rua Padre Antonio D'Angelo 121";
46+
lookup2.address2 = "Casa Verde";
47+
lookup2.locality = "Sao Paulo";
48+
lookup2.administrativeArea = "SP";
49+
lookup2.country = "Brazil";
50+
lookup2.postalCode = "02516-050";
51+
52+
await handleRequest(lookup1);
53+
await handleRequest(lookup2);
54+
}
55+
56+
main();

examples/us_autocomplete_pro.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { ClientBuilder, BasicAuthCredentials, LookupUSAutocompletePro } from "smartystreets-javascript-sdk";
2+
3+
// for client-side requests (browser/mobile), use this code:
4+
// import { SharedCredentials } from "smartystreets-javascript-sdk";
5+
// const key: string = process.env.SMARTY_EMBEDDED_KEY!;
6+
// const credentials = new SharedCredentials(key);
7+
8+
// for Server-to-server requests, use this code:
9+
const authId = process.env.SMARTY_AUTH_ID!;
10+
const authToken = process.env.SMARTY_AUTH_TOKEN!;
11+
const credentials = new BasicAuthCredentials(authId, authToken);
12+
13+
// The appropriate license values to be used for your subscriptions
14+
// can be found on the Subscription page of the account dashboard.
15+
// https://www.smarty.com/docs/cloud/licensing
16+
const clientBuilder = new ClientBuilder(credentials);
17+
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
18+
19+
const client = clientBuilder.buildUsAutocompleteProClient();
20+
21+
// Documentation for input fields can be found at:
22+
// https://www.smarty.com/docs/cloud/us-autocomplete-api#pro-http-request-input-fields
23+
24+
// ************************************************
25+
26+
function logSuggestions(response: LookupUSAutocompletePro, message: string): void {
27+
console.log(message);
28+
console.log(response.result);
29+
console.log("*********************");
30+
}
31+
32+
async function handleRequest(lookup: LookupUSAutocompletePro, lookupType: string): Promise<void> {
33+
try {
34+
const results = await client.send(lookup);
35+
logSuggestions(results, lookupType);
36+
} catch (err: unknown) {
37+
console.error(err);
38+
}
39+
}
40+
41+
async function main(): Promise<void> {
42+
// *** Simple Lookup ***
43+
let lookup = new LookupUSAutocompletePro("4770 Lincoln");
44+
// uncomment the following line to add a custom parameter
45+
// lookup.addCustomParameter("max_results", "3");
46+
47+
await handleRequest(lookup, "Simple Lookup");
48+
49+
// *** Using Filter and Prefer ***
50+
lookup = new LookupUSAutocompletePro("4770 Lincoln");
51+
52+
lookup.maxResults = 10;
53+
lookup.includeOnlyCities = ["Chicago,La Grange,IL", "Blaine,WA"];
54+
lookup.preferStates = ["IL"];
55+
lookup.preferRatio = 33;
56+
lookup.source = "all";
57+
58+
await handleRequest(lookup, "Using Filter and Prefer");
59+
60+
// *** Using 'selected' to Expand Secondaries ***
61+
lookup = new LookupUSAutocompletePro("4770 Lincoln");
62+
63+
lookup.selected = "4770 N Lincoln Ave Ste 2 (3) Chicago, IL 60625";
64+
65+
await handleRequest(lookup, "Using 'selected' to Expand Secondaries");
66+
}
67+
68+
main();

examples/us_enrichment.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ClientBuilder, BasicAuthCredentials, LookupUSEnrichment } from "smartystreets-javascript-sdk";
2+
3+
// for client-side requests (browser/mobile), use this code:
4+
// import { SharedCredentials } from "smartystreets-javascript-sdk";
5+
// const key: string = process.env.SMARTY_EMBEDDED_KEY!;
6+
// const credentials = new SharedCredentials(key);
7+
8+
// for Server-to-server requests, use this code:
9+
const authId = process.env.SMARTY_AUTH_ID!;
10+
const authToken = process.env.SMARTY_AUTH_TOKEN!;
11+
const credentials = new BasicAuthCredentials(authId, authToken);
12+
13+
// The appropriate license values to be used for your subscriptions
14+
// can be found on the Subscription page of the account dashboard.
15+
// https://www.smarty.com/docs/cloud/licensing
16+
const clientBuilder = new ClientBuilder(credentials);
17+
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
18+
19+
const client = clientBuilder.buildUsEnrichmentClient();
20+
21+
// Documentation for input fields can be found at:
22+
// https://www.smarty.com/docs/us-street-api#input-fields
23+
24+
async function main(): Promise<void> {
25+
const lookup = new LookupUSEnrichment("334968275");
26+
// uncomment the following line to add a custom parameter
27+
// lookup.addCustomParameter("include", "group_financial");
28+
29+
try {
30+
const result = await client.sendPrincipal(lookup);
31+
console.log(result.response);
32+
} catch (err: unknown) {
33+
console.error(err);
34+
}
35+
}
36+
37+
main();

examples/us_extract.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { ClientBuilder, BasicAuthCredentials, LookupUSExtract } from "smartystreets-javascript-sdk";
2+
3+
// for client-side requests (browser/mobile), use this code:
4+
// import { SharedCredentials } from "smartystreets-javascript-sdk";
5+
// const key: string = process.env.SMARTY_EMBEDDED_KEY!;
6+
// const credentials = new SharedCredentials(key);
7+
8+
// for Server-to-server requests, use this code:
9+
const authId = process.env.SMARTY_AUTH_ID!;
10+
const authToken = process.env.SMARTY_AUTH_TOKEN!;
11+
const credentials = new BasicAuthCredentials(authId, authToken);
12+
13+
const clientBuilder = new ClientBuilder(credentials);
14+
// .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API
15+
16+
const client = clientBuilder.buildUsExtractClient();
17+
18+
// Documentation for input fields can be found at:
19+
// https://www.smarty.com/docs/cloud/us-extract-api#http-request-input-fields
20+
21+
function logResult(response: LookupUSExtract): void {
22+
console.log(response.result);
23+
}
24+
25+
async function handleRequest(lookup: LookupUSExtract): Promise<void> {
26+
try {
27+
const response = await client.send(lookup);
28+
logResult(response);
29+
} catch (err: unknown) {
30+
console.error(err);
31+
}
32+
}
33+
34+
async function main(): Promise<void> {
35+
const lookup = new LookupUSExtract(
36+
"If you work at 1600 Pennsylvania Ave NW, Washington DC you're gonna have a hard time.",
37+
);
38+
lookup.aggressive = true;
39+
lookup.addressesHaveLineBreaks = false;
40+
lookup.addressesPerLine = 1;
41+
42+
// uncomment the following line to add a custom parameter
43+
// lookup.addCustomParameter("addr_line_breaks", false);
44+
45+
await handleRequest(lookup);
46+
}
47+
48+
main();

0 commit comments

Comments
 (0)