@rebuy/rebuy 1.2.1 → 1.3.2

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.
Files changed (3) hide show
  1. package/api.js +27 -8
  2. package/client.js +21 -6
  3. package/package.json +1 -1
package/api.js CHANGED
@@ -7,9 +7,16 @@ const config = {
7
7
  eventDomain: 'https://rebuyengine.com',
8
8
  };
9
9
 
10
- const makeCall = async (method, path, data, origin) => {
10
+ const staging = {
11
+ domain: 'https://enigneyuber.com',
12
+ cdnDomain: 'https://cdn.enigneyuber.com',
13
+ eventDomain: 'https://enigneyuber.com',
14
+ };
15
+
16
+ const makeCall = async (method, path, data, origin, options = {}) => {
11
17
  const url = `${origin}${path}`;
12
18
  const requestUrl = new URL(url);
19
+ const fetchOptions = options?.fetch ?? {};
13
20
 
14
21
  const requestData = {
15
22
  key: config.key,
@@ -21,6 +28,7 @@ const makeCall = async (method, path, data, origin) => {
21
28
 
22
29
  const requestObject = {
23
30
  method,
31
+ ...fetchOptions,
24
32
  };
25
33
 
26
34
  if (method == 'GET') {
@@ -33,7 +41,14 @@ const makeCall = async (method, path, data, origin) => {
33
41
  }
34
42
 
35
43
  const request = await fetch(requestUrl, requestObject);
36
- return await request.json();
44
+ const response = await request.json();
45
+
46
+ if (!request.ok && fetchOptions.strictErrors === true) {
47
+ const message = `An error has occurred: ${request.status}`;
48
+ throw new Error(message, { cause: response });
49
+ }
50
+
51
+ return response;
37
52
  };
38
53
 
39
54
  export class Api {
@@ -43,18 +58,22 @@ export class Api {
43
58
  } else if (typeof options == 'object' && options != null) {
44
59
  Object.assign(config, options);
45
60
  }
61
+
62
+ if (location?.host?.includes(staging.domain)) {
63
+ Object.assign(config, staging);
64
+ }
46
65
  }
47
66
 
48
- async callEvent(method, path, data) {
49
- return await makeCall(method, path, data, config.eventDomain);
67
+ async callEvent(method, path, data, options = {}) {
68
+ return await makeCall(method, path, data, config.eventDomain, options);
50
69
  }
51
70
 
52
- async callCdn(method, path, data) {
53
- return await makeCall(method, path, data, config.cdnDomain);
71
+ async callCdn(method, path, data, options = {}) {
72
+ return await makeCall(method, path, data, config.cdnDomain, options);
54
73
  }
55
74
 
56
- async callApi(method, path, data) {
57
- return await makeCall(method, path, data, config.domain);
75
+ async callApi(method, path, data, options = {}) {
76
+ return await makeCall(method, path, data, config.domain, options);
58
77
  }
59
78
  }
60
79
 
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) {
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.1",
4
+ "version": "1.3.2",
5
5
  "license": "MIT",
6
6
  "author": "Rebuy, Inc.",
7
7
  "type": "module",