@rebuy/rebuy 1.2.0 → 1.3.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.
@@ -1,2 +1,2 @@
1
- 0000000000000000000000000000000000000000 28657bddf50c93f95e089397c1b7e8b745ec9ec3 Mike Reid <mike.reid@rebuyengine.com> 1658872537 -0600 branch: Created from HEAD
2
- 28657bddf50c93f95e089397c1b7e8b745ec9ec3 d1073d6c613eeb3e53fa6de416fa507979a5b905 Mike Reid <mike.reid@rebuyengine.com> 1658872546 -0600 commit: New: Add additional named exports
1
+ 0000000000000000000000000000000000000000 145cb0c967aef90450c887bbaf2a9bf61a00577d Mike Reid <mike.reid@rebuyengine.com> 1658876468 -0600 branch: Created from HEAD
2
+ 145cb0c967aef90450c887bbaf2a9bf61a00577d f5fedc7d3aea2600d58bfde5527d49f2b99d5abc Mike Reid <mike.reid@rebuyengine.com> 1658876497 -0600 commit: Fix: Include both named + default exports (for BC)
@@ -1 +1 @@
1
- d1073d6c613eeb3e53fa6de416fa507979a5b905
1
+ f5fedc7d3aea2600d58bfde5527d49f2b99d5abc
package/api.js CHANGED
@@ -7,9 +7,10 @@ const config = {
7
7
  eventDomain: 'https://rebuyengine.com',
8
8
  };
9
9
 
10
- const makeCall = async (method, path, data, origin) => {
10
+ const makeCall = async (method, path, data, origin, options = {}) => {
11
11
  const url = `${origin}${path}`;
12
12
  const requestUrl = new URL(url);
13
+ const fetchOptions = options?.fetch ?? {};
13
14
 
14
15
  const requestData = {
15
16
  key: config.key,
@@ -21,6 +22,7 @@ const makeCall = async (method, path, data, origin) => {
21
22
 
22
23
  const requestObject = {
23
24
  method,
25
+ ...fetchOptions,
24
26
  };
25
27
 
26
28
  if (method == 'GET') {
@@ -33,7 +35,14 @@ const makeCall = async (method, path, data, origin) => {
33
35
  }
34
36
 
35
37
  const request = await fetch(requestUrl, requestObject);
36
- return await request.json();
38
+ const response = await request.json();
39
+
40
+ if (!request.ok && fetchOptions.strictErrors === true) {
41
+ const message = `An error has occurred: ${request.status}`;
42
+ throw new Error(message, { cause: response });
43
+ }
44
+
45
+ return response;
37
46
  };
38
47
 
39
48
  export class Api {
@@ -45,17 +54,17 @@ export class Api {
45
54
  }
46
55
  }
47
56
 
48
- async callEvent(method, path, data) {
49
- return await makeCall(method, path, data, config.eventDomain);
57
+ async callEvent(method, path, data, options = {}) {
58
+ return await makeCall(method, path, data, config.eventDomain, options);
50
59
  }
51
60
 
52
- async callCdn(method, path, data) {
53
- return await makeCall(method, path, data, config.cdnDomain);
61
+ async callCdn(method, path, data, options = {}) {
62
+ return await makeCall(method, path, data, config.cdnDomain, options);
54
63
  }
55
64
 
56
- async callApi(method, path, data) {
57
- return await makeCall(method, path, data, config.domain);
65
+ async callApi(method, path, data, options = {}) {
66
+ return await makeCall(method, path, data, config.domain, options);
58
67
  }
59
68
  }
60
69
 
61
- export default { Api };
70
+ export default Api;
package/client.js CHANGED
@@ -18,7 +18,11 @@ const trackEvent = async (eventData) => {
18
18
  return await config.api.callEvent('POST', '/api/v1/analytics/event', eventData);
19
19
  };
20
20
 
21
- const makeCall = async (endpoint, params, format) => {
21
+ const makeCDNCall = async (endpoint, params, format, options = {}) => {
22
+ return await makeCall(endpoint, params, format, { ...options, cdn: true });
23
+ };
24
+
25
+ const makeCall = async (endpoint, params, format, options = {}) => {
22
26
  const query = {};
23
27
 
24
28
  if (config.defaultParameters != null) {
@@ -33,11 +37,18 @@ const makeCall = async (endpoint, params, format) => {
33
37
  Object.assign(query, params);
34
38
  }
35
39
 
40
+ if (typeof options != 'object' || options == null) {
41
+ console.warn('Unsupported fetch options provided.', options);
42
+ options = {};
43
+ }
44
+
36
45
  if (config.identity && config.identity.visitorId()) {
37
46
  query.uuid = config.identity.visitorId();
38
47
  }
39
48
 
40
- const response = await config.api.callApi('GET', endpoint, query);
49
+ // Origin or CDN?
50
+ const source = options.cdn ? 'callCdn' : 'callApi';
51
+ const response = await config.api[source]('GET', endpoint, query, options);
41
52
 
42
53
  if (response.data && format == 'storefront') {
43
54
  for (let i = 0; i < response.data.length; i++) {
@@ -74,12 +85,16 @@ export class RebuyClient {
74
85
  }
75
86
  }
76
87
 
77
- async getData(endpoint, params) {
78
- return await makeCall(endpoint, params);
88
+ async getData(endpoint, params, options = {}) {
89
+ return await makeCall(endpoint, params, null, options);
90
+ }
91
+
92
+ async getDataFromCDN(endpoint, params, options = {}) {
93
+ return await makeCDNCall(endpoint, params, null, options);
79
94
  }
80
95
 
81
- async getStorefrontData(endpoint, params) {
82
- return await makeCall(endpoint, params, 'storefront');
96
+ async getStorefrontData(endpoint, params, options = {}) {
97
+ return await makeCall(endpoint, params, 'storefront', options);
83
98
  }
84
99
 
85
100
  async trackProductViewed(data) {
@@ -103,4 +118,4 @@ export class RebuyClient {
103
118
  }
104
119
  }
105
120
 
106
- export default { RebuyClient };
121
+ export default RebuyClient;
package/geolocation.js CHANGED
@@ -60,4 +60,4 @@ export class Geolocation {
60
60
  }
61
61
  }
62
62
 
63
- export default { Geolocation };
63
+ export default Geolocation;
package/identity.js CHANGED
@@ -69,4 +69,4 @@ export class Identity {
69
69
  }
70
70
  }
71
71
 
72
- export default { Identity };
72
+ export default Identity;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rebuy/rebuy",
3
3
  "description": "This is the default library for Rebuy",
4
- "version": "1.2.0",
4
+ "version": "1.3.1",
5
5
  "license": "MIT",
6
6
  "author": "Rebuy, Inc.",
7
7
  "type": "module",
package/session.js CHANGED
@@ -49,4 +49,4 @@ export class Session {
49
49
  }
50
50
  }
51
51
 
52
- export default { Session };
52
+ export default Session;