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