@rebuy/rebuy 1.5.0 → 1.6.0-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/api.d.ts +30 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/client.d.ts +13 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/cookie.d.ts +57 -0
- package/dist/cookie.d.ts.map +1 -0
- package/dist/geolocation.d.ts +6 -0
- package/dist/geolocation.d.ts.map +1 -0
- package/dist/identity.d.ts +10 -0
- package/dist/identity.d.ts.map +1 -0
- package/{index.js → dist/index.d.ts} +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +790 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +767 -0
- package/dist/index.mjs.map +7 -0
- package/dist/session.d.ts +8 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/utilities.d.ts +166 -0
- package/dist/utilities.d.ts.map +1 -0
- package/package.json +50 -16
- package/.git/logs/refs/heads/REB-2426/add-index.js +0 -2
- package/.git/refs/heads/REB-2426/add-index.js +0 -1
- package/api.js +0 -130
- package/client.js +0 -148
- package/cookie.js +0 -153
- package/geolocation.js +0 -63
- package/identity.js +0 -72
- package/session.js +0 -52
- package/utilities.js +0 -342
package/utilities.js
DELETED
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
export function isBase64Encoded(str) {
|
|
2
|
-
const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
3
|
-
return base64Regex.test(str) ? true : false;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export function stringToData(data, decode) {
|
|
7
|
-
let response = data;
|
|
8
|
-
|
|
9
|
-
// Base64 decode
|
|
10
|
-
if (decode == true) {
|
|
11
|
-
response = atob(response);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
try {
|
|
15
|
-
response = JSON.parse(response);
|
|
16
|
-
} catch (e) {
|
|
17
|
-
// failure to parse cookie
|
|
18
|
-
response = data;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return response;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function dataToString(data, encode) {
|
|
25
|
-
let response = data;
|
|
26
|
-
|
|
27
|
-
if (typeof response != 'string') {
|
|
28
|
-
response = JSON.stringify(response);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Base64 encode
|
|
32
|
-
if (encode == true) {
|
|
33
|
-
response = btoa(response);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return response;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function stripHtml(str) {
|
|
40
|
-
return str.replace(/<(.|\n)*?>/g, '');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function getIdFromGraphUrl(graphId, objectType) {
|
|
44
|
-
let id = null;
|
|
45
|
-
|
|
46
|
-
const regex = new RegExp(`gid://shopify/${objectType}/(.*)`);
|
|
47
|
-
const matches = graphId.match(regex);
|
|
48
|
-
|
|
49
|
-
if (matches != null) {
|
|
50
|
-
id = matches[1];
|
|
51
|
-
|
|
52
|
-
if (!isNaN(id)) {
|
|
53
|
-
id = Number(id);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return id;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function variantAvailable(variant) {
|
|
61
|
-
return !(variant.inventory_management && variant.inventory_policy == 'deny' && variant.inventory_quantity <= 0);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function firstAvailableVariant(product) {
|
|
65
|
-
// Select first variant
|
|
66
|
-
let selectedVariant = product.variants[0];
|
|
67
|
-
|
|
68
|
-
// Upgrade to first available
|
|
69
|
-
for (let i = 0; i < product.variants.length; i++) {
|
|
70
|
-
if (variantAvailable(product.variants[i])) {
|
|
71
|
-
selectedVariant = product.variants[i];
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return selectedVariant;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function uuid() {
|
|
80
|
-
let d = new Date().getTime();
|
|
81
|
-
let d2 = (performance && performance.now && performance.now() * 1000) || 0;
|
|
82
|
-
|
|
83
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
84
|
-
let r = Math.random() * 16;
|
|
85
|
-
if (d > 0) {
|
|
86
|
-
r = (d + r) % 16 | 0;
|
|
87
|
-
d = Math.floor(d / 16);
|
|
88
|
-
} else {
|
|
89
|
-
r = (d2 + r) % 16 | 0;
|
|
90
|
-
d2 = Math.floor(d2 / 16);
|
|
91
|
-
}
|
|
92
|
-
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function sessionId() {
|
|
97
|
-
const config = {
|
|
98
|
-
chars: '0123456789',
|
|
99
|
-
length: 12,
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
let sessionId = '';
|
|
103
|
-
let sessionStart = new Date().getTime();
|
|
104
|
-
|
|
105
|
-
for (let i = config.length; i > 0; i--) {
|
|
106
|
-
sessionId += config.chars[Math.floor(Math.random() * config.chars.length)];
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return `REBUY.${sessionStart}.${sessionId}`;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function convertProductToStorefrontFormat(product) {
|
|
113
|
-
let defaultVariant = firstAvailableVariant(product);
|
|
114
|
-
let minVariantPrice = variantMinMaxPriceObject(product, 'price', 'min');
|
|
115
|
-
let maxVariantPrice = variantMinMaxPriceObject(product, 'price', 'max');
|
|
116
|
-
let minVariantCompareAtPrice = variantMinMaxPriceObject(product, 'compare_at_price', 'min');
|
|
117
|
-
let maxVariantCompareAtPrice = variantMinMaxPriceObject(product, 'compare_at_price', 'max');
|
|
118
|
-
let selectedOptions = selectedVariantOptions(product, defaultVariant);
|
|
119
|
-
|
|
120
|
-
let variants = product.variants.map((variant) => convertVariantToStorefrontFormat(product, variant));
|
|
121
|
-
|
|
122
|
-
return {
|
|
123
|
-
id: `gid://shopify/Product/${product.id}`,
|
|
124
|
-
title: product.title,
|
|
125
|
-
handle: product.handle,
|
|
126
|
-
description: stripHtml(product.body_html),
|
|
127
|
-
descriptionHtml: product.body_html,
|
|
128
|
-
vendor: product.vendor,
|
|
129
|
-
featuredImage: productImageObject(product),
|
|
130
|
-
options: [
|
|
131
|
-
...product.options.map((option) => {
|
|
132
|
-
return { name: option.name, values: option.values };
|
|
133
|
-
}),
|
|
134
|
-
],
|
|
135
|
-
selectedOptions,
|
|
136
|
-
priceRange: {
|
|
137
|
-
minVariantPrice,
|
|
138
|
-
maxVariantPrice,
|
|
139
|
-
},
|
|
140
|
-
compareAtPriceRange: {
|
|
141
|
-
minVariantCompareAtPrice,
|
|
142
|
-
maxVariantCompareAtPrice,
|
|
143
|
-
},
|
|
144
|
-
variants: convertToNodes(variants),
|
|
145
|
-
images: null,
|
|
146
|
-
media: [],
|
|
147
|
-
metafields: [],
|
|
148
|
-
collections: null,
|
|
149
|
-
selectedSellingPlan: null,
|
|
150
|
-
selectedSellingPlanAllocation: null,
|
|
151
|
-
sellingPlanGroups: [],
|
|
152
|
-
seo: { title: null, description: null },
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export function convertToNodes(arr) {
|
|
157
|
-
return {
|
|
158
|
-
edges: [
|
|
159
|
-
...arr.map((node) => {
|
|
160
|
-
return { node };
|
|
161
|
-
}),
|
|
162
|
-
],
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export function convertVariantToStorefrontFormat(product, variant) {
|
|
167
|
-
let selectedOptions = selectedVariantOptions(product, variant);
|
|
168
|
-
let image = productImageObject(product, variant.image_id);
|
|
169
|
-
|
|
170
|
-
return {
|
|
171
|
-
id: `gid://shopify/ProductVariant/${variant.id}`,
|
|
172
|
-
availableForSale: variantAvailable(variant),
|
|
173
|
-
priceV2: variantPriceObject(variant, 'price'),
|
|
174
|
-
compareAtPriceV2: variantPriceObject(variant, 'compare_at_price'),
|
|
175
|
-
image: image,
|
|
176
|
-
selectedOptions: [selectedOptions],
|
|
177
|
-
sku: variant.sku,
|
|
178
|
-
title: variant.title,
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export function variantMinMaxPriceObject(product, key = 'price', operator = 'min') {
|
|
183
|
-
let priceObject = variantPriceObject(product.variants[0], key);
|
|
184
|
-
|
|
185
|
-
for (let i = 0; i < product.variants.length; i++) {
|
|
186
|
-
const variantPrice = variantPriceObject(product.variants[i], key);
|
|
187
|
-
|
|
188
|
-
if (variantPrice != null) {
|
|
189
|
-
const variantPriceAmount = Number(variantPrice.amount);
|
|
190
|
-
const currentPriceAmount = priceObject != null ? Number(priceObject.amount) : null;
|
|
191
|
-
|
|
192
|
-
if (
|
|
193
|
-
currentPriceAmount == null ||
|
|
194
|
-
(operator == 'min' && variantPriceAmount < currentPriceAmount) ||
|
|
195
|
-
(operator == 'max' && variantPriceAmount > currentPriceAmount)
|
|
196
|
-
) {
|
|
197
|
-
priceObject = variantPrice;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
return priceObject;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export function variantPriceObject(variant, key = 'price') {
|
|
206
|
-
if (variant[key] != null) {
|
|
207
|
-
return {
|
|
208
|
-
amount: variant[key],
|
|
209
|
-
currencyCode: 'USD',
|
|
210
|
-
};
|
|
211
|
-
} else {
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
export function selectedVariantOptions(product, variant) {
|
|
217
|
-
let selectedOptions = {};
|
|
218
|
-
|
|
219
|
-
if (variant.option1 != null) {
|
|
220
|
-
selectedOptions[product.options[0].name] = variant.option1;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (variant.option2 != null) {
|
|
224
|
-
selectedOptions[product.options[1].name] = variant.option2;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (variant.option3 != null) {
|
|
228
|
-
selectedOptions[product.options[2].name] = variant.option3;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return selectedOptions;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export function productImageObject(product, id) {
|
|
235
|
-
let image = null;
|
|
236
|
-
|
|
237
|
-
if (product.image) {
|
|
238
|
-
image = {
|
|
239
|
-
id: `gid://shopify/ProductImage/${product.image.id}`,
|
|
240
|
-
url: product.image.src,
|
|
241
|
-
altText: product.image.alt,
|
|
242
|
-
width: product.image.width,
|
|
243
|
-
height: product.image.height,
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
if (product.images && product.images.length > 0 && id != null) {
|
|
248
|
-
const matchingImage = product.images.find((i) => i.id == id);
|
|
249
|
-
|
|
250
|
-
if (matchingImage) {
|
|
251
|
-
image = {
|
|
252
|
-
id: `gid://shopify/ProductImage/${matchingImage.id}`,
|
|
253
|
-
url: matchingImage.src,
|
|
254
|
-
altText: matchingImage.alt,
|
|
255
|
-
width: matchingImage.width,
|
|
256
|
-
height: matchingImage.height,
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
return image;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export function queryStringToObject(str) {
|
|
265
|
-
const params = new URLSearchParams(str);
|
|
266
|
-
return Object.fromEntries(params.entries());
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export function utmObjectFromString(str) {
|
|
270
|
-
const utmKeys = ['utm_campaign', 'utm_medium', 'utm_source', 'utm_term', 'utm_content'];
|
|
271
|
-
|
|
272
|
-
const matches = {};
|
|
273
|
-
|
|
274
|
-
const url = new URL(str);
|
|
275
|
-
const queryObject = queryStringToObject(url.search);
|
|
276
|
-
|
|
277
|
-
for (const [key, value] of Object.entries(queryObject)) {
|
|
278
|
-
if (utmKeys.includes(key)) {
|
|
279
|
-
matches[key] = value;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
return Object.keys(matches).length > 0 ? matches : null;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
export function amountToCents(amount) {
|
|
287
|
-
if (isNaN(amount)) {
|
|
288
|
-
amount = 0;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
if (typeof amount != 'string') {
|
|
292
|
-
amount = amount.toString();
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
if (amount.indexOf('.') != -1) {
|
|
296
|
-
amount = parseFloat(amount).toFixed(2) * 100;
|
|
297
|
-
} else {
|
|
298
|
-
amount = parseInt(amount);
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
return parseInt(amount);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
export function serialize(obj) {
|
|
305
|
-
const serialized = [];
|
|
306
|
-
|
|
307
|
-
const add = (key, value) => {
|
|
308
|
-
value = typeof value === 'function' ? value() : value;
|
|
309
|
-
value = value === null ? '' : value === undefined ? '' : value;
|
|
310
|
-
serialized[serialized.length] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
const buildParameters = (prefix, obj) => {
|
|
314
|
-
let i, len, key;
|
|
315
|
-
|
|
316
|
-
if (prefix) {
|
|
317
|
-
if (Array.isArray(obj)) {
|
|
318
|
-
for (i = 0, len = obj.length; i < len; i++) {
|
|
319
|
-
buildParameters(prefix + '[' + (typeof obj[i] === 'object' && obj[i] ? i : '') + ']', obj[i]);
|
|
320
|
-
}
|
|
321
|
-
} else if (Object.prototype.toString.call(obj) === '[object Object]') {
|
|
322
|
-
for (key in obj) {
|
|
323
|
-
buildParameters(prefix + '[' + key + ']', obj[key]);
|
|
324
|
-
}
|
|
325
|
-
} else {
|
|
326
|
-
add(prefix, obj);
|
|
327
|
-
}
|
|
328
|
-
} else if (Array.isArray(obj)) {
|
|
329
|
-
for (i = 0, len = obj.length; i < len; i++) {
|
|
330
|
-
add(obj[i].name, obj[i].value);
|
|
331
|
-
}
|
|
332
|
-
} else {
|
|
333
|
-
for (key in obj) {
|
|
334
|
-
buildParameters(key, obj[key]);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
return serialized;
|
|
339
|
-
};
|
|
340
|
-
|
|
341
|
-
return buildParameters('', obj).join('&');
|
|
342
|
-
}
|