Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ examples-ts: build
@npx tsx examples/us_reverse_geo.ts || true
@npx tsx examples/us_enrichment.ts || true
@npx tsx examples/us_enrichment_business.ts || true
@npx tsx examples/us_enrichment_business_name_search.ts || true
@npx tsx examples/us_enrichment_etag.ts || true
@npx tsx examples/international_street.ts || true
@npx tsx examples/international_address_autocomplete.ts || true
Expand All @@ -48,6 +49,7 @@ examples-js: build
@node examples/us_reverse_geo.mjs || true
@node examples/us_enrichment.mjs || true
@node examples/us_enrichment_business.mjs || true
@node examples/us_enrichment_business_name_search.mjs || true
@node examples/us_enrichment_etag.mjs || true
@node examples/international_street.mjs || true
@node examples/international_address_autocomplete.mjs || true
Expand Down
35 changes: 35 additions & 0 deletions examples/us_enrichment_business_name_search.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import SmartySDK, { NotModifiedError } from "smartystreets-javascript-sdk";

const SmartyCore = SmartySDK.core;
const { BusinessSummaryLookup, BusinessDetailLookup } = SmartySDK.usEnrichment;

const authId = process.env.SMARTY_AUTH_ID;
const authToken = process.env.SMARTY_AUTH_TOKEN;
const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken);

const client = new SmartyCore.ClientBuilder(credentials).buildUsEnrichmentClient();

async function main() {
const summaryLookup = new BusinessSummaryLookup();
summaryLookup.businessName = "Smarty";
summaryLookup.city = "atlanta";

const summary = await client.sendBusinessSummary(summaryLookup);
const firstResult = summary.results?.[0];
if (!firstResult || firstResult.businesses.length === 0) {
console.log("No businesses found for this business-name search");
return;
}

for (const entry of firstResult.businesses) {
console.log(`${entry.businessId}\t${entry.companyName}`);
}

const firstBusinessId = firstResult.businesses[0].businessId;
const detailLookup = new BusinessDetailLookup(firstBusinessId);
const detail = await client.sendBusinessDetail(detailLookup);
console.log("\n--- Detail for", firstBusinessId, "---");
console.log(detail.result);
}

main().catch((err) => console.error(err));
38 changes: 38 additions & 0 deletions examples/us_enrichment_business_name_search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
ClientBuilder,
BasicAuthCredentials,
BusinessSummaryLookup,
BusinessDetailLookup,
NotModifiedError,
Comment thread
mae-smarty marked this conversation as resolved.
Outdated
} from "smartystreets-javascript-sdk";

const authId = process.env.SMARTY_AUTH_ID!;
const authToken = process.env.SMARTY_AUTH_TOKEN!;
const credentials = new BasicAuthCredentials(authId, authToken);

const client = new ClientBuilder(credentials).buildUsEnrichmentClient();

async function main(): Promise<void> {
const summaryLookup = new BusinessSummaryLookup();
summaryLookup.businessName = "delta air";
summaryLookup.city = "atlanta";

const summary = await client.sendBusinessSummary(summaryLookup);
const firstResult = summary.results?.[0];
if (!firstResult || firstResult.businesses.length === 0) {
console.log("No businesses found for this business-name search");
return;
}

for (const entry of firstResult.businesses) {
console.log(`${entry.businessId}\t${entry.companyName}`);
}

const firstBusinessId = firstResult.businesses[0]!.businessId!;
const detailLookup = new BusinessDetailLookup(firstBusinessId);
const detail = await client.sendBusinessDetail(detailLookup);
console.log("\n--- Detail for", firstBusinessId, "---");
console.log(detail.result);
}

main().catch((err) => console.error(err));
5 changes: 3 additions & 2 deletions src/us_enrichment/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ export default class Client {

sendBusinessSummary(lookup: SummaryLookup): Promise<SummaryLookup> {
if (!lookup) throw new UndefinedLookupError();
if (isBlank(lookup.smartyKey) && isBlank(lookup.street) && isBlank(lookup.freeform)) {
if (isBlank(lookup.smartyKey) && isBlank(lookup.street) && isBlank(lookup.freeform) && isBlank(lookup.businessName)) {
throw new SmartyError(
"Business.Summary lookup requires one of 'smartyKey', 'street', or 'freeform' to be set",
"Business.Summary lookup requires one of 'smartyKey', 'street', 'freeform', or 'business_name' to be set",
);
}

Expand All @@ -88,6 +88,7 @@ export default class Client {
} else {
request.baseUrlParam = "search/business";
if (!isBlank(lookup.freeform)) request.parameters["freeform"] = lookup.freeform!;
if (!isBlank(lookup.businessName)) request.parameters["business_name"] = lookup.businessName!;
if (!isBlank(lookup.street)) request.parameters["street"] = lookup.street!;
if (!isBlank(lookup.city)) request.parameters["city"] = lookup.city!;
if (!isBlank(lookup.state)) request.parameters["state"] = lookup.state!;
Expand Down
1 change: 1 addition & 0 deletions src/us_enrichment/business/SummaryLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SummaryResult from "./SummaryResult.js";
export default class SummaryLookup extends EnrichmentLookupBase {
smartyKey: string | undefined;
freeform: string | undefined;
businessName: string | undefined;
street: string | undefined;
city: string | undefined;
state: string | undefined;
Expand Down
26 changes: 26 additions & 0 deletions tests/us_enrichment/test_BusinessClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ describe("Client.sendBusinessSummary", function () {
});
});

it("targets /search/business with a business_name param", function () {
const mockSender = new MockSender();
const client = new Client(mockSender);
const lookup = new SummaryLookup();
lookup.businessName = "Style Studio";

client.sendBusinessSummary(lookup);

expect(mockSender.request.baseUrlParam).to.equal("search/business");
expect(mockSender.request.parameters).to.deep.equal({
business_name: "Style Studio",
});
});

it("omits the business_name param when businessName is blank", function () {
const mockSender = new MockSender();
const client = new Client(mockSender);
const lookup = new SummaryLookup();
lookup.freeform = "1 Rosedale, Baltimore, Maryland";
lookup.businessName = "";

client.sendBusinessSummary(lookup);

expect(mockSender.request.parameters).to.not.have.property("business_name");
});

it("sends include, exclude, and custom parameters when set", function () {
const mockSender = new MockSender();
const client = new Client(mockSender);
Expand Down
1 change: 1 addition & 0 deletions tests/us_enrichment/test_BusinessLookups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe("Business SummaryLookup", function () {
expect(l.state).to.equal(undefined);
expect(l.zipcode).to.equal(undefined);
expect(l.freeform).to.equal(undefined);
expect(l.businessName).to.equal(undefined);
expect(l.results).to.equal(undefined);
expect(l.customParameters).to.deep.equal({});
});
Expand Down