smartystreets-javascript-sdk 4.0.2 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -15,10 +15,6 @@ module.exports = {
15
15
  Lookup: require("./src/us_zipcode/Lookup"),
16
16
  Result: require("./src/us_zipcode/Result"),
17
17
  },
18
- usAutocomplete: {
19
- Lookup: require("./src/us_autocomplete/Lookup"),
20
- Suggestion: require("./src/us_autocomplete/Suggestion"),
21
- },
22
18
  usAutocompletePro: {
23
19
  Lookup: require("./src/us_autocomplete_pro/Lookup"),
24
20
  Suggestion: require("./src/us_autocomplete_pro/Suggestion"),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smartystreets-javascript-sdk",
3
- "version": "4.0.2",
3
+ "version": "5.0.1",
4
4
  "description": "Quick and easy Smarty address validation.",
5
5
  "keywords": [
6
6
  "smarty",
@@ -14,7 +14,6 @@ const Sleeper = require("./util/Sleeper.js");
14
14
  //TODO: refactor this to work more cleanly with a bundler.
15
15
  const UsStreetClient = require("./us_street/Client");
16
16
  const UsZipcodeClient = require("./us_zipcode/Client");
17
- const UsAutocompleteClient = require("./us_autocomplete/Client");
18
17
  const UsAutocompleteProClient = require("./us_autocomplete_pro/Client");
19
18
  const UsExtractClient = require("./us_extract/Client");
20
19
  const InternationalStreetClient = require("./international_street/Client");
@@ -22,7 +21,6 @@ const UsReverseGeoClient = require("./us_reverse_geo/Client");
22
21
  const InternationalAddressAutocompleteClient = require("./international_address_autocomplete/Client");
23
22
 
24
23
  const INTERNATIONAL_STREET_API_URI = "https://international-street.api.smarty.com/verify";
25
- const US_AUTOCOMPLETE_API_URL = "https://us-autocomplete.api.smarty.com/suggest";
26
24
  const US_AUTOCOMPLETE_PRO_API_URL = "https://us-autocomplete-pro.api.smarty.com/lookup";
27
25
  const US_EXTRACT_API_URL = "https://us-extract.api.smarty.com/";
28
26
  const US_STREET_API_URL = "https://us-street.api.smarty.com/street-address";
@@ -184,10 +182,6 @@ class ClientBuilder {
184
182
  return this.buildClient(US_ZIP_CODE_API_URL, UsZipcodeClient);
185
183
  }
186
184
 
187
- buildUsAutocompleteClient() { // Deprecated
188
- return this.buildClient(US_AUTOCOMPLETE_API_URL, UsAutocompleteClient);
189
- }
190
-
191
185
  buildUsAutocompleteProClient() {
192
186
  return this.buildClient(US_AUTOCOMPLETE_PRO_API_URL, UsAutocompleteProClient);
193
187
  }
package/src/Request.js CHANGED
@@ -1,11 +1,9 @@
1
1
  class Request {
2
- constructor(payload) {
2
+ constructor(payload, headers = {"Content-Type": "application/json; charset=utf-8"}) {
3
3
  this.baseUrl = "";
4
4
  this.baseUrlParam = "";
5
5
  this.payload = payload;
6
- this.headers = {
7
- "Content-Type": "application/json; charset=utf-8",
8
- };
6
+ this.headers = headers;
9
7
 
10
8
  this.parameters = {};
11
9
  }
@@ -16,7 +16,7 @@ class Client {
16
16
  send(lookup) {
17
17
  if (typeof lookup === "undefined") throw new Errors.UndefinedLookupError();
18
18
 
19
- let request = new Request(lookup.text);
19
+ let request = new Request(lookup.text, {"Content-Type": "text/plain; charset=utf-8"});
20
20
  request.parameters = buildInputData(lookup, keyTranslationFormat);
21
21
 
22
22
  return new Promise((resolve, reject) => {
@@ -8,10 +8,6 @@ function buildUsStreetApiClient(credentials) {
8
8
  return instantiateClientBuilder(credentials).buildUsStreetApiClient();
9
9
  }
10
10
 
11
- function buildUsAutocompleteApiClient(credentials) {
12
- return instantiateClientBuilder(credentials).buildUsAutocompleteClient();
13
- }
14
-
15
11
  function buildUsAutocompleteProApiClient(credentials) {
16
12
  return instantiateClientBuilder(credentials).buildUsAutocompleteProClient();
17
13
  }
@@ -38,7 +34,6 @@ function buildInternationalAddressAutocompleteApiClient(credentials) {
38
34
 
39
35
  module.exports = {
40
36
  usStreet: buildUsStreetApiClient,
41
- usAutocomplete: buildUsAutocompleteApiClient,
42
37
  usAutocompletePro: buildUsAutocompleteProApiClient,
43
38
  usExtract: buildUsExtractApiClient,
44
39
  usZipcode: buildUsZipcodeApiClient,
@@ -0,0 +1,167 @@
1
+ const chai = require("chai");
2
+ const expect = chai.expect;
3
+ const SmartySDK = require("../index");
4
+
5
+ describe("Extract example test", () => {
6
+ const authId = "";
7
+ const authToken = "";
8
+ const SmartyCore = SmartySDK.core;
9
+ const credentials = new SmartyCore.StaticCredentials(authId, authToken);
10
+ const clientBuilder = new SmartyCore.ClientBuilder(credentials);
11
+ clientBuilder.withBaseUrl("https://us-extract.api.hobbes.smartyops.net");
12
+
13
+ const usExtractClient = clientBuilder.buildUsExtractClient();
14
+
15
+ it("Check multi-line test", async () => {
16
+ if (!(authId && authToken)) {
17
+ expect("bypass").to.equal("bypass");
18
+ } else {
19
+ const lookup = new SmartySDK.usExtract.Lookup(`
20
+ 1476 Sandhill Rd, Orem, UT asdlfkjasldfkja sldfj 350 E University Pkwy, Orem, UT 84058 asldfkjasldfj asldkfjasldfj
21
+ 417 W 1300 S, Orem, UT asdlfkjasldfkjal skdjf alskdjf 309 E University Pkwy, Orem, UT 84058
22
+ `);
23
+
24
+ lookup.addressesHaveLineBreaks = true;
25
+ lookup.addressesPerLine = 1;
26
+
27
+ const result = await usExtractClient.send(lookup);
28
+
29
+ expect(result.result.meta.addressCount).to.equal(2);
30
+ expect(result.result.meta.verifiedCount).to.equal(2);
31
+
32
+ }
33
+ });
34
+
35
+ it("Check HTML test", async () => {
36
+ if (!(authId && authToken)) {
37
+ expect("bypass").to.equal("bypass");
38
+ } else {
39
+ const lookup = new SmartySDK.usExtract.Lookup(`
40
+ HTML is automatically detected and, if found, ignored. You can override this behavior with API calls by manually setting the 'html' parameter to true or false.
41
+
42
+ For input with <b>HTML code</code> like this, addresses will be scraped from outside the tags. The HTML should be properly formatted, valid HTML.<br><br>
43
+
44
+ 7584<span class='street-name'>Big Canyon</span> Anaheim Hills, CA <span class='zip-code'>92808</span><br><br>
45
+
46
+ You can force HTML mode if auto-detect doesn't work properly.
47
+ `);
48
+
49
+ const result = await usExtractClient.send(lookup);
50
+
51
+ expect(result.result.meta.addressCount).to.equal(1);
52
+ }
53
+ });
54
+
55
+ it("Check addresses with line breaks", async () => {
56
+ if (!(authId && authToken)) {
57
+ expect("bypass").to.equal("bypass");
58
+ } else {
59
+ const lookup = new SmartySDK.usExtract.Lookup(`
60
+ This address is valid:
61
+
62
+ 1109 Ninth 85007
63
+
64
+ but this one is not:
65
+
66
+ 3777 Las Vegas Blvd
67
+ Las Vegas, Nevada
68
+
69
+ However, this nearby location is valid, despite the poor spelling:
70
+
71
+ 3785 Las Vegs Av.
72
+ Los Vegas, Nevada
73
+ `);
74
+
75
+ lookup.addressesHaveLineBreaks = true;
76
+
77
+ const result = await usExtractClient.send(lookup);
78
+
79
+ expect(result.result.meta.addressCount).to.equal(3);
80
+ expect(result.result.meta.verifiedCount).to.equal(2);
81
+ }
82
+ });
83
+
84
+ it("Check addresses needing aggressive mode", async () => {
85
+ if (!(authId && authToken)) {
86
+ expect("bypass").to.equal("bypass");
87
+ } else {
88
+ const lookup = new SmartySDK.usExtract.Lookup(`
89
+ With aggressive mode on, popular US cities may be matched if no state or ZIP code can be found.
90
+
91
+ This means addresses like 5455 North 250 West, Provo, can be found even though it doesn't have anything looking like a state or ZIP code following it.
92
+ `);
93
+
94
+ lookup.aggressive = true;
95
+
96
+ const result = await usExtractClient.send(lookup);
97
+
98
+ expect(result.result.meta.addressCount).to.equal(1);
99
+ expect(result.result.meta.verifiedCount).to.equal(1);
100
+ }
101
+ });
102
+
103
+ it("Check addresses in URLs", async () => {
104
+ if (!(authId && authToken)) {
105
+ expect("bypass").to.equal("bypass");
106
+ } else {
107
+ const lookup = new SmartySDK.usExtract.Lookup(`
108
+ Smarty can handle addresses in URLs, as long as there are no spaces in them or they are surrounded by quotes.
109
+
110
+ Address in one parameter: https://maps.google.com/?q=1+Rosedale+St+Baltimore+MD&ie=UTF8
111
+ or,
112
+ same URL with spaces surrounded by quotes: "https://maps.google.com/?q=1 Rosedale St Baltimore MD&ie=UTF8"
113
+ ...
114
+
115
+ Address across multiple parameters: https://example.com/?street=4004%20Grant%20St%20&state=WA&zipcode=98660&city=Vancouver
116
+ We can find them either way.
117
+ `);
118
+
119
+ const result = await usExtractClient.send(lookup);
120
+
121
+ expect(result.result.meta.addressCount).to.equal(3);
122
+ expect(result.result.meta.verifiedCount).to.equal(3);
123
+ }
124
+ });
125
+
126
+ it("Check address with unicode character", async () => {
127
+ if (!(authId && authToken)) {
128
+ expect("bypass").to.equal("bypass");
129
+ } else {
130
+ const lookup = new SmartySDK.usExtract.Lookup(`
131
+ This address has a Unicode character in it (the special í in María): 123 María Lane Tempe, AZ, 85284
132
+
133
+ Addresses with Unicode can still be found and verified.
134
+ `);
135
+
136
+ const result = await usExtractClient.send(lookup);
137
+
138
+ expect(result.result.meta.addressCount).to.equal(1);
139
+ expect(result.result.meta.verifiedCount).to.equal(1);
140
+ }
141
+ });
142
+
143
+ it("Check more addresses needing aggressive mode", async () => {
144
+ if (!(authId && authToken)) {
145
+ expect("bypass").to.equal("bypass");
146
+ } else {
147
+ const lookup = new SmartySDK.usExtract.Lookup(`
148
+ Try this one with and without aggressive mode. The '10379' looks like a ZIP code but is actually a primary number:
149
+
150
+ 8465 Park Meadows Center Drive
151
+ Lone Tree, CO
152
+ 303-799-3400
153
+
154
+ 10379 South State Street
155
+ Sandy, UT 84070
156
+ 801-432-5100
157
+
158
+ This input has two addresses and only one is found without aggressive mode.
159
+ `);
160
+
161
+ const result = await usExtractClient.send(lookup);
162
+
163
+ expect(result.result.meta.addressCount).to.equal(1);
164
+ expect(result.result.meta.verifiedCount).to.equal(1);
165
+ }
166
+ });
167
+ });
@@ -1,56 +0,0 @@
1
- const Errors = require("../Errors");
2
- const Request = require("../Request");
3
- const Suggestion = require("./Suggestion");
4
-
5
- /**
6
- * This client sends lookups to the Smarty US Autocomplete API, <br>
7
- * and attaches the results to the appropriate Lookup objects.
8
- */
9
- class Client {
10
- constructor(sender) {
11
- this.sender = sender;
12
- }
13
-
14
- send(lookup) {
15
- if (typeof lookup === "undefined") throw new Errors.UndefinedLookupError();
16
-
17
- let request = new Request();
18
- request.parameters = buildRequestParameters(lookup);
19
-
20
- return new Promise((resolve, reject) => {
21
- this.sender.send(request)
22
- .then(response => {
23
- if (response.error) reject(response.error);
24
-
25
- lookup.result = buildSuggestionsFromResponse(response.payload);
26
- resolve(lookup);
27
- })
28
- .catch(reject);
29
- });
30
-
31
- function buildRequestParameters(lookup) {
32
- return {
33
- prefix: lookup.prefix,
34
- suggestions: lookup.maxSuggestions,
35
- city_filter: joinFieldWith(lookup.cityFilter, ","),
36
- state_filter: joinFieldWith(lookup.stateFilter, ","),
37
- prefer: joinFieldWith(lookup.prefer, ";"),
38
- prefer_ratio: lookup.preferRatio,
39
- geolocate: lookup.geolocate,
40
- geolocate_precision: lookup.geolocatePrecision,
41
- };
42
-
43
- function joinFieldWith(field, delimiter) {
44
- if (field.length) return field.join(delimiter);
45
- }
46
- }
47
-
48
- function buildSuggestionsFromResponse(payload) {
49
- if (payload.suggestions === null) return [];
50
-
51
- return payload.suggestions.map(suggestion => new Suggestion(suggestion));
52
- }
53
- }
54
- }
55
-
56
- module.exports = Client;
@@ -1,24 +0,0 @@
1
- /**
2
- * In addition to holding all of the input data for this lookup, this class also<br>
3
- * will contain the result of the lookup after it comes back from the API.
4
- * @see "https://www.smarty.com/docs/cloud/us-autocomplete-api#http-request-input-fields"
5
- */
6
- class Lookup {
7
- /**
8
- * @param prefix The beginning of an address. This is required to be set.
9
- */
10
- constructor(prefix) {
11
- this.result = [];
12
-
13
- this.prefix = prefix;
14
- this.maxSuggestions = undefined;
15
- this.cityFilter = [];
16
- this.stateFilter = [];
17
- this.prefer = [];
18
- this.preferRatio = undefined;
19
- this.geolocate = undefined;
20
- this.geolocatePrecision = undefined;
21
- }
22
- }
23
-
24
- module.exports = Lookup;
@@ -1,13 +0,0 @@
1
- /**
2
- * @see "https://www.smarty.com/docs/cloud/us-autocomplete-api#http-response"
3
- */
4
- class Suggestion {
5
- constructor(responseData) {
6
- this.text = responseData.text;
7
- this.streetLine = responseData.street_line;
8
- this.city = responseData.city;
9
- this.state = responseData.state;
10
- }
11
- }
12
-
13
- module.exports = Suggestion;
@@ -1,105 +0,0 @@
1
- const chai = require("chai");
2
- const expect = chai.expect;
3
- const Client = require("../../src/us_autocomplete/Client");
4
- const Lookup = require("../../src/us_autocomplete/Lookup");
5
- const Suggestion = require("../../src/us_autocomplete/Suggestion");
6
- const errors = require("../../src/Errors");
7
- const MockSender = require("../fixtures/mock_senders").MockSender;
8
- const MockSenderWithResponse = require("../fixtures/mock_senders").MockSenderWithResponse;
9
-
10
- describe("A US Autocomplete Client", function () {
11
- it("correctly builds parameters for a prefix only lookup.", function () {
12
- let mockSender = new MockSender();
13
- let client = new Client(mockSender);
14
- let prefix = '(>")>#';
15
- let lookup = new Lookup(prefix);
16
- let expectedParameters = {
17
- prefix: prefix,
18
- suggestions: undefined,
19
- city_filter: undefined,
20
- state_filter: undefined,
21
- prefer: undefined,
22
- prefer_ratio: undefined,
23
- geolocate: undefined,
24
- geolocate_precision: undefined,
25
- };
26
-
27
- client.send(lookup);
28
-
29
- expect(mockSender.request.parameters).to.deep.equal(expectedParameters);
30
- });
31
-
32
- it("correctly builds parameters for a fully-populated lookup.", function () {
33
- let mockSender = new MockSender();
34
- let client = new Client(mockSender);
35
- let lookup = new Lookup();
36
- lookup.prefix = "1";
37
- lookup.maxSuggestions = "2";
38
- lookup.cityFilter.push("a");
39
- lookup.cityFilter.push("b");
40
- lookup.stateFilter = ["c", "d"];
41
- lookup.prefer = ["e", "f", "g"];
42
- lookup.preferRatio = "5";
43
- lookup.geolocate = "6";
44
- lookup.geolocatePrecision = "7";
45
-
46
- let expectedParameters = {
47
- prefix: "1",
48
- suggestions: "2",
49
- city_filter: "a,b",
50
- state_filter: "c,d",
51
- prefer: "e;f;g",
52
- prefer_ratio: "5",
53
- geolocate: "6",
54
- geolocate_precision: "7",
55
- };
56
-
57
- client.send(lookup);
58
- expect(mockSender.request.parameters).to.deep.equal(expectedParameters);
59
- });
60
-
61
- it("throws an error if sending without a lookup.", function () {
62
- let mockSender = new MockSender();
63
- let client = new Client(mockSender);
64
- expect(client.send).to.throw(errors.UndefinedLookupError);
65
- });
66
-
67
- it("rejects with an exception if the response comes back with an error.", function () {
68
- let expectedError = new Error("I'm the error.");
69
- let mockSender = new MockSenderWithResponse("", expectedError);
70
- let client = new Client(mockSender);
71
- let lookup = new Lookup("¯\\_(ツ)_/¯");
72
-
73
- return client.send(lookup).catch((e) => {expect(e).to.equal(expectedError);});
74
- });
75
-
76
- it("returns an empty array when no suggestions are returned.", () => {
77
- let mockExpectedPayload = {suggestions: null};
78
- let mockSender = new MockSenderWithResponse(mockExpectedPayload);
79
- let client = new Client(mockSender);
80
- let lookup = new Lookup("Please let this be easy to test.");
81
- let expectedSuggestion = [];
82
-
83
- return client.send(lookup).then(response => {
84
- expect(lookup.result).to.deep.equal(expectedSuggestion);
85
- });
86
- });
87
-
88
- it("attaches suggestions from a response to a lookup.", function () {
89
- const responseData = {
90
- text: "a",
91
- street_line: "b",
92
- city: "c",
93
- state: "d",
94
- };
95
- let mockExpectedPayload = {suggestions: [responseData]};
96
- let mockSender = new MockSenderWithResponse(mockExpectedPayload);
97
- let client = new Client(mockSender);
98
- let lookup = new Lookup("Trevor the Vampire");
99
- let expectedSuggestion = new Suggestion(responseData);
100
-
101
- return client.send(lookup).then(response => {
102
- expect(lookup.result[0]).to.deep.equal(expectedSuggestion);
103
- });
104
- })
105
- });
@@ -1,11 +0,0 @@
1
- const chai = require("chai");
2
- const expect = chai.expect;
3
- const Lookup = require("../../src/us_autocomplete/Lookup");
4
-
5
- describe("A US Autocomplete Lookup", function () {
6
- it("can be newed up with a prefix.", function () {
7
- const expectedPrefix = "a";
8
- let lookup = new Lookup(expectedPrefix);
9
- expect(lookup.prefix).to.equal(expectedPrefix);
10
- });
11
- });
@@ -1,20 +0,0 @@
1
- const chai = require("chai");
2
- const expect = chai.expect;
3
- const Suggestion = require("../../src/us_autocomplete/Suggestion");
4
-
5
- describe("A US Autocomplete Suggestion", function () {
6
- it("is initialized correctly with API response data.", function () {
7
- const mockSuggestion = {
8
- text: "a",
9
- street_line: "b",
10
- city: "c",
11
- state: "d"
12
- };
13
- let suggestion = new Suggestion(mockSuggestion);
14
-
15
- expect(suggestion.text).to.equal("a");
16
- expect(suggestion.streetLine).to.equal("b");
17
- expect(suggestion.city).to.equal("c");
18
- expect(suggestion.state).to.equal("d");
19
- });
20
- });