@rebuy/rebuy 1.1.0-rc.3 → 1.1.0-rc.4

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/api.js CHANGED
@@ -5,22 +5,50 @@ const config = {
5
5
  domain: 'https://rebuyengine.com',
6
6
  cdnDomain: 'https://cdn.rebuyengine.com',
7
7
  eventDomain: 'https://rebuyengine.com',
8
+ shieldDomain: 'https://cached.rebuyengine.com',
9
+ staticDomain: 'https://static.rebuyengine.com',
10
+ shop: null,
8
11
  };
9
12
 
10
- const makeCall = async (method, path, data, origin) => {
13
+ const staging = {
14
+ domain: 'https://enigneyuber.com',
15
+ cdnDomain: 'https://cdn.enigneyuber.com',
16
+ eventDomain: 'https://enigneyuber.com',
17
+ shieldDomain: 'https://cached.enigneyuber.com',
18
+ staticDomain: 'https://static.enigneyuber.com',
19
+ };
20
+
21
+ const stagingDomains = [
22
+ 'enigneyuber.com',
23
+ 'rebuy-engine-regression.myshopify.com',
24
+ 'rebuy-stage-currency-test.myshopify.com',
25
+ 'rebuy-staging-automation.myshopify.com',
26
+ 'rebuy-staging-regression.myshopify.com',
27
+ 'rebuy-uat.myshopify.com',
28
+ 'rebuy-regression-qa.myshopify.com',
29
+ 'senator-staging.myshopify.com',
30
+ 'mike-reids-test-store.myshopify.com',
31
+ 'dash-ext-hoke.myshopify.com',
32
+ ];
33
+
34
+ const makeCall = async (method, path, data, origin, options = {}) => {
11
35
  const url = `${origin}${path}`;
12
36
  const requestUrl = new URL(url);
37
+ const fetchOptions = options?.fetch ?? {};
13
38
 
14
- const requestData = {
15
- key: config.key,
16
- };
39
+ const requestData = {};
17
40
 
18
41
  if (typeof data == 'object' && data != null) {
19
42
  Object.assign(requestData, data);
20
43
  }
21
44
 
45
+ if (typeof config.key == 'string' && config.key.length > 0) {
46
+ Object.assign(requestData, { key: config.key });
47
+ }
48
+
22
49
  const requestObject = {
23
50
  method,
51
+ ...fetchOptions,
24
52
  };
25
53
 
26
54
  if (method == 'GET') {
@@ -33,7 +61,14 @@ const makeCall = async (method, path, data, origin) => {
33
61
  }
34
62
 
35
63
  const request = await fetch(requestUrl, requestObject);
36
- return await request.json();
64
+ const response = await request.json();
65
+
66
+ if (!request.ok && fetchOptions.strictErrors === true) {
67
+ const message = `An error has occurred: ${request.status}`;
68
+ throw new Error(message, { cause: response });
69
+ }
70
+
71
+ return response;
37
72
  };
38
73
 
39
74
  export class Api {
@@ -43,18 +78,40 @@ export class Api {
43
78
  } else if (typeof options == 'object' && options != null) {
44
79
  Object.assign(config, options);
45
80
  }
81
+
82
+ for (const domain of stagingDomains) {
83
+ if (typeof location === 'object' && location?.host?.includes(domain)) {
84
+ Object.assign(config, staging);
85
+ break;
86
+ }
87
+ if (typeof config.shop === 'string' && config.shop.includes(domain)) {
88
+ Object.assign(config, staging);
89
+ break;
90
+ }
91
+ }
92
+ }
93
+
94
+ async callEvent(method, path, data, options = {}) {
95
+ return await makeCall(method, path, data, config.eventDomain, options);
96
+ }
97
+
98
+ async callShield(method, path, data, options = {}) {
99
+ return await makeCall(method, path, data, config.shieldDomain, options);
46
100
  }
47
101
 
48
- async callEvent(method, path, data) {
49
- return await makeCall(method, path, data, config.eventDomain);
102
+ async callStatic(method, path, data, options = {}) {
103
+ return await makeCall(method, path, data, config.staticDomain, options);
50
104
  }
51
105
 
52
- async callCdn(method, path, data) {
53
- return await makeCall(method, path, data, config.cdnDomain);
106
+ /**
107
+ * @deprecated
108
+ */
109
+ async callCdn(method, path, data, options = {}) {
110
+ return await makeCall(method, path, data, config.cdnDomain, options);
54
111
  }
55
112
 
56
- async callApi(method, path, data) {
57
- return await makeCall(method, path, data, config.domain);
113
+ async callApi(method, path, data, options = {}) {
114
+ return await makeCall(method, path, data, config.domain, options);
58
115
  }
59
116
  }
60
117
 
package/client.js CHANGED
@@ -1,6 +1,6 @@
1
- import { convertProductToStorefrontFormat } from './utilities.js';
2
- import Identity from './identity.js';
3
1
  import Api from './api.js';
2
+ import Identity from './identity.js';
3
+ import { convertProductToStorefrontFormat } from './utilities.js';
4
4
 
5
5
  const config = {
6
6
  key: null,
@@ -8,6 +8,7 @@ const config = {
8
8
  contextParameters: null,
9
9
  api: null,
10
10
  identity: null,
11
+ shop: null,
11
12
  };
12
13
 
13
14
  const trackEvent = async (eventData) => {
@@ -18,7 +19,22 @@ const trackEvent = async (eventData) => {
18
19
  return await config.api.callEvent('POST', '/api/v1/analytics/event', eventData);
19
20
  };
20
21
 
21
- const makeCall = async (endpoint, params, format) => {
22
+ const makeShieldCall = async (endpoint, params, format, options = {}) => {
23
+ return await makeCall(endpoint, params, format, { ...options, shield: true });
24
+ };
25
+
26
+ const makeStaticCall = async (endpoint, params, format, options = {}) => {
27
+ return await makeCall(endpoint, params, format, { ...options, static: true });
28
+ };
29
+
30
+ /**
31
+ * @deprecated
32
+ */
33
+ const makeCDNCall = async (endpoint, params, format, options = {}) => {
34
+ return await makeCall(endpoint, params, format, { ...options, cdn: true });
35
+ };
36
+
37
+ const makeCall = async (endpoint, params, format, options = {}) => {
22
38
  const query = {};
23
39
 
24
40
  if (config.defaultParameters != null) {
@@ -33,11 +49,18 @@ const makeCall = async (endpoint, params, format) => {
33
49
  Object.assign(query, params);
34
50
  }
35
51
 
52
+ if (typeof options != 'object' || options == null) {
53
+ console.warn('Unsupported fetch options provided.', options);
54
+ options = {};
55
+ }
56
+
36
57
  if (config.identity && config.identity.visitorId()) {
37
58
  query.uuid = config.identity.visitorId();
38
59
  }
39
60
 
40
- const response = await config.api.callApi('GET', endpoint, query);
61
+ // Origin or dedicated edge?
62
+ const source = options.cdn ? 'callCdn' : options.shield ? 'callShield' : options.static ? 'callStatic' : 'callApi';
63
+ const response = await config.api[source]('GET', endpoint, query, options);
41
64
 
42
65
  if (response.data && format == 'storefront') {
43
66
  for (let i = 0; i < response.data.length; i++) {
@@ -49,7 +72,7 @@ const makeCall = async (endpoint, params, format) => {
49
72
  };
50
73
 
51
74
  export class RebuyClient {
52
- constructor(key, defaultParameters) {
75
+ constructor(key, defaultParameters, shop) {
53
76
  if (typeof key == 'string') {
54
77
  config.key = key;
55
78
  }
@@ -58,7 +81,11 @@ export class RebuyClient {
58
81
  config.defaultParameters = defaultParameters;
59
82
  }
60
83
 
61
- config.api = new Api(config.key);
84
+ if (typeof shop == 'string' && shop.endsWith('.myshopify.com')) {
85
+ config.shop = shop;
86
+ }
87
+
88
+ config.api = new Api({ key: config.key, shop: config.shop });
62
89
  config.identity = new Identity();
63
90
  }
64
91
 
@@ -74,12 +101,27 @@ export class RebuyClient {
74
101
  }
75
102
  }
76
103
 
77
- async getData(endpoint, params) {
78
- return await makeCall(endpoint, params);
104
+ async getData(endpoint, params, options = {}) {
105
+ return await makeCall(endpoint, params, null, options);
106
+ }
107
+
108
+ /**
109
+ * @deprecated
110
+ */
111
+ async getDataFromCDN(endpoint, params, options = {}) {
112
+ return await makeCDNCall(endpoint, params, null, options);
113
+ }
114
+
115
+ async getShieldedAsset(endpoint, params, options = {}) {
116
+ return await makeShieldCall(endpoint, params, null, options);
117
+ }
118
+
119
+ async getStaticAsset(endpoint, params, options = {}) {
120
+ return await makeStaticCall(endpoint, params, null, options);
79
121
  }
80
122
 
81
- async getStorefrontData(endpoint, params) {
82
- return await makeCall(endpoint, params, 'storefront');
123
+ async getStorefrontData(endpoint, params, options = {}) {
124
+ return await makeCall(endpoint, params, 'storefront', options);
83
125
  }
84
126
 
85
127
  async trackProductViewed(data) {
package/cookie.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { dataToString, stringToData, isBase64Encoded } from './utilities.js';
2
2
 
3
3
  export function get(name) {
4
- if (typeof document == 'undefined') {
4
+ if (typeof document == 'undefined' || !document.cookie) {
5
5
  return null;
6
6
  }
7
7
 
@@ -49,7 +49,7 @@ export function getAll() {
49
49
  }
50
50
 
51
51
  export function set(name, value, config) {
52
- if (typeof document == 'undefined') {
52
+ if (typeof document == 'undefined' || !document.cookie) {
53
53
  return null;
54
54
  }
55
55
 
package/geolocation.js CHANGED
@@ -35,7 +35,7 @@ const getGeolocation = async () => {
35
35
 
36
36
  export class Geolocation {
37
37
  constructor(key) {
38
- if (typeof document == 'undefined') {
38
+ if (typeof document == 'undefined' || !document.cookie) {
39
39
  return;
40
40
  }
41
41
 
package/identity.js CHANGED
@@ -15,7 +15,7 @@ const config = {
15
15
 
16
16
  export class Identity {
17
17
  constructor(key) {
18
- if (typeof document == 'undefined') {
18
+ if (typeof document == 'undefined' || !document.cookie) {
19
19
  return;
20
20
  }
21
21
 
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-rc.3",
4
+ "version": "1.1.0-rc.4",
5
5
  "license": "MIT",
6
6
  "author": "Rebuy, Inc.",
7
7
  "type": "module",
package/session.js CHANGED
@@ -12,7 +12,7 @@ const config = {
12
12
 
13
13
  export class Session {
14
14
  constructor() {
15
- if (typeof document == 'undefined') {
15
+ if (typeof document == 'undefined' || !document.cookie) {
16
16
  return;
17
17
  }
18
18
 
package/utilities.js CHANGED
@@ -15,6 +15,7 @@ export function stringToData(data, decode) {
15
15
  response = JSON.parse(response);
16
16
  } catch (e) {
17
17
  // failure to parse cookie
18
+ response = data;
18
19
  }
19
20
 
20
21
  return response;