@voucherify/sdk 2.1.6 → 2.1.7

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @voucherify/sdk
2
2
 
3
+ ## 2.1.7
4
+
5
+ ### Patch Changes
6
+
7
+ - [`92ab4fb`](https://github.com/voucherifyio/voucherify-js-sdk/commit/92ab4fb3577332b60846536204c9d3cc6955b22b) [#188](https://github.com/voucherifyio/voucherify-js-sdk/pull/188) Thanks [@darekg11](https://github.com/darekg11)! - Add explicit properties to VoucherifyError class to make it easier for TypeScript folks. Added examples of proper error handling for both classical Node.JS and for TypeScript. Examples includes different handling for networking and API errors
8
+
9
+ * [`48a46b9`](https://github.com/voucherifyio/voucherify-js-sdk/commit/48a46b9c3c3b64636fa8885919ca9311f2b497e5) [#190](https://github.com/voucherifyio/voucherify-js-sdk/pull/190) Thanks [@darekg11](https://github.com/darekg11)! - Add missing 'related_object_ids', 'related_object_type' and 'related_object_total' properties to 'VoucherifyError' class. Those properties are returned fmor API in case of 'resource_in_ise' error
10
+
3
11
  ## 2.1.6
4
12
 
5
13
  ### Patch Changes
package/README.md CHANGED
@@ -1347,6 +1347,25 @@ You can find TS example in [examples/with-nodejs-typescript](/examples/with-node
1347
1347
 
1348
1348
  Voucherify `error` object always has consistent structure, described in details in our [API reference](https://docs.voucherify.io/reference/errors).
1349
1349
 
1350
+ If you wish to see code examples for server-side error handling, you can check it here:
1351
+ - [Error handling for Node.JS example without TypeScript](../../examples/sdk/with-nodejs/server-error-handling.js)
1352
+ - [Error handling for Node.JS example with TypeScript](../../examples/sdk//with-nodejs-typescript/server-error-handling.ts)
1353
+
1354
+ Structure / typing for error returned from Voucherify API can be seen here:
1355
+ - [VoucherifyError class](src/VoucherifyError.ts)
1356
+
1357
+ If you wish to receive original `AxiosError` when `VoucherifyError` is thrown then you need to explicitly set `exposeErrorCause` property to `true` when creating instance of `VouchierfyClient`, ie:
1358
+
1359
+ ```
1360
+ const voucherify = VoucherifyServerSide({
1361
+ applicationId: 'APPLICATION-ID',
1362
+ secretKey: 'SECRET-KEY',
1363
+ exposeErrorCause: true
1364
+ })
1365
+ ```
1366
+
1367
+ Original `AxiosError` will be available under `cause` property of `VoucherifyError`
1368
+
1350
1369
  # <a name="legacy"></a>👴 Legacy Voucherify JS SDKs
1351
1370
 
1352
1371
  Legacy client-side Voucherify JS SDK is available here: [voucherify.js](https://github.com/rspective/voucherify.js).
@@ -2,6 +2,7 @@ export interface RequestControllerOptions {
2
2
  baseURL: string;
3
3
  basePath: string;
4
4
  headers: Record<string, any>;
5
+ exposeErrorCause: boolean;
5
6
  }
6
7
  /**
7
8
  * @internal
@@ -13,7 +14,8 @@ export declare class RequestController {
13
14
  private request;
14
15
  private lastResponseHeaders;
15
16
  private isLastResponseHeadersSet;
16
- constructor({ basePath, baseURL, headers }: RequestControllerOptions);
17
+ private exposeErrorCause;
18
+ constructor({ basePath, baseURL, headers, exposeErrorCause }: RequestControllerOptions);
17
19
  isLastReponseHeadersSet(): boolean;
18
20
  getLastResponseHeaders(): Record<string, string>;
19
21
  private setLastResponseHeaders;
@@ -72,5 +72,11 @@ export interface VoucherifyClientSideOptions {
72
72
  * ```
73
73
  */
74
74
  customHeaders?: Record<string, string>;
75
+ /**
76
+ * If you wish to include original Axios error in VoucherifyError instance set this to true
77
+ * It can prove to be useful when debugging various scenarios.
78
+ * The original Axios error will be included in cause property of VoucherifyError
79
+ */
80
+ exposeErrorCause?: boolean;
75
81
  }
76
82
  export declare function VoucherifyClientSide(options: VoucherifyClientSideOptions): ClientSide;
@@ -1,6 +1,17 @@
1
+ import { AxiosError } from 'axios';
1
2
  /**
2
3
  * @internal
3
4
  */
4
5
  export declare class VoucherifyError extends Error {
5
- constructor(statusCode: number, body?: unknown);
6
+ code: number;
7
+ key: string;
8
+ details?: string;
9
+ request_id?: string;
10
+ resource_id?: string;
11
+ resource_type?: string;
12
+ related_object_ids?: string[];
13
+ related_object_type?: string;
14
+ related_object_total?: number;
15
+ cause?: AxiosError;
16
+ constructor(statusCode: number, body?: unknown, axiosError?: AxiosError);
6
17
  }
@@ -92,6 +92,12 @@ export interface VoucherifyServerSideOptions {
92
92
  * ```
93
93
  */
94
94
  customHeaders?: Record<string, string>;
95
+ /**
96
+ * If you wish to include original Axios error in VoucherifyError instance set this to true
97
+ * It can prove to be useful when debugging various scenarios.
98
+ * The original Axios error will be included in cause property of VoucherifyError
99
+ */
100
+ exposeErrorCause?: boolean;
95
101
  }
96
102
  export declare function VoucherifyServerSide(options: VoucherifyServerSideOptions): {
97
103
  vouchers: Vouchers;
@@ -14,13 +14,32 @@ var DiscountVouchersTypesEnum;
14
14
  * @internal
15
15
  */
16
16
  class VoucherifyError extends Error {
17
- constructor(statusCode, body) {
17
+ constructor(statusCode, body, axiosError) {
18
18
  var _body, _body2;
19
19
 
20
20
  body = (_body = body) != null ? _body : {};
21
21
  const message = ((_body2 = body) == null ? void 0 : _body2.message) || generateMessage(body, statusCode);
22
22
  super(message);
23
- Object.assign(this, body);
23
+ this.code = void 0;
24
+ this.key = void 0;
25
+ this.details = void 0;
26
+ this.request_id = void 0;
27
+ this.resource_id = void 0;
28
+ this.resource_type = void 0;
29
+ this.related_object_ids = void 0;
30
+ this.related_object_type = void 0;
31
+ this.related_object_total = void 0;
32
+ this.cause = void 0;
33
+ this.code = body.code;
34
+ this.key = body.key;
35
+ this.details = body.details;
36
+ this.request_id = body.request_id;
37
+ this.resource_id = body.resource_id;
38
+ this.resource_type = body.resource_type;
39
+ this.related_object_ids = body.related_object_ids;
40
+ this.related_object_type = body.related_object_type;
41
+ this.related_object_total = body.related_object_total;
42
+ this.cause = axiosError;
24
43
  }
25
44
 
26
45
  }
@@ -38,7 +57,8 @@ class RequestController {
38
57
  constructor({
39
58
  basePath,
40
59
  baseURL,
41
- headers
60
+ headers,
61
+ exposeErrorCause
42
62
  }) {
43
63
  this.baseURL = void 0;
44
64
  this.basePath = void 0;
@@ -46,9 +66,11 @@ class RequestController {
46
66
  this.request = void 0;
47
67
  this.lastResponseHeaders = void 0;
48
68
  this.isLastResponseHeadersSet = void 0;
69
+ this.exposeErrorCause = void 0;
49
70
  this.basePath = basePath;
50
71
  this.baseURL = baseURL;
51
72
  this.headers = headers;
73
+ this.exposeErrorCause = exposeErrorCause;
52
74
  this.lastResponseHeaders = {};
53
75
  this.isLastResponseHeadersSet = false;
54
76
  this.request = axios.create({
@@ -63,7 +85,7 @@ class RequestController {
63
85
  * Handle any HTTP response error (status code outside of 2xx) as a VoucherifyError
64
86
  */
65
87
  if (error != null && (_error$response = error.response) != null && _error$response.status) {
66
- return Promise.reject(new VoucherifyError(error.response.status, error.response.data));
88
+ return Promise.reject(new VoucherifyError(error.response.status, error.response.data, this.exposeErrorCause === true ? error : undefined));
67
89
  }
68
90
 
69
91
  return Promise.reject(error);
@@ -1417,7 +1439,7 @@ class MetadataSchemas {
1417
1439
  // }
1418
1440
 
1419
1441
  function VoucherifyServerSide(options) {
1420
- var _options$apiUrl;
1442
+ var _options$apiUrl, _options$exposeErrorC;
1421
1443
 
1422
1444
  assert(isObject(options), 'VoucherifyServerSide: the "options" argument must be an object');
1423
1445
  assert(isString(options.applicationId), 'VoucherifyServerSide: "options.applicationId" is required');
@@ -1427,7 +1449,7 @@ function VoucherifyServerSide(options) {
1427
1449
  let headers = {
1428
1450
  'X-App-Id': options.applicationId,
1429
1451
  'X-App-Token': options.secretKey,
1430
- 'X-Voucherify-Channel': options.channel || `${environment()}-SDK-v${"2.1.6"}`,
1452
+ 'X-Voucherify-Channel': options.channel || `${environment()}-SDK-v${"2.1.7"}`,
1431
1453
  'Content-Type': 'application/json'
1432
1454
  };
1433
1455
 
@@ -1452,7 +1474,8 @@ function VoucherifyServerSide(options) {
1452
1474
  const client = new RequestController({
1453
1475
  basePath: 'v1',
1454
1476
  baseURL: (_options$apiUrl = options.apiUrl) != null ? _options$apiUrl : 'https://api.voucherify.io',
1455
- headers
1477
+ headers,
1478
+ exposeErrorCause: (_options$exposeErrorC = options.exposeErrorCause) != null ? _options$exposeErrorC : false
1456
1479
  });
1457
1480
  const asyncActions = new AsyncActions(client);
1458
1481
  const balance = new Balance(client);
@@ -1666,7 +1689,7 @@ class ClientSide {
1666
1689
  }
1667
1690
 
1668
1691
  function VoucherifyClientSide(options) {
1669
- var _options$apiUrl;
1692
+ var _options$apiUrl, _options$exposeErrorC;
1670
1693
 
1671
1694
  assert(isObject(options), 'VoucherifyCustomer: expected "options" argument to be an object');
1672
1695
  assert(isString(options.clientApplicationId), 'VoucherifyCustomer: "options.clientApplicationId" is required');
@@ -1676,7 +1699,7 @@ function VoucherifyClientSide(options) {
1676
1699
  let headers = {
1677
1700
  'X-Client-Application-Id': options.clientApplicationId,
1678
1701
  'X-Client-Token': options.clientSecretKey,
1679
- 'X-Voucherify-Channel': `${environment()}-ClientSide-SDK-v${"2.1.6"}`
1702
+ 'X-Voucherify-Channel': `${environment()}-ClientSide-SDK-v${"2.1.7"}`
1680
1703
  };
1681
1704
 
1682
1705
  if (environment().startsWith('Node')) {
@@ -1691,7 +1714,8 @@ function VoucherifyClientSide(options) {
1691
1714
  const client = new RequestController({
1692
1715
  basePath: 'client/v1',
1693
1716
  baseURL: (_options$apiUrl = options.apiUrl) != null ? _options$apiUrl : 'https://api.voucherify.io',
1694
- headers
1717
+ headers,
1718
+ exposeErrorCause: (_options$exposeErrorC = options.exposeErrorCause) != null ? _options$exposeErrorC : false
1695
1719
  });
1696
1720
  return new ClientSide(client, options.trackingId);
1697
1721
  }