smartystreets-javascript-sdk 4.0.1 → 5.0.0
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/Makefile +2 -2
- package/index.js +0 -4
- package/package.json +1 -1
- package/src/ClientBuilder.js +0 -6
- package/src/HttpSender.js +1 -1
- package/src/util/buildClients.js +0 -5
- package/src/us_autocomplete/Client.js +0 -56
- package/src/us_autocomplete/Lookup.js +0 -24
- package/src/us_autocomplete/Suggestion.js +0 -13
- package/tests/us_autocomplete/test_Client.js +0 -105
- package/tests/us_autocomplete/test_Lookup.js +0 -11
- package/tests/us_autocomplete/test_Suggestion.js +0 -20
package/Makefile
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/make -f
|
|
2
2
|
|
|
3
|
-
VERSION := $(shell tagit -
|
|
3
|
+
VERSION := $(shell tagit -M --dry-run)
|
|
4
4
|
VERSION_FILE1 := package.json
|
|
5
5
|
VERSION_FILE2 := package-lock.json
|
|
6
6
|
|
|
@@ -11,7 +11,7 @@ node_modules:
|
|
|
11
11
|
npm install
|
|
12
12
|
|
|
13
13
|
publish: test version upload unversion
|
|
14
|
-
tagit -
|
|
14
|
+
tagit -M
|
|
15
15
|
git push origin --tags
|
|
16
16
|
|
|
17
17
|
upload:
|
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
package/src/ClientBuilder.js
CHANGED
|
@@ -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/HttpSender.js
CHANGED
package/src/util/buildClients.js
CHANGED
|
@@ -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,
|
|
@@ -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
|
-
});
|