@rebuy/rebuy 2.14.0 → 2.15.0-rc.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 (39) hide show
  1. package/dist/client.js +720 -0
  2. package/dist/client.js.map +7 -0
  3. package/dist/client.mjs +697 -0
  4. package/dist/client.mjs.map +7 -0
  5. package/dist/index.js +329 -313
  6. package/dist/index.js.map +4 -4
  7. package/dist/index.mjs +326 -310
  8. package/dist/index.mjs.map +4 -4
  9. package/dist/schema/checkout-and-beyond.js +862 -0
  10. package/dist/schema/checkout-and-beyond.js.map +7 -0
  11. package/dist/schema/checkout-and-beyond.mjs +839 -0
  12. package/dist/schema/checkout-and-beyond.mjs.map +7 -0
  13. package/dist/schema/shop-config.js +105 -0
  14. package/dist/schema/shop-config.js.map +7 -0
  15. package/dist/schema/shop-config.mjs +84 -0
  16. package/dist/schema/shop-config.mjs.map +7 -0
  17. package/dist/schema/user-config.js +115 -0
  18. package/dist/schema/user-config.js.map +7 -0
  19. package/dist/schema/user-config.mjs +94 -0
  20. package/dist/schema/user-config.mjs.map +7 -0
  21. package/dist/schema/widget-data.js +834 -0
  22. package/dist/schema/widget-data.js.map +7 -0
  23. package/dist/schema/widget-data.mjs +813 -0
  24. package/dist/schema/widget-data.mjs.map +7 -0
  25. package/dist/schema/widgets/checkout-and-beyond/cartLine.d.ts +141 -0
  26. package/dist/schema/widgets/checkout-and-beyond/cartLine.d.ts.map +1 -0
  27. package/dist/schema/widgets/checkout-and-beyond/common.d.ts +3 -1
  28. package/dist/schema/widgets/checkout-and-beyond/common.d.ts.map +1 -1
  29. package/dist/schema/widgets/checkout-and-beyond/index.d.ts +1 -0
  30. package/dist/schema/widgets/checkout-and-beyond/index.d.ts.map +1 -1
  31. package/dist/schema/widgets/checkout-and-beyond/shared.d.ts +2 -1
  32. package/dist/schema/widgets/checkout-and-beyond/shared.d.ts.map +1 -1
  33. package/dist/schema/widgets/checkout-and-beyond/utils.d.ts +2 -0
  34. package/dist/schema/widgets/checkout-and-beyond/utils.d.ts.map +1 -1
  35. package/dist/utilities.js +323 -0
  36. package/dist/utilities.js.map +7 -0
  37. package/dist/utilities.mjs +302 -0
  38. package/dist/utilities.mjs.map +7 -0
  39. package/package.json +29 -3
package/dist/client.js ADDED
@@ -0,0 +1,720 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/client.ts
21
+ var client_exports = {};
22
+ __export(client_exports, {
23
+ RebuyClient: () => RebuyClient,
24
+ default: () => client_default
25
+ });
26
+ module.exports = __toCommonJS(client_exports);
27
+
28
+ // src/utilities.ts
29
+ function isBase64Encoded(str) {
30
+ const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
31
+ return base64Regex.test(str) ? true : false;
32
+ }
33
+ function stringToData(data, decode) {
34
+ let response = data;
35
+ if (decode === true) {
36
+ response = atob(response);
37
+ }
38
+ try {
39
+ response = JSON.parse(response);
40
+ } catch (e) {
41
+ response = data;
42
+ }
43
+ return response;
44
+ }
45
+ function dataToString(data, encode) {
46
+ let response = data;
47
+ if (typeof response != "string") {
48
+ response = JSON.stringify(response);
49
+ }
50
+ if (encode === true) {
51
+ response = btoa(response);
52
+ }
53
+ return response;
54
+ }
55
+ function stripHtml(str) {
56
+ return str?.replace(/<(.|\n)*?>/g, "") ?? "";
57
+ }
58
+ function variantAvailable(variant) {
59
+ return !(variant.inventory_management && variant.inventory_policy === "deny" && variant.inventory_quantity <= 0);
60
+ }
61
+ function firstAvailableVariant(product) {
62
+ let selectedVariant = product.variants[0];
63
+ for (let i = 0; i < product.variants.length; i++) {
64
+ if (variantAvailable(product.variants[i])) {
65
+ selectedVariant = product.variants[i];
66
+ break;
67
+ }
68
+ }
69
+ return selectedVariant;
70
+ }
71
+ function uuid() {
72
+ let d = (/* @__PURE__ */ new Date()).getTime();
73
+ let d2 = performance && performance.now && performance.now() * 1e3 || 0;
74
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
75
+ let r = Math.random() * 16;
76
+ if (d > 0) {
77
+ r = (d + r) % 16 | 0;
78
+ d = Math.floor(d / 16);
79
+ } else {
80
+ r = (d2 + r) % 16 | 0;
81
+ d2 = Math.floor(d2 / 16);
82
+ }
83
+ return (c === "x" ? r : r & 3 | 8).toString(16);
84
+ });
85
+ }
86
+ function sessionId() {
87
+ const config2 = {
88
+ chars: "0123456789",
89
+ length: 12
90
+ };
91
+ let sessionId2 = "";
92
+ const sessionStart = (/* @__PURE__ */ new Date()).getTime();
93
+ for (let i = config2.length; i > 0; i--) {
94
+ sessionId2 += config2.chars[Math.floor(Math.random() * config2.chars.length)];
95
+ }
96
+ return `REBUY.${sessionStart}.${sessionId2}`;
97
+ }
98
+ function convertProductToStorefrontFormat(product) {
99
+ const defaultVariant = firstAvailableVariant(product);
100
+ const minVariantPrice = variantMinMaxPriceObject(product, "price", "min");
101
+ const maxVariantPrice = variantMinMaxPriceObject(product, "price", "max");
102
+ const minVariantCompareAtPrice = variantMinMaxPriceObject(product, "compare_at_price", "min");
103
+ const maxVariantCompareAtPrice = variantMinMaxPriceObject(product, "compare_at_price", "max");
104
+ const selectedOptions = selectedVariantOptions(product, defaultVariant);
105
+ const variants = product.variants.map((variant) => convertVariantToStorefrontFormat(product, variant));
106
+ return {
107
+ collections: null,
108
+ compareAtPriceRange: {
109
+ maxVariantCompareAtPrice,
110
+ minVariantCompareAtPrice
111
+ },
112
+ description: stripHtml(product.body_html),
113
+ descriptionHtml: product.body_html,
114
+ featuredImage: productImageObject(product, null),
115
+ handle: product.handle,
116
+ id: `gid://shopify/Product/${product.id}`,
117
+ images: null,
118
+ media: [],
119
+ metafields: [],
120
+ options: [
121
+ ...product.options.map((option) => {
122
+ return { name: option.name, values: option.values };
123
+ })
124
+ ],
125
+ priceRange: {
126
+ maxVariantPrice,
127
+ minVariantPrice
128
+ },
129
+ selectedOptions,
130
+ selectedSellingPlan: null,
131
+ selectedSellingPlanAllocation: null,
132
+ sellingPlanGroups: [],
133
+ seo: { description: null, title: null },
134
+ title: product.title,
135
+ variants: convertToNodes(variants),
136
+ vendor: product.vendor
137
+ };
138
+ }
139
+ function convertToNodes(arr) {
140
+ return {
141
+ edges: [
142
+ ...arr.map((node) => {
143
+ return { node };
144
+ })
145
+ ]
146
+ };
147
+ }
148
+ function convertVariantToStorefrontFormat(product, variant) {
149
+ const selectedOptions = selectedVariantOptions(product, variant);
150
+ const image = productImageObject(product, variant.image_id);
151
+ return {
152
+ availableForSale: variantAvailable(variant),
153
+ compareAtPriceV2: variantPriceObject(variant, "compare_at_price"),
154
+ id: `gid://shopify/ProductVariant/${variant.id}`,
155
+ image,
156
+ priceV2: variantPriceObject(variant, "price"),
157
+ product: {
158
+ handle: product.handle,
159
+ id: product.id,
160
+ title: product.title,
161
+ vendor: product.vendor
162
+ },
163
+ selectedOptions: [selectedOptions],
164
+ sku: variant.sku,
165
+ title: variant.title
166
+ };
167
+ }
168
+ function variantMinMaxPriceObject(product, key = "price", operator = "min") {
169
+ let priceObject = variantPriceObject(product.variants[0], key);
170
+ for (let i = 0; i < product.variants.length; i++) {
171
+ const variantPrice = variantPriceObject(product.variants[i], key);
172
+ if (variantPrice != null) {
173
+ const variantPriceAmount = Number(variantPrice.amount);
174
+ const currentPriceAmount = priceObject != null ? Number(priceObject.amount) : null;
175
+ if (currentPriceAmount == null || operator === "min" && variantPriceAmount < currentPriceAmount || operator === "max" && variantPriceAmount > currentPriceAmount) {
176
+ priceObject = variantPrice;
177
+ }
178
+ }
179
+ }
180
+ return priceObject;
181
+ }
182
+ function variantPriceObject(variant, key = "price") {
183
+ if (variant[key] != null) {
184
+ return {
185
+ amount: variant[key],
186
+ currencyCode: "USD"
187
+ };
188
+ }
189
+ return null;
190
+ }
191
+ function selectedVariantOptions(product, variant) {
192
+ const selectedOptions = {};
193
+ if (variant.option1 != null) {
194
+ selectedOptions[product.options[0].name] = variant.option1;
195
+ }
196
+ if (variant.option2 != null) {
197
+ selectedOptions[product.options[1].name] = variant.option2;
198
+ }
199
+ if (variant.option3 != null) {
200
+ selectedOptions[product.options[2].name] = variant.option3;
201
+ }
202
+ return selectedOptions;
203
+ }
204
+ function productImageObject(product, id) {
205
+ let image = null;
206
+ if (product.image) {
207
+ image = {
208
+ altText: product.image.alt,
209
+ height: product.image.height,
210
+ id: `gid://shopify/ProductImage/${product.image.id}`,
211
+ url: product.image.src,
212
+ width: product.image.width
213
+ };
214
+ }
215
+ if (product.images && product.images.length > 0 && id != null) {
216
+ const matchingImage = product.images.find((i) => i.id === id);
217
+ if (matchingImage) {
218
+ image = {
219
+ altText: matchingImage.alt,
220
+ height: matchingImage.height,
221
+ id: `gid://shopify/ProductImage/${matchingImage.id}`,
222
+ url: matchingImage.src,
223
+ width: matchingImage.width
224
+ };
225
+ }
226
+ }
227
+ return image;
228
+ }
229
+ function serialize(obj) {
230
+ const serialized = [];
231
+ const add = (key, value) => {
232
+ value = typeof value === "function" ? value() : value;
233
+ value = value === null ? "" : value === void 0 ? "" : value;
234
+ serialized[serialized.length] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
235
+ };
236
+ const buildParameters = (prefix, obj2) => {
237
+ let i, key, len;
238
+ if (prefix) {
239
+ if (Array.isArray(obj2)) {
240
+ for (i = 0, len = obj2.length; i < len; i++) {
241
+ buildParameters(prefix + "[" + (typeof obj2[i] === "object" && obj2[i] ? i : "") + "]", obj2[i]);
242
+ }
243
+ } else if (Object.prototype.toString.call(obj2) === "[object Object]") {
244
+ for (key in obj2) {
245
+ buildParameters(prefix + "[" + key + "]", obj2[key]);
246
+ }
247
+ } else {
248
+ add(prefix, obj2);
249
+ }
250
+ } else if (Array.isArray(obj2)) {
251
+ for (i = 0, len = obj2.length; i < len; i++) {
252
+ add(obj2[i].name, obj2[i].value);
253
+ }
254
+ } else {
255
+ for (key in obj2) {
256
+ buildParameters(key, obj2[key]);
257
+ }
258
+ }
259
+ return serialized;
260
+ };
261
+ return buildParameters("", obj).join("&");
262
+ }
263
+
264
+ // src/api.ts
265
+ var staging = {
266
+ /**
267
+ * @deprecated
268
+ */
269
+ cdnDomain: "https://cdn.enigneyuber.com",
270
+ domain: "https://enigneyuber.com",
271
+ eventDomain: "https://enigneyuber.com",
272
+ geoDomain: "https://geo.enigneyuber.com",
273
+ shieldDomain: "https://cached.enigneyuber.com",
274
+ staticDomain: "https://cdn.enigneyuber.com"
275
+ };
276
+ var stagingDomains = [
277
+ "cab-staging.myshopify.com",
278
+ "enigneyuber.com",
279
+ "rebuy-engine-regression.myshopify.com",
280
+ "rebuy-stage-currency-test.myshopify.com",
281
+ "rebuy-staging-automation.myshopify.com",
282
+ "rebuy-staging-regression.myshopify.com",
283
+ "rebuy-uat.myshopify.com",
284
+ "rebuy-regression-qa.myshopify.com",
285
+ "senator-staging.myshopify.com",
286
+ "mike-reids-test-store.myshopify.com",
287
+ "dash-ext-hoke.myshopify.com"
288
+ ];
289
+ var makeCall = async (key, method, path, data, origin, options = {}) => {
290
+ const url = `${origin}${path}`;
291
+ const requestUrl = new URL(url);
292
+ const fetchOptions = options?.fetch ?? {};
293
+ const requestData = {};
294
+ if (typeof data == "object" && data != null) {
295
+ Object.assign(requestData, data);
296
+ }
297
+ if (typeof key == "string" && key.length > 0) {
298
+ Object.assign(requestData, { key });
299
+ }
300
+ const requestObject = {
301
+ method,
302
+ ...fetchOptions
303
+ };
304
+ if (method === "GET") {
305
+ requestUrl.search = serialize(requestData);
306
+ } else if (method === "POST") {
307
+ requestObject.headers = {
308
+ "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
309
+ };
310
+ requestObject.body = new URLSearchParams(requestData);
311
+ }
312
+ const request = await fetch(requestUrl.toString(), requestObject);
313
+ const response = await request.json();
314
+ if (!request.ok && fetchOptions.strictErrors === true) {
315
+ const message = `An error has occurred: ${request.status}`;
316
+ throw new Error(message, { cause: response });
317
+ }
318
+ return response;
319
+ };
320
+ var Api = class {
321
+ constructor(options) {
322
+ this.config = {
323
+ /**
324
+ * @deprecated
325
+ */
326
+ cdnDomain: "https://cdn.rebuyengine.com",
327
+ domain: "https://rebuyengine.com",
328
+ eventDomain: "https://rebuyengine.com",
329
+ geoDomain: "https://geo.rebuyengine.com",
330
+ key: null,
331
+ shieldDomain: "https://cached.rebuyengine.com",
332
+ shop: null,
333
+ staticDomain: "https://cdn.rebuyengine.com"
334
+ };
335
+ if (typeof options == "string") {
336
+ this.config.key = options;
337
+ } else if (typeof options == "object" && options != null) {
338
+ Object.assign(this.config, options);
339
+ }
340
+ for (const domain of stagingDomains) {
341
+ if (typeof location === "object" && location?.host?.includes(domain)) {
342
+ Object.assign(this.config, staging);
343
+ break;
344
+ }
345
+ if (typeof this.config.shop === "string" && this.config.shop.includes(domain)) {
346
+ Object.assign(this.config, staging);
347
+ break;
348
+ }
349
+ }
350
+ }
351
+ async callEvent(method, path, data, options) {
352
+ return await makeCall(this.config.key, method, path, data, this.config.eventDomain, options);
353
+ }
354
+ async callShield(method, path, data, options) {
355
+ return await makeCall(this.config.key, method, path, data, this.config.shieldDomain, options);
356
+ }
357
+ async callStatic(method, path, data, options) {
358
+ return await makeCall(this.config.key, method, path, data, this.config.staticDomain, options);
359
+ }
360
+ async callGeo(method, path, data, options) {
361
+ return await makeCall(this.config.key, method, path, data, this.config.geoDomain, options);
362
+ }
363
+ async callCdn(method, path, data, options) {
364
+ return await makeCall(this.config.key, method, path, data, this.config.cdnDomain, options);
365
+ }
366
+ async callApi(method, path, data, options) {
367
+ return await makeCall(this.config.key, method, path, data, this.config.domain, options);
368
+ }
369
+ };
370
+ var api_default = Api;
371
+
372
+ // src/cookie.ts
373
+ function get(name) {
374
+ if (typeof document == "undefined" || !document.cookie) {
375
+ return null;
376
+ }
377
+ const cookie = document.cookie.match("(^|;) ?" + decodeURIComponent(name) + "=([^;]*)(;|$)");
378
+ let value = null;
379
+ if (cookie != null) {
380
+ const data = decodeURIComponent(cookie[2]);
381
+ const decode = isBase64Encoded(data) ? true : false;
382
+ value = stringToData(data, decode);
383
+ }
384
+ return value;
385
+ }
386
+ function getAll() {
387
+ const cookies = {};
388
+ if (document && document.cookie && document.cookie !== "") {
389
+ const split = document.cookie.split(";");
390
+ for (let i = 0; i < split.length; i++) {
391
+ const pairs = split[i].split("=");
392
+ pairs[0] = pairs[0].replace(/^ /, "");
393
+ const key = decodeURIComponent(pairs[0]);
394
+ const value = decodeURIComponent(pairs[1]);
395
+ const decode = isBase64Encoded(value) ? true : false;
396
+ cookies[key] = stringToData(value, decode);
397
+ }
398
+ }
399
+ return cookies;
400
+ }
401
+ function set(name, value, config2) {
402
+ if (typeof document == "undefined" || !document.cookie) {
403
+ return;
404
+ }
405
+ const attributes = ["path", "domain", "maxAge", "expires", "secure", "sameSite"];
406
+ const convenienceTimes = ["seconds", "minutes", "hours", "days", "weeks", "months", "years"];
407
+ const cookieAttributes = {
408
+ path: "/"
409
+ };
410
+ if (typeof config2 != "undefined" && Number.isInteger(config2)) {
411
+ cookieAttributes["max-age"] = config2;
412
+ } else if (typeof config2 === "object" && config2 !== null) {
413
+ for (const key in config2) {
414
+ if (attributes.includes(key)) {
415
+ if (key === "maxAge") {
416
+ cookieAttributes["max-age"] = config2[key];
417
+ } else if (key === "sameSite") {
418
+ cookieAttributes["samesite"] = config2[key];
419
+ } else if (key === "expires") {
420
+ cookieAttributes[key] = new Date(config2[key]).toUTCString();
421
+ } else {
422
+ cookieAttributes[key] = config2[key];
423
+ }
424
+ } else if (convenienceTimes.includes(key)) {
425
+ let duration = config2[key];
426
+ if (key === "seconds") {
427
+ duration = duration * 1;
428
+ } else if (key === "minutes") {
429
+ duration = duration * 60;
430
+ } else if (key === "hours") {
431
+ duration = duration * 60 * 60;
432
+ } else if (key === "days") {
433
+ duration = duration * 60 * 60 * 24;
434
+ } else if (key === "weeks") {
435
+ duration = duration * 60 * 60 * 24 * 7;
436
+ } else if (key === "months") {
437
+ duration = duration * 60 * 60 * 24 * 30;
438
+ } else if (key === "years") {
439
+ duration = duration * 60 * 60 * 24 * 365;
440
+ }
441
+ cookieAttributes["max-age"] = duration;
442
+ }
443
+ }
444
+ }
445
+ value = dataToString(value, config2?.encode);
446
+ let cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);
447
+ for (const key in cookieAttributes) {
448
+ cookie += ";" + key + "=" + cookieAttributes[key];
449
+ }
450
+ document.cookie = cookie;
451
+ }
452
+ function find(name) {
453
+ const matches = [];
454
+ const cookies = getAll();
455
+ for (const key in cookies) {
456
+ if (key.includes(name)) {
457
+ matches.push({
458
+ name: key,
459
+ value: cookies[key]
460
+ });
461
+ }
462
+ }
463
+ return matches;
464
+ }
465
+ function destroy(name) {
466
+ set(name, "", { encode: false, seconds: 0 });
467
+ }
468
+ function enabled() {
469
+ const test = {
470
+ key: "__cookie_test",
471
+ value: 1
472
+ };
473
+ set(test.key, test.value, { encode: false });
474
+ const enabled2 = get(test.key) === test.value ? true : false;
475
+ if (enabled2) {
476
+ destroy(test.key);
477
+ }
478
+ return enabled2;
479
+ }
480
+ var cookie_default = { destroy, enabled, find, get, getAll, set };
481
+
482
+ // src/geolocation.ts
483
+ var Geolocation = class {
484
+ constructor(key) {
485
+ this.config = {
486
+ geolocation: null,
487
+ geolocationCookie: "_rebuyGeolocation",
488
+ geolocationDuration: {
489
+ minutes: 30
490
+ },
491
+ key: null
492
+ };
493
+ if (typeof document == "undefined" || !document.cookie) {
494
+ return;
495
+ }
496
+ if (typeof key == "string") {
497
+ this.config.key = key;
498
+ }
499
+ this.config.geolocation = cookie_default.get(this.config.geolocationCookie);
500
+ if (this.config.geolocation === null) {
501
+ this._fetchGeolocation();
502
+ }
503
+ }
504
+ async _fetchGeolocation() {
505
+ const api = new api_default({ key: this.config.key });
506
+ const response = await api.callGeo("GET", "/", null);
507
+ if (response.data) {
508
+ this.config.geolocation = response.data;
509
+ const cookieOptions = {
510
+ secure: true
511
+ };
512
+ Object.assign(cookieOptions, this.config.geolocationDuration);
513
+ cookie_default.set(this.config.geolocationCookie, this.config.geolocation, { ...cookieOptions, encode: false });
514
+ }
515
+ return this.config.geolocation;
516
+ }
517
+ async geolocation() {
518
+ if (this.config.geolocation == null) {
519
+ await this._fetchGeolocation();
520
+ }
521
+ return this.config.geolocation;
522
+ }
523
+ };
524
+ var geolocation_default = Geolocation;
525
+
526
+ // src/session.ts
527
+ var config = {
528
+ now: null,
529
+ sessionDuration: {
530
+ minutes: 30
531
+ },
532
+ sessionId: null,
533
+ sessionIdCookie: "_rebuySessionId"
534
+ };
535
+ var Session = class {
536
+ constructor() {
537
+ if (typeof document == "undefined" || !document.cookie) {
538
+ return;
539
+ }
540
+ config.now = (/* @__PURE__ */ new Date()).getTime();
541
+ config.sessionId = cookie_default.get(config.sessionIdCookie);
542
+ if (config.sessionId === null) {
543
+ config.sessionId = sessionId();
544
+ }
545
+ const cookieOptions = {
546
+ secure: true
547
+ };
548
+ Object.assign(cookieOptions, config.sessionDuration);
549
+ cookie_default.set(config.sessionIdCookie, config.sessionId, cookieOptions);
550
+ }
551
+ sessionId() {
552
+ return config.sessionId;
553
+ }
554
+ sessionStart() {
555
+ return config.sessionId ? Number(config.sessionId.split(".")[1]) : 0;
556
+ }
557
+ sessionDuration() {
558
+ return config.now !== null ? parseInt(((config.now - this.sessionStart()) / 1e3 / 60).toString()) : 0;
559
+ }
560
+ };
561
+ var session_default = Session;
562
+
563
+ // src/identity.ts
564
+ var Identity = class {
565
+ constructor(key) {
566
+ this.config = {
567
+ geolocation: null,
568
+ key: null,
569
+ session: null,
570
+ visitorDuration: {
571
+ years: 1
572
+ },
573
+ visitorId: null,
574
+ visitorIdCookie: "_rebuyVisitorId"
575
+ };
576
+ if (typeof document == "undefined" || !document.cookie) {
577
+ return;
578
+ }
579
+ if (typeof key == "string") {
580
+ this.config.key = key;
581
+ }
582
+ this.config.visitorId = cookie_default.get(this.config.visitorIdCookie);
583
+ if (this.config.visitorId === null) {
584
+ this.config.visitorId = uuid();
585
+ }
586
+ const cookieOptions = {
587
+ secure: true
588
+ };
589
+ Object.assign(cookieOptions, this.config.visitorDuration);
590
+ cookie_default.set(this.config.visitorIdCookie, this.config.visitorId, cookieOptions);
591
+ this.config.session = new session_default();
592
+ this.config.geolocation = new geolocation_default(this.config.key);
593
+ }
594
+ visitorId() {
595
+ return this.config.visitorId;
596
+ }
597
+ sessionId() {
598
+ return this.config.session ? this.config.session.sessionId() : null;
599
+ }
600
+ sessionStart() {
601
+ return this.config.session ? this.config.session.sessionStart() : null;
602
+ }
603
+ sessionDuration() {
604
+ return this.config.session ? this.config.session.sessionDuration() : null;
605
+ }
606
+ async geolocation() {
607
+ return this.config.geolocation ? await this.config.geolocation.geolocation() : null;
608
+ }
609
+ };
610
+ var identity_default = Identity;
611
+
612
+ // src/client.ts
613
+ var trackEvent = async (config2, eventData) => {
614
+ if (config2.identity && config2.identity.visitorId()) {
615
+ eventData.uuid = config2.identity.visitorId();
616
+ }
617
+ return await config2.api.callEvent("POST", "/api/v1/analytics/event", eventData);
618
+ };
619
+ var makeShieldCall = async (config2, endpoint, params, format, options = {}) => {
620
+ return await makeCall2(config2, endpoint, params, format, { ...options, shield: true });
621
+ };
622
+ var makeStaticCall = async (config2, endpoint, params, format, options = {}) => {
623
+ return await makeCall2(config2, endpoint, params, format, { ...options, static: true });
624
+ };
625
+ var makeCDNCall = async (config2, endpoint, params, format, options = {}) => {
626
+ return await makeCall2(config2, endpoint, params, format, { ...options, cdn: true });
627
+ };
628
+ var makeCall2 = async (config2, endpoint, params, format, options = {}) => {
629
+ const query = {};
630
+ if (config2.defaultParameters != null) {
631
+ Object.assign(query, config2.defaultParameters);
632
+ }
633
+ if (config2.contextParameters != null) {
634
+ Object.assign(query, config2.contextParameters);
635
+ }
636
+ if (typeof params == "object" && params != null) {
637
+ Object.assign(query, params);
638
+ }
639
+ if (typeof options != "object" || options == null) {
640
+ console.warn("Unsupported fetch options provided.", options);
641
+ options = {};
642
+ }
643
+ if (config2.identity && config2.identity.visitorId()) {
644
+ query.uuid = config2.identity.visitorId();
645
+ }
646
+ const source = options.cdn ? "callCdn" : options.shield ? "callShield" : options.static ? "callStatic" : "callApi";
647
+ const response = await config2.api[source]("GET", endpoint, query, options);
648
+ if (response.data && format === "storefront") {
649
+ for (let i = 0; i < response.data.length; i++) {
650
+ response.data[i] = convertProductToStorefrontFormat(response.data[i]);
651
+ }
652
+ }
653
+ return response;
654
+ };
655
+ var RebuyClient = class {
656
+ constructor(key, defaultParameters, shop) {
657
+ this.config = {
658
+ api: null,
659
+ contextParameters: null,
660
+ defaultParameters: null,
661
+ identity: null,
662
+ key: null,
663
+ shop: null
664
+ };
665
+ if (typeof key == "string") {
666
+ this.config.key = key;
667
+ }
668
+ if (typeof defaultParameters == "object" && defaultParameters != null) {
669
+ this.config.defaultParameters = defaultParameters;
670
+ }
671
+ if (typeof shop == "string" && shop.endsWith(".myshopify.com")) {
672
+ this.config.shop = shop;
673
+ }
674
+ this.config.api = new api_default({ key: this.config.key, shop: this.config.shop });
675
+ this.config.identity = new identity_default(this.config.key);
676
+ }
677
+ setDefaultParameters(defaultParameters) {
678
+ if (typeof defaultParameters == "object" && defaultParameters != null) {
679
+ this.config.defaultParameters = defaultParameters;
680
+ }
681
+ }
682
+ setContextParameters(contextParameters) {
683
+ if (typeof contextParameters == "object" && contextParameters != null) {
684
+ this.config.contextParameters = contextParameters;
685
+ }
686
+ }
687
+ async getData(endpoint, params, options = {}) {
688
+ return await makeCall2(this.config, endpoint, params, null, options);
689
+ }
690
+ async getDataFromCDN(endpoint, params, options = {}) {
691
+ return await makeCDNCall(this.config, endpoint, params, null, options);
692
+ }
693
+ async getShieldedAsset(endpoint, params, options = {}) {
694
+ return await makeShieldCall(this.config, endpoint, params, null, options);
695
+ }
696
+ async getStaticAsset(endpoint, params, options = {}) {
697
+ return await makeStaticCall(this.config, endpoint, params, null, options);
698
+ }
699
+ async getStorefrontData(endpoint, params, options = {}) {
700
+ return await makeCall2(this.config, endpoint, params, "storefront", options);
701
+ }
702
+ async trackProductViewed(data) {
703
+ const requiredKeys = ["shopify_product_id", "shopify_product_handle"];
704
+ const defaultData = {
705
+ noun: "product",
706
+ subject: "user",
707
+ verb: "viewed"
708
+ };
709
+ if (typeof data != "undefined" && data != null) {
710
+ const dataKeys = Object.keys(data);
711
+ if (dataKeys.some((key) => requiredKeys.includes(key))) {
712
+ const payload = Object.assign(data, defaultData);
713
+ return await trackEvent(this.config, payload);
714
+ }
715
+ }
716
+ return null;
717
+ }
718
+ };
719
+ var client_default = RebuyClient;
720
+ //# sourceMappingURL=client.js.map