smartystreets-javascript-sdk 2.2.1 → 3.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/make -f
2
2
 
3
- VERSION := $(shell tagit -p --dry-run)
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 -p
14
+ tagit -m
15
15
  git push origin --tags
16
16
 
17
17
  upload:
@@ -3,13 +3,13 @@ const SmartyCore = SmartySDK.core;
3
3
  const Lookup = SmartySDK.usAutocompletePro.Lookup;
4
4
 
5
5
  // for Server-to-server requests, use this code:
6
- let authId = "f0fd813a-2c37-2afe-a322-1a46794d6c91";
7
- let authToken = "sPDMxTLaMh8BCp6YWSNW";
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
9
 
10
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);
11
+ let key = process.env.SMARTY_EMBEDDED_KEY;
12
+ const credentials = new SmartyCore.SharedCredentials(key);
13
13
 
14
14
  // The appropriate license values to be used for your subscriptions
15
15
  // can be found on the Subscription page of the account dashboard.
@@ -23,11 +23,26 @@ let client = clientBuilder.buildUsAutocompleteProClient();
23
23
 
24
24
  // *** Simple Lookup ***
25
25
  let lookup = new Lookup("4770 Lincoln");
26
- lookup.excludeStates = ["CA"];
27
26
 
28
- (async () => {
29
- await handleRequest(lookup, "Simple Lookup");
30
- })()
27
+ await handleRequest(lookup, "Simple Lookup");
28
+
29
+ // *** Using Filter and Prefer ***
30
+ lookup = new Lookup("4770 Lincoln");
31
+
32
+ lookup.maxResults = 10;
33
+ lookup.includeOnlyCities = ["Chicago,La Grange,IL", "Blaine,WA"];
34
+ lookup.preferStates = ["IL"];
35
+ lookup.preferRatio = 33;
36
+ lookup.source = "all";
37
+
38
+ await handleRequest(lookup, "Using Filter and Prefer");
39
+
40
+ // *** Using 'selected' to Expand Secondaries ***
41
+ lookup = new Lookup("4770 Lincoln");
42
+
43
+ lookup.selected = "4770 N Lincoln Ave Ste 2 (3) Chicago, IL 60625";
44
+
45
+ await handleRequest(lookup, "Using 'selected' to Expand Secondaries")
31
46
 
32
47
  // ************************************************
33
48
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smartystreets-javascript-sdk",
3
- "version": "2.2.1",
3
+ "version": "3.1.0",
4
4
  "description": "Quick and easy Smarty address validation.",
5
5
  "keywords": [
6
6
  "smarty",
package/src/Errors.js CHANGED
@@ -1,7 +1,14 @@
1
1
  class SmartyError extends Error {
2
+ constructor(message = "unexpected error") {
3
+ super(message);
4
+ }
5
+ }
6
+
7
+ class DefaultError extends SmartyError {
2
8
  constructor(message) {
3
9
  super(message);
4
10
  }
11
+
5
12
  }
6
13
 
7
14
  class BatchFullError extends SmartyError {
@@ -88,5 +95,6 @@ module.exports = {
88
95
  TooManyRequestsError: TooManyRequestsError,
89
96
  InternalServerError: InternalServerError,
90
97
  ServiceUnavailableError: ServiceUnavailableError,
91
- GatewayTimeoutError: GatewayTimeoutError
98
+ GatewayTimeoutError: GatewayTimeoutError,
99
+ DefaultError: DefaultError
92
100
  };
@@ -11,30 +11,6 @@ class StatusCodeSender {
11
11
  .then(resolve)
12
12
  .catch(error => {
13
13
  switch (error.statusCode) {
14
- case 400:
15
- error.error = new Errors.BadRequestError();
16
- break;
17
-
18
- case 401:
19
- error.error = new Errors.BadCredentialsError();
20
- break;
21
-
22
- case 402:
23
- error.error = new Errors.PaymentRequiredError();
24
- break;
25
-
26
- case 413:
27
- error.error = new Errors.RequestEntityTooLargeError();
28
- break;
29
-
30
- case 422:
31
- error.error = new Errors.UnprocessableEntityError("GET request lacked required fields.");
32
- break;
33
-
34
- case 429:
35
- error.error = new Errors.TooManyRequestsError();
36
- break;
37
-
38
14
  case 500:
39
15
  error.error = new Errors.InternalServerError();
40
16
  break;
@@ -46,8 +22,10 @@ class StatusCodeSender {
46
22
  case 504:
47
23
  error.error = new Errors.GatewayTimeoutError();
48
24
  break;
49
- }
50
25
 
26
+ default:
27
+ error.error = new Errors.DefaultError(error && error.payload && error.payload.errors[0] && error.payload.errors[0].message);
28
+ }
51
29
  reject(error);
52
30
  });
53
31
  });
@@ -14,6 +14,7 @@ class Candidate {
14
14
  this.deliveryLine2 = responseData.delivery_line_2;
15
15
  this.lastLine = responseData.last_line;
16
16
  this.deliveryPointBarcode = responseData.delivery_point_barcode;
17
+ this.smartyKey = responseData.smarty_key;
17
18
 
18
19
  this.components = {};
19
20
  if (responseData.components !== undefined) {
@@ -23,35 +23,24 @@ describe("A status code sender", function () {
23
23
  });
24
24
  });
25
25
 
26
- it("gives a Bad Credentials error on a 401.", function () {
27
- return expectedErrorForStatusCode(errors.BadCredentialsError, 401);
28
- });
29
-
30
- it("gives a Payment Required error on a 402.", function () {
31
- return expectedErrorForStatusCode(errors.PaymentRequiredError, 402);
32
- });
33
-
34
- it("gives a Request Entity Too Large error on a 413.", function () {
35
- return expectedErrorForStatusCode(errors.RequestEntityTooLargeError, 413);
36
- });
37
-
38
- it("gives a Bad Request error on a 400.", function () {
39
- return expectedErrorForStatusCode(errors.BadRequestError, 400);
40
- });
41
-
42
- it("gives an Unprocessable Entity error on a 422.", function () {
43
- return expectedErrorForStatusCode(errors.UnprocessableEntityError, 422);
44
- });
26
+ it("gives a custom message for 400", function () {
27
+ const payload = {
28
+ errors: [
29
+ {message: "custom message"}
30
+ ]
31
+ };
32
+ return expectedErrorWithPayloadMessage(400, payload);
33
+ })
45
34
 
46
- it("gives a Too Many Requests error on a 429.", function () {
47
- return expectedErrorForStatusCode(errors.TooManyRequestsError, 429);
48
- });
35
+ it("returns an error message if payload is undefined", function () {
36
+ return expectedDefaultError()
37
+ })
49
38
 
50
- it("gives an Internal Server Error error on a 500.", function () {
39
+ it("gives an Internal Server Error on a 500.", function () {
51
40
  return expectedErrorForStatusCode(errors.InternalServerError, 500);
52
41
  });
53
42
 
54
- it("gives an Service Unvailable error on a 503.", function () {
43
+ it("gives an Service Unavailable error on a 503.", function () {
55
44
  return expectedErrorForStatusCode(errors.ServiceUnavailableError, 503);
56
45
  });
57
46
 
@@ -60,22 +49,46 @@ describe("A status code sender", function () {
60
49
  });
61
50
  });
62
51
 
52
+ const expectedErrorWithPayloadMessage = (errorCode, payload) => {
53
+ let mockSender = generateMockSender(errorCode, payload);
54
+ let statusCodeSender = new StatusCodeSender(mockSender);
55
+ let request = new Request();
56
+
57
+ return statusCodeSender.send(request).then(() => {
58
+ }, error => {
59
+ expect(error.error).to.be.an.instanceOf(errors.DefaultError);
60
+ expect(error.error.message).to.be.equal(payload.errors[0].message);
61
+ })
62
+ }
63
+
64
+ const expectedDefaultError = () => {
65
+ let mockSender = generateMockSender(400);
66
+ let statusCodeSender = new StatusCodeSender(mockSender);
67
+ let request = new Request();
68
+
69
+ return statusCodeSender.send(request).then(() => {
70
+ }, error => {
71
+ expect(error.error).to.be.an.instanceOf(errors.DefaultError);
72
+ expect(error.error.message).to.be.equal("unexpected error");
73
+ })
74
+ }
75
+
63
76
  function expectedErrorForStatusCode(expectedError, errorCode) {
64
77
  let mockSender = generateMockSender(errorCode);
65
78
  let statusCodeSender = new StatusCodeSender(mockSender);
66
79
  let request = new Request();
67
80
 
68
- return statusCodeSender.send(request).then(response => {
81
+ return statusCodeSender.send(request).then(() => {
69
82
  }, error => {
70
83
  expect(error.error).to.be.an.instanceOf(expectedError);
71
84
  })
72
85
  }
73
86
 
74
- function generateMockSender(errorCode) {
87
+ function generateMockSender(errorCode, payload) {
75
88
  return {
76
89
  send: () => {
77
90
  return new Promise((resolve, reject) => {
78
- reject(new Response(errorCode))
91
+ reject(new Response(errorCode, payload))
79
92
  });
80
93
  }
81
94
  };
@@ -12,6 +12,7 @@ describe("A match candidate", function () {
12
12
  delivery_line_2: "4",
13
13
  last_line: "5",
14
14
  delivery_point_barcode: "6",
15
+ smarty_key: "0000",
15
16
  components: {
16
17
  urbanization: "7",
17
18
  primary_number: "8",
@@ -76,6 +77,7 @@ describe("A match candidate", function () {
76
77
  expect(candidate.deliveryLine2).to.equal('4');
77
78
  expect(candidate.lastLine).to.equal('5');
78
79
  expect(candidate.deliveryPointBarcode).to.equal('6');
80
+ expect(candidate.smartyKey).to.equal('0000');
79
81
 
80
82
  expect(candidate.components.urbanization).to.equal('7');
81
83
  expect(candidate.components.primaryNumber).to.equal('8');