@rebuy/rebuy 1.1.0 → 1.3.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.
@@ -1,2 +1,2 @@
1
- 0000000000000000000000000000000000000000 bcf5e549305c9c226f00e3b71bf30a78aa1dbff1 Mike Reid <mike.reid@rebuyengine.com> 1658870225 -0600 branch: Created from HEAD
2
- bcf5e549305c9c226f00e3b71bf30a78aa1dbff1 38b5136db509bf2ef9e5eb9e1e76eff50e2a2115 Mike Reid <mike.reid@rebuyengine.com> 1658870245 -0600 commit: Update: Add index.js for improved imports
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
- 38b5136db509bf2ef9e5eb9e1e76eff50e2a2115
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,10 +35,17 @@ 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
- export default class Api {
48
+ export class Api {
40
49
  constructor(options) {
41
50
  if (typeof options == 'string') {
42
51
  config.key = options;
@@ -45,15 +54,17 @@ export default 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
  }
69
+
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++) {
@@ -48,7 +59,7 @@ const makeCall = async (endpoint, params, format) => {
48
59
  return response;
49
60
  };
50
61
 
51
- export default class RebuyClient {
62
+ export class RebuyClient {
52
63
  constructor(key, defaultParameters) {
53
64
  if (typeof key == 'string') {
54
65
  config.key = key;
@@ -74,12 +85,16 @@ export default 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);
79
90
  }
80
91
 
81
- async getStorefrontData(endpoint, params) {
82
- return await makeCall(endpoint, params, 'storefront');
92
+ async getDataFromCDN(endpoint, params, options = {}) {
93
+ return await makeCDNCall(endpoint, params, null, options);
94
+ }
95
+
96
+ async getStorefrontData(endpoint, params, options = {}) {
97
+ return await makeCall(endpoint, params, 'storefront', options);
83
98
  }
84
99
 
85
100
  async trackProductViewed(data) {
@@ -102,3 +117,5 @@ export default class RebuyClient {
102
117
  return null;
103
118
  }
104
119
  }
120
+
121
+ export default RebuyClient;
package/geolocation.js CHANGED
@@ -33,7 +33,7 @@ const getGeolocation = async () => {
33
33
  return config.geolocation;
34
34
  };
35
35
 
36
- export default class Geolocation {
36
+ export class Geolocation {
37
37
  constructor(key) {
38
38
  if (typeof document == 'undefined') {
39
39
  return;
@@ -59,3 +59,5 @@ export default class Geolocation {
59
59
  return config.geolocation;
60
60
  }
61
61
  }
62
+
63
+ export default Geolocation;
package/identity.js CHANGED
@@ -13,7 +13,7 @@ const config = {
13
13
  session: null,
14
14
  };
15
15
 
16
- export default class Identity {
16
+ export class Identity {
17
17
  constructor(key) {
18
18
  if (typeof document == 'undefined') {
19
19
  return;
@@ -68,3 +68,5 @@ export default class Identity {
68
68
  return await config.geolocation.geolocation();
69
69
  }
70
70
  }
71
+
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.1.0",
4
+ "version": "1.3.0",
5
5
  "license": "MIT",
6
6
  "author": "Rebuy, Inc.",
7
7
  "type": "module",
package/session.js CHANGED
@@ -10,7 +10,7 @@ const config = {
10
10
  },
11
11
  };
12
12
 
13
- export default class Session {
13
+ export class Session {
14
14
  constructor() {
15
15
  if (typeof document == 'undefined') {
16
16
  return;
@@ -48,3 +48,5 @@ export default class Session {
48
48
  return parseInt((config.now - this.sessionStart()) / 1000 / 60);
49
49
  }
50
50
  }
51
+
52
+ export default Session;