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