@daouypkgs/commerce-commercetools 0.0.183
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/README.md +1 -0
- package/dist/cart/index.d.ts +2 -0
- package/dist/cart/use-add-item.d.ts +5 -0
- package/dist/cart/use-cart.d.ts +5 -0
- package/dist/cart/use-remove-item.d.ts +11 -0
- package/dist/cart/use-update-item.d.ts +11 -0
- package/dist/client.d.ts +3 -0
- package/dist/commerce-commercetools.cjs.development.js +1201 -0
- package/dist/commerce-commercetools.cjs.development.js.map +1 -0
- package/dist/commerce-commercetools.cjs.production.min.js +2 -0
- package/dist/commerce-commercetools.cjs.production.min.js.map +1 -0
- package/dist/commerce-commercetools.esm.js +1189 -0
- package/dist/commerce-commercetools.esm.js.map +1 -0
- package/dist/commercetools.d.ts +5 -0
- package/dist/const.d.ts +4 -0
- package/dist/fetcher.d.ts +3 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +8 -0
- package/dist/product/use-product.d.ts +8 -0
- package/dist/product/use-search.d.ts +12 -0
- package/dist/provider.d.ts +49 -0
- package/dist/registerCommerceProvider.d.ts +11 -0
- package/dist/registerable.d.ts +6 -0
- package/dist/site/use-brands.d.ts +5 -0
- package/dist/site/use-categories.d.ts +5 -0
- package/dist/types/cart.d.ts +24 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/product.d.ts +14 -0
- package/dist/types/site.d.ts +7 -0
- package/dist/utils/cart-active.d.ts +4 -0
- package/dist/utils/cart-cookie.d.ts +3 -0
- package/dist/utils/cart-create.d.ts +4 -0
- package/dist/utils/common.d.ts +2 -0
- package/dist/utils/cookies.d.ts +3 -0
- package/dist/utils/get-sort-variables.d.ts +2 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/localized-string.d.ts +2 -0
- package/dist/utils/normalize.d.ts +21 -0
- package/package.json +47 -0
|
@@ -0,0 +1,1201 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
|
+
|
|
7
|
+
var registerGlobalContext = _interopDefault(require('@daouy/host/registerGlobalContext'));
|
|
8
|
+
var query = require('@daouy/query');
|
|
9
|
+
var commerce = require('@daouypkgs/commerce');
|
|
10
|
+
var React = require('react');
|
|
11
|
+
var React__default = _interopDefault(React);
|
|
12
|
+
var Cookies = _interopDefault(require('js-cookie'));
|
|
13
|
+
var debounce = _interopDefault(require('debounce'));
|
|
14
|
+
var platformSdk = require('@commercetools/platform-sdk');
|
|
15
|
+
var sdkClientV2 = require('@commercetools/sdk-client-v2');
|
|
16
|
+
|
|
17
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) {
|
|
18
|
+
try {
|
|
19
|
+
var i = n[a](c),
|
|
20
|
+
u = i.value;
|
|
21
|
+
} catch (n) {
|
|
22
|
+
return void e(n);
|
|
23
|
+
}
|
|
24
|
+
i.done ? t(u) : Promise.resolve(u).then(r, o);
|
|
25
|
+
}
|
|
26
|
+
function _asyncToGenerator(n) {
|
|
27
|
+
return function () {
|
|
28
|
+
var t = this,
|
|
29
|
+
e = arguments;
|
|
30
|
+
return new Promise(function (r, o) {
|
|
31
|
+
var a = n.apply(t, e);
|
|
32
|
+
function _next(n) {
|
|
33
|
+
asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
|
|
34
|
+
}
|
|
35
|
+
function _throw(n) {
|
|
36
|
+
asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
|
|
37
|
+
}
|
|
38
|
+
_next(void 0);
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function _extends() {
|
|
43
|
+
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
|
44
|
+
for (var e = 1; e < arguments.length; e++) {
|
|
45
|
+
var t = arguments[e];
|
|
46
|
+
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
|
47
|
+
}
|
|
48
|
+
return n;
|
|
49
|
+
}, _extends.apply(null, arguments);
|
|
50
|
+
}
|
|
51
|
+
function _regenerator() {
|
|
52
|
+
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
|
|
53
|
+
var e,
|
|
54
|
+
t,
|
|
55
|
+
r = "function" == typeof Symbol ? Symbol : {},
|
|
56
|
+
n = r.iterator || "@@iterator",
|
|
57
|
+
o = r.toStringTag || "@@toStringTag";
|
|
58
|
+
function i(r, n, o, i) {
|
|
59
|
+
var c = n && n.prototype instanceof Generator ? n : Generator,
|
|
60
|
+
u = Object.create(c.prototype);
|
|
61
|
+
return _regeneratorDefine(u, "_invoke", function (r, n, o) {
|
|
62
|
+
var i,
|
|
63
|
+
c,
|
|
64
|
+
u,
|
|
65
|
+
f = 0,
|
|
66
|
+
p = o || [],
|
|
67
|
+
y = !1,
|
|
68
|
+
G = {
|
|
69
|
+
p: 0,
|
|
70
|
+
n: 0,
|
|
71
|
+
v: e,
|
|
72
|
+
a: d,
|
|
73
|
+
f: d.bind(e, 4),
|
|
74
|
+
d: function (t, r) {
|
|
75
|
+
return i = t, c = 0, u = e, G.n = r, a;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
function d(r, n) {
|
|
79
|
+
for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) {
|
|
80
|
+
var o,
|
|
81
|
+
i = p[t],
|
|
82
|
+
d = G.p,
|
|
83
|
+
l = i[2];
|
|
84
|
+
r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0));
|
|
85
|
+
}
|
|
86
|
+
if (o || r > 1) return a;
|
|
87
|
+
throw y = !0, n;
|
|
88
|
+
}
|
|
89
|
+
return function (o, p, l) {
|
|
90
|
+
if (f > 1) throw TypeError("Generator is already running");
|
|
91
|
+
for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) {
|
|
92
|
+
i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u);
|
|
93
|
+
try {
|
|
94
|
+
if (f = 2, i) {
|
|
95
|
+
if (c || (o = "next"), t = i[o]) {
|
|
96
|
+
if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object");
|
|
97
|
+
if (!t.done) return t;
|
|
98
|
+
u = t.value, c < 2 && (c = 0);
|
|
99
|
+
} else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1);
|
|
100
|
+
i = e;
|
|
101
|
+
} else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break;
|
|
102
|
+
} catch (t) {
|
|
103
|
+
i = e, c = 1, u = t;
|
|
104
|
+
} finally {
|
|
105
|
+
f = 1;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
value: t,
|
|
110
|
+
done: y
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
}(r, o, i), !0), u;
|
|
114
|
+
}
|
|
115
|
+
var a = {};
|
|
116
|
+
function Generator() {}
|
|
117
|
+
function GeneratorFunction() {}
|
|
118
|
+
function GeneratorFunctionPrototype() {}
|
|
119
|
+
t = Object.getPrototypeOf;
|
|
120
|
+
var c = [][n] ? t(t([][n]())) : (_regeneratorDefine(t = {}, n, function () {
|
|
121
|
+
return this;
|
|
122
|
+
}), t),
|
|
123
|
+
u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c);
|
|
124
|
+
function f(e) {
|
|
125
|
+
return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e;
|
|
126
|
+
}
|
|
127
|
+
return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine(u), _regeneratorDefine(u, o, "Generator"), _regeneratorDefine(u, n, function () {
|
|
128
|
+
return this;
|
|
129
|
+
}), _regeneratorDefine(u, "toString", function () {
|
|
130
|
+
return "[object Generator]";
|
|
131
|
+
}), (_regenerator = function () {
|
|
132
|
+
return {
|
|
133
|
+
w: i,
|
|
134
|
+
m: f
|
|
135
|
+
};
|
|
136
|
+
})();
|
|
137
|
+
}
|
|
138
|
+
function _regeneratorDefine(e, r, n, t) {
|
|
139
|
+
var i = Object.defineProperty;
|
|
140
|
+
try {
|
|
141
|
+
i({}, "", {});
|
|
142
|
+
} catch (e) {
|
|
143
|
+
i = 0;
|
|
144
|
+
}
|
|
145
|
+
_regeneratorDefine = function (e, r, n, t) {
|
|
146
|
+
function o(r, n) {
|
|
147
|
+
_regeneratorDefine(e, r, function (e) {
|
|
148
|
+
return this._invoke(r, n, e);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
r ? i ? i(e, r, {
|
|
152
|
+
value: n,
|
|
153
|
+
enumerable: !t,
|
|
154
|
+
configurable: !t,
|
|
155
|
+
writable: !t
|
|
156
|
+
}) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2));
|
|
157
|
+
}, _regeneratorDefine(e, r, n, t);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
var getSortVariables = function getSortVariables(sort, isCategory) {
|
|
161
|
+
var output = undefined;
|
|
162
|
+
switch (sort) {
|
|
163
|
+
case 'price-asc':
|
|
164
|
+
output = 'price asc';
|
|
165
|
+
break;
|
|
166
|
+
case 'price-desc':
|
|
167
|
+
output = 'price desc';
|
|
168
|
+
break;
|
|
169
|
+
case 'trending-desc':
|
|
170
|
+
// default option
|
|
171
|
+
output = undefined;
|
|
172
|
+
break;
|
|
173
|
+
case 'latest-desc':
|
|
174
|
+
output = 'createdAt desc';
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
return output;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
var COMMERCETOOLS_COOKIE_EXPIRE = 30;
|
|
181
|
+
var COMMERCETOOLS_CART_COOKIE = 'commercetools_cart';
|
|
182
|
+
|
|
183
|
+
var options = {
|
|
184
|
+
expires: COMMERCETOOLS_COOKIE_EXPIRE,
|
|
185
|
+
sameSite: "none",
|
|
186
|
+
secure: true
|
|
187
|
+
};
|
|
188
|
+
var getCookies = function getCookies(name) {
|
|
189
|
+
var cookie = Cookies.get(name);
|
|
190
|
+
return cookie ? JSON.parse(cookie) : undefined;
|
|
191
|
+
};
|
|
192
|
+
var setCookies = function setCookies(name, value) {
|
|
193
|
+
return Cookies.set(name, JSON.stringify(value), options);
|
|
194
|
+
};
|
|
195
|
+
var removeCookies = function removeCookies(name) {
|
|
196
|
+
return Cookies.remove(name);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
var getCartId = function getCartId() {
|
|
200
|
+
return getCookies(COMMERCETOOLS_CART_COOKIE);
|
|
201
|
+
};
|
|
202
|
+
var setCartId = function setCartId(id) {
|
|
203
|
+
return setCookies(COMMERCETOOLS_CART_COOKIE, id);
|
|
204
|
+
};
|
|
205
|
+
var removeCartCookie = function removeCartCookie() {
|
|
206
|
+
return removeCookies(COMMERCETOOLS_CART_COOKIE);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
var createCart = /*#__PURE__*/function () {
|
|
210
|
+
var _ref = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(fetch) {
|
|
211
|
+
var draft, cart;
|
|
212
|
+
return _regenerator().w(function (_context) {
|
|
213
|
+
while (1) switch (_context.n) {
|
|
214
|
+
case 0:
|
|
215
|
+
draft = {
|
|
216
|
+
currency: 'USD',
|
|
217
|
+
country: 'US'
|
|
218
|
+
};
|
|
219
|
+
_context.n = 1;
|
|
220
|
+
return fetch({
|
|
221
|
+
query: 'carts',
|
|
222
|
+
method: 'post',
|
|
223
|
+
body: draft
|
|
224
|
+
});
|
|
225
|
+
case 1:
|
|
226
|
+
cart = _context.v;
|
|
227
|
+
if (!cart.body) {
|
|
228
|
+
removeCartCookie();
|
|
229
|
+
} else {
|
|
230
|
+
setCartId(cart.body.id);
|
|
231
|
+
}
|
|
232
|
+
return _context.a(2, cart.body);
|
|
233
|
+
}
|
|
234
|
+
}, _callee);
|
|
235
|
+
}));
|
|
236
|
+
return function createCart(_x) {
|
|
237
|
+
return _ref.apply(this, arguments);
|
|
238
|
+
};
|
|
239
|
+
}();
|
|
240
|
+
|
|
241
|
+
var getActiveCart = /*#__PURE__*/function () {
|
|
242
|
+
var _ref = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(fetch) {
|
|
243
|
+
var cartId, activeCart;
|
|
244
|
+
return _regenerator().w(function (_context) {
|
|
245
|
+
while (1) switch (_context.n) {
|
|
246
|
+
case 0:
|
|
247
|
+
cartId = getCartId();
|
|
248
|
+
if (!cartId) {
|
|
249
|
+
_context.n = 2;
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
_context.n = 1;
|
|
253
|
+
return fetch({
|
|
254
|
+
query: 'carts',
|
|
255
|
+
method: 'get',
|
|
256
|
+
variables: {
|
|
257
|
+
id: cartId
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
case 1:
|
|
261
|
+
activeCart = _context.v.body;
|
|
262
|
+
_context.n = 4;
|
|
263
|
+
break;
|
|
264
|
+
case 2:
|
|
265
|
+
_context.n = 3;
|
|
266
|
+
return createCart(fetch);
|
|
267
|
+
case 3:
|
|
268
|
+
activeCart = _context.v;
|
|
269
|
+
case 4:
|
|
270
|
+
if (!activeCart) {
|
|
271
|
+
removeCartCookie();
|
|
272
|
+
} else {
|
|
273
|
+
setCartId(activeCart.id);
|
|
274
|
+
}
|
|
275
|
+
return _context.a(2, activeCart);
|
|
276
|
+
}
|
|
277
|
+
}, _callee);
|
|
278
|
+
}));
|
|
279
|
+
return function getActiveCart(_x) {
|
|
280
|
+
return _ref.apply(this, arguments);
|
|
281
|
+
};
|
|
282
|
+
}();
|
|
283
|
+
|
|
284
|
+
var getLocalizedString = function getLocalizedString(localizedString, locale) {
|
|
285
|
+
return !localizedString || !locale ? undefined : locale in localizedString ? localizedString[locale] : Object.values(localizedString)[0];
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
var withoutNils = function withoutNils(xs) {
|
|
289
|
+
return xs.filter(function (x) {
|
|
290
|
+
return x != null;
|
|
291
|
+
});
|
|
292
|
+
};
|
|
293
|
+
var dedup = function dedup(xs) {
|
|
294
|
+
return [].concat(new Set(xs));
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
var currencyCode = 'USD';
|
|
298
|
+
var stringify = function stringify(value) {
|
|
299
|
+
return typeof value === 'string' ? value : JSON.stringify(value);
|
|
300
|
+
};
|
|
301
|
+
var money = function money(price) {
|
|
302
|
+
return price ? {
|
|
303
|
+
value: price.centAmount / 100,
|
|
304
|
+
currencyCode: price.currencyCode
|
|
305
|
+
} : {
|
|
306
|
+
value: -1.0,
|
|
307
|
+
currencyCode: currencyCode
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
var normalizeProductOption = function normalizeProductOption(option) {
|
|
311
|
+
return {
|
|
312
|
+
__typename: 'MultipleChoiceOption',
|
|
313
|
+
id: option.name,
|
|
314
|
+
displayName: option.name,
|
|
315
|
+
values: dedup(Array.isArray(option.value) ? option.value : [option.value]).map(function (val) {
|
|
316
|
+
if (option.name.match(/colou?r/gi) && /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i.test(val)) {
|
|
317
|
+
return {
|
|
318
|
+
label: stringify(val),
|
|
319
|
+
hexColors: [val]
|
|
320
|
+
};
|
|
321
|
+
} else {
|
|
322
|
+
return {
|
|
323
|
+
label: stringify(val)
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
})
|
|
327
|
+
};
|
|
328
|
+
};
|
|
329
|
+
var normalizeProductImages = function normalizeProductImages(images) {
|
|
330
|
+
return images.map(function (image) {
|
|
331
|
+
return _extends({
|
|
332
|
+
url: image.url
|
|
333
|
+
}, image.label ? {
|
|
334
|
+
alt: image.label
|
|
335
|
+
} : {}, {
|
|
336
|
+
width: image.dimensions.w,
|
|
337
|
+
height: image.dimensions.h
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
};
|
|
341
|
+
var normalizeProductVariant = function normalizeProductVariant(variant) {
|
|
342
|
+
var _variant$prices$find$, _variant$prices, _variant$prices2, _variant$sku, _variant$attributes$m, _variant$attributes;
|
|
343
|
+
var price = money((_variant$prices$find$ = (_variant$prices = variant.prices) == null || (_variant$prices = _variant$prices.find(function (price) {
|
|
344
|
+
return price.value.currencyCode === currencyCode;
|
|
345
|
+
})) == null ? void 0 : _variant$prices.value) != null ? _variant$prices$find$ : (_variant$prices2 = variant.prices) == null || (_variant$prices2 = _variant$prices2[0]) == null ? void 0 : _variant$prices2.value).value;
|
|
346
|
+
return {
|
|
347
|
+
id: "" + variant.id,
|
|
348
|
+
name: "" + variant.id,
|
|
349
|
+
sku: (_variant$sku = variant.sku) != null ? _variant$sku : '',
|
|
350
|
+
price: price,
|
|
351
|
+
options: (_variant$attributes$m = (_variant$attributes = variant.attributes) == null ? void 0 : _variant$attributes.map(function (attribute) {
|
|
352
|
+
return normalizeProductOption({
|
|
353
|
+
name: attribute.name,
|
|
354
|
+
value: attribute.value.key
|
|
355
|
+
});
|
|
356
|
+
})) != null ? _variant$attributes$m : [],
|
|
357
|
+
requiresShipping: false,
|
|
358
|
+
listPrice: price
|
|
359
|
+
};
|
|
360
|
+
};
|
|
361
|
+
var normalizeProduct = function normalizeProduct(product, locale) {
|
|
362
|
+
var _getLocalizedString, _getLocalizedString2, _product$masterVarian, _product$masterVarian2, _product$masterVarian3;
|
|
363
|
+
return {
|
|
364
|
+
id: product.id,
|
|
365
|
+
name: (_getLocalizedString = getLocalizedString(product.name, locale)) != null ? _getLocalizedString : "",
|
|
366
|
+
slug: getLocalizedString(product.slug, locale),
|
|
367
|
+
path: "/" + getLocalizedString(product.slug, locale),
|
|
368
|
+
description: (_getLocalizedString2 = getLocalizedString(product.description, locale)) != null ? _getLocalizedString2 : '',
|
|
369
|
+
price: money((_product$masterVarian = (_product$masterVarian2 = product.masterVariant.prices) == null || (_product$masterVarian2 = _product$masterVarian2.find(function (price) {
|
|
370
|
+
return price.value.currencyCode === currencyCode;
|
|
371
|
+
})) == null ? void 0 : _product$masterVarian2.value) != null ? _product$masterVarian : (_product$masterVarian3 = product.masterVariant.prices) == null || (_product$masterVarian3 = _product$masterVarian3[0]) == null ? void 0 : _product$masterVarian3.value),
|
|
372
|
+
images: normalizeProductImages(withoutNils([].concat(product.masterVariant.images ? product.masterVariant.images : [], product.variants.flatMap(function (variant) {
|
|
373
|
+
return variant.images;
|
|
374
|
+
})))),
|
|
375
|
+
variants: [product.masterVariant].concat(product.variants).map(normalizeProductVariant),
|
|
376
|
+
options: withoutNils([].concat(product.masterVariant.attributes ? product.masterVariant.attributes : [], product.variants.flatMap(function (variant) {
|
|
377
|
+
return variant.attributes;
|
|
378
|
+
}))).reduce(function (groupedAttributes, attribute) {
|
|
379
|
+
var groupedAttribute = groupedAttributes.find(function (gAttr) {
|
|
380
|
+
return gAttr.name === attribute.name;
|
|
381
|
+
});
|
|
382
|
+
if (groupedAttribute) {
|
|
383
|
+
groupedAttribute.value.push(stringify(attribute.value.key));
|
|
384
|
+
} else {
|
|
385
|
+
groupedAttributes.push({
|
|
386
|
+
name: attribute.name,
|
|
387
|
+
value: [stringify(attribute.value.key)]
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
return groupedAttributes;
|
|
391
|
+
}, []).map(normalizeProductOption)
|
|
392
|
+
};
|
|
393
|
+
};
|
|
394
|
+
var normalizeLineItem = function normalizeLineItem(lineItem, locale) {
|
|
395
|
+
var _getLocalizedString3, _lineItem$variant$att, _lineItem$variant$att2;
|
|
396
|
+
return {
|
|
397
|
+
id: lineItem.id,
|
|
398
|
+
variantId: "" + lineItem.variant.id,
|
|
399
|
+
productId: lineItem.productId,
|
|
400
|
+
name: (_getLocalizedString3 = getLocalizedString(lineItem.name, locale)) != null ? _getLocalizedString3 : "",
|
|
401
|
+
path: '',
|
|
402
|
+
quantity: lineItem.quantity,
|
|
403
|
+
discounts: [],
|
|
404
|
+
variant: normalizeProductVariant(lineItem.variant),
|
|
405
|
+
options: (_lineItem$variant$att = (_lineItem$variant$att2 = lineItem.variant.attributes) == null ? void 0 : _lineItem$variant$att2.map(function (attribute) {
|
|
406
|
+
return {
|
|
407
|
+
id: attribute.name,
|
|
408
|
+
name: attribute.name,
|
|
409
|
+
value: attribute.value.key
|
|
410
|
+
};
|
|
411
|
+
})) != null ? _lineItem$variant$att : []
|
|
412
|
+
};
|
|
413
|
+
};
|
|
414
|
+
var normalizeCart = function normalizeCart(cart, locale) {
|
|
415
|
+
return {
|
|
416
|
+
id: cart.id,
|
|
417
|
+
customerId: cart.customerId,
|
|
418
|
+
email: cart.customerEmail,
|
|
419
|
+
createdAt: cart.createdAt,
|
|
420
|
+
currency: {
|
|
421
|
+
code: currencyCode
|
|
422
|
+
},
|
|
423
|
+
taxesIncluded: cart.taxMode !== 'Disabled',
|
|
424
|
+
lineItems: cart.lineItems.map(function (item) {
|
|
425
|
+
return normalizeLineItem(item, locale);
|
|
426
|
+
}),
|
|
427
|
+
lineItemsSubtotalPrice: 0,
|
|
428
|
+
subtotalPrice: money(cart.totalPrice).value,
|
|
429
|
+
totalPrice: money(cart.totalPrice).value,
|
|
430
|
+
discounts: []
|
|
431
|
+
};
|
|
432
|
+
};
|
|
433
|
+
var normalizeCategory = function normalizeCategory(category, locale) {
|
|
434
|
+
var _getLocalizedString4, _getLocalizedString5;
|
|
435
|
+
return {
|
|
436
|
+
id: category.id,
|
|
437
|
+
name: (_getLocalizedString4 = getLocalizedString(category.name, locale)) != null ? _getLocalizedString4 : "",
|
|
438
|
+
slug: (_getLocalizedString5 = getLocalizedString(category.slug, locale)) != null ? _getLocalizedString5 : "",
|
|
439
|
+
path: "/" + getLocalizedString(category.slug, locale)
|
|
440
|
+
};
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
var handler = {
|
|
444
|
+
fetchOptions: {
|
|
445
|
+
query: "cart",
|
|
446
|
+
method: "get"
|
|
447
|
+
},
|
|
448
|
+
fetcher: function fetcher(_ref) {
|
|
449
|
+
return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
450
|
+
var fetch, provider, activeCart;
|
|
451
|
+
return _regenerator().w(function (_context) {
|
|
452
|
+
while (1) switch (_context.n) {
|
|
453
|
+
case 0:
|
|
454
|
+
fetch = _ref.fetch, provider = _ref.provider;
|
|
455
|
+
_context.n = 1;
|
|
456
|
+
return getActiveCart(fetch);
|
|
457
|
+
case 1:
|
|
458
|
+
activeCart = _context.v;
|
|
459
|
+
return _context.a(2, activeCart ? normalizeCart(activeCart, provider.locale) : null);
|
|
460
|
+
}
|
|
461
|
+
}, _callee);
|
|
462
|
+
}))();
|
|
463
|
+
},
|
|
464
|
+
useHook: function useHook(_ref2) {
|
|
465
|
+
var useData = _ref2.useData;
|
|
466
|
+
return function (input) {
|
|
467
|
+
var response = useData({
|
|
468
|
+
swrOptions: _extends({
|
|
469
|
+
revalidateOnFocus: false
|
|
470
|
+
}, input == null ? void 0 : input.swrOptions)
|
|
471
|
+
});
|
|
472
|
+
return React.useMemo(function () {
|
|
473
|
+
return Object.create(response, {
|
|
474
|
+
isEmpty: {
|
|
475
|
+
get: function get() {
|
|
476
|
+
var _response$data$lineIt, _response$data;
|
|
477
|
+
return ((_response$data$lineIt = (_response$data = response.data) == null ? void 0 : _response$data.lineItems.length) != null ? _response$data$lineIt : 0) <= 0;
|
|
478
|
+
},
|
|
479
|
+
enumerable: true
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
}, [response]);
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
var handler$1 = {
|
|
488
|
+
fetchOptions: {
|
|
489
|
+
query: "cart",
|
|
490
|
+
method: "post"
|
|
491
|
+
},
|
|
492
|
+
fetcher: function fetcher(_ref) {
|
|
493
|
+
return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
494
|
+
var _item$quantity;
|
|
495
|
+
var item, fetch, provider, activeCart, updatedCart;
|
|
496
|
+
return _regenerator().w(function (_context) {
|
|
497
|
+
while (1) switch (_context.n) {
|
|
498
|
+
case 0:
|
|
499
|
+
item = _ref.input, fetch = _ref.fetch, provider = _ref.provider;
|
|
500
|
+
_context.n = 1;
|
|
501
|
+
return getActiveCart(fetch);
|
|
502
|
+
case 1:
|
|
503
|
+
activeCart = _context.v;
|
|
504
|
+
if (!(item.quantity && (!Number.isInteger(item.quantity) || item.quantity < 1) || !activeCart)) {
|
|
505
|
+
_context.n = 2;
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
return _context.a(2, undefined);
|
|
509
|
+
case 2:
|
|
510
|
+
_context.n = 3;
|
|
511
|
+
return fetch({
|
|
512
|
+
query: "carts",
|
|
513
|
+
method: "post",
|
|
514
|
+
variables: {
|
|
515
|
+
id: activeCart.id
|
|
516
|
+
},
|
|
517
|
+
body: {
|
|
518
|
+
version: activeCart.version,
|
|
519
|
+
actions: [{
|
|
520
|
+
action: "addLineItem",
|
|
521
|
+
variantId: +item.variantId,
|
|
522
|
+
productId: item.productId,
|
|
523
|
+
quantity: (_item$quantity = item.quantity) != null ? _item$quantity : 1
|
|
524
|
+
}]
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
case 3:
|
|
528
|
+
updatedCart = _context.v;
|
|
529
|
+
if (updatedCart.body) {
|
|
530
|
+
setCartId(updatedCart.body.id);
|
|
531
|
+
} else {
|
|
532
|
+
removeCartCookie();
|
|
533
|
+
}
|
|
534
|
+
return _context.a(2, updatedCart.body ? normalizeCart(updatedCart.body, provider.locale) : undefined);
|
|
535
|
+
}
|
|
536
|
+
}, _callee);
|
|
537
|
+
}))();
|
|
538
|
+
},
|
|
539
|
+
useHook: function useHook(_ref2) {
|
|
540
|
+
var fetch = _ref2.fetch;
|
|
541
|
+
return function () {
|
|
542
|
+
var _useCart = commerce.useCart(),
|
|
543
|
+
mutate = _useCart.mutate;
|
|
544
|
+
return React.useCallback(/*#__PURE__*/function () {
|
|
545
|
+
var _addItem = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(input) {
|
|
546
|
+
var data;
|
|
547
|
+
return _regenerator().w(function (_context2) {
|
|
548
|
+
while (1) switch (_context2.n) {
|
|
549
|
+
case 0:
|
|
550
|
+
_context2.n = 1;
|
|
551
|
+
return fetch({
|
|
552
|
+
input: input
|
|
553
|
+
});
|
|
554
|
+
case 1:
|
|
555
|
+
data = _context2.v;
|
|
556
|
+
_context2.n = 2;
|
|
557
|
+
return mutate(data, false);
|
|
558
|
+
case 2:
|
|
559
|
+
return _context2.a(2, data);
|
|
560
|
+
}
|
|
561
|
+
}, _callee2);
|
|
562
|
+
}));
|
|
563
|
+
function addItem(_x) {
|
|
564
|
+
return _addItem.apply(this, arguments);
|
|
565
|
+
}
|
|
566
|
+
return addItem;
|
|
567
|
+
}(), [fetch, mutate]);
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
|
|
572
|
+
var handler$2 = {
|
|
573
|
+
fetchOptions: {
|
|
574
|
+
query: "cart",
|
|
575
|
+
method: "post"
|
|
576
|
+
},
|
|
577
|
+
fetcher: function fetcher(_ref) {
|
|
578
|
+
return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
579
|
+
var itemId, fetch, provider, activeCart, updatedCart;
|
|
580
|
+
return _regenerator().w(function (_context) {
|
|
581
|
+
while (1) switch (_context.n) {
|
|
582
|
+
case 0:
|
|
583
|
+
itemId = _ref.input.itemId, fetch = _ref.fetch, provider = _ref.provider;
|
|
584
|
+
_context.n = 1;
|
|
585
|
+
return getActiveCart(fetch);
|
|
586
|
+
case 1:
|
|
587
|
+
activeCart = _context.v;
|
|
588
|
+
if (!(!itemId || !activeCart)) {
|
|
589
|
+
_context.n = 2;
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
return _context.a(2, undefined);
|
|
593
|
+
case 2:
|
|
594
|
+
_context.n = 3;
|
|
595
|
+
return fetch({
|
|
596
|
+
query: "carts",
|
|
597
|
+
method: "post",
|
|
598
|
+
variables: {
|
|
599
|
+
id: activeCart.id
|
|
600
|
+
},
|
|
601
|
+
body: {
|
|
602
|
+
version: activeCart.version,
|
|
603
|
+
actions: [{
|
|
604
|
+
action: "removeLineItem",
|
|
605
|
+
lineItemId: itemId
|
|
606
|
+
}]
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
case 3:
|
|
610
|
+
updatedCart = _context.v;
|
|
611
|
+
if (updatedCart.body) {
|
|
612
|
+
setCartId(updatedCart.body.id);
|
|
613
|
+
} else {
|
|
614
|
+
removeCartCookie();
|
|
615
|
+
}
|
|
616
|
+
return _context.a(2, normalizeCart(updatedCart.body, provider.locale));
|
|
617
|
+
}
|
|
618
|
+
}, _callee);
|
|
619
|
+
}))();
|
|
620
|
+
},
|
|
621
|
+
useHook: function useHook(_ref2) {
|
|
622
|
+
var fetch = _ref2.fetch;
|
|
623
|
+
return function (ctx) {
|
|
624
|
+
if (ctx === void 0) {
|
|
625
|
+
ctx = {};
|
|
626
|
+
}
|
|
627
|
+
var _ctx = ctx,
|
|
628
|
+
item = _ctx.item;
|
|
629
|
+
var _useCart = commerce.useCart(),
|
|
630
|
+
mutate = _useCart.mutate;
|
|
631
|
+
var removeItem = /*#__PURE__*/function () {
|
|
632
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(input) {
|
|
633
|
+
var _input$id;
|
|
634
|
+
var itemId, data;
|
|
635
|
+
return _regenerator().w(function (_context2) {
|
|
636
|
+
while (1) switch (_context2.n) {
|
|
637
|
+
case 0:
|
|
638
|
+
itemId = (_input$id = input == null ? void 0 : input.id) != null ? _input$id : item == null ? void 0 : item.id;
|
|
639
|
+
if (itemId) {
|
|
640
|
+
_context2.n = 1;
|
|
641
|
+
break;
|
|
642
|
+
}
|
|
643
|
+
throw new commerce.ValidationError({
|
|
644
|
+
message: "Invalid input used for this operation"
|
|
645
|
+
});
|
|
646
|
+
case 1:
|
|
647
|
+
_context2.n = 2;
|
|
648
|
+
return fetch({
|
|
649
|
+
input: {
|
|
650
|
+
itemId: itemId
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
case 2:
|
|
654
|
+
data = _context2.v;
|
|
655
|
+
_context2.n = 3;
|
|
656
|
+
return mutate(data, false);
|
|
657
|
+
case 3:
|
|
658
|
+
return _context2.a(2, data);
|
|
659
|
+
}
|
|
660
|
+
}, _callee2);
|
|
661
|
+
}));
|
|
662
|
+
return function removeItem(_x) {
|
|
663
|
+
return _ref3.apply(this, arguments);
|
|
664
|
+
};
|
|
665
|
+
}();
|
|
666
|
+
return React.useCallback(removeItem, [fetch, mutate]);
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
var handler$3 = {
|
|
672
|
+
fetchOptions: {
|
|
673
|
+
query: "cart",
|
|
674
|
+
method: "post"
|
|
675
|
+
},
|
|
676
|
+
fetcher: function fetcher(_ref) {
|
|
677
|
+
return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
678
|
+
var _item$quantity;
|
|
679
|
+
var _ref$input, item, itemId, fetch, provider, activeCart, updatedCart;
|
|
680
|
+
return _regenerator().w(function (_context) {
|
|
681
|
+
while (1) switch (_context.n) {
|
|
682
|
+
case 0:
|
|
683
|
+
_ref$input = _ref.input, item = _ref$input.item, itemId = _ref$input.itemId, fetch = _ref.fetch, provider = _ref.provider;
|
|
684
|
+
if (!Number.isInteger(item.quantity)) {
|
|
685
|
+
_context.n = 2;
|
|
686
|
+
break;
|
|
687
|
+
}
|
|
688
|
+
if (!(item.quantity < 1)) {
|
|
689
|
+
_context.n = 1;
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
return _context.a(2, handler$2.fetcher == null ? void 0 : handler$2.fetcher({
|
|
693
|
+
options: handler$2.fetchOptions,
|
|
694
|
+
input: {
|
|
695
|
+
itemId: itemId
|
|
696
|
+
},
|
|
697
|
+
fetch: fetch,
|
|
698
|
+
provider: provider
|
|
699
|
+
}));
|
|
700
|
+
case 1:
|
|
701
|
+
_context.n = 3;
|
|
702
|
+
break;
|
|
703
|
+
case 2:
|
|
704
|
+
if (!item.quantity) {
|
|
705
|
+
_context.n = 3;
|
|
706
|
+
break;
|
|
707
|
+
}
|
|
708
|
+
throw new commerce.ValidationError({
|
|
709
|
+
message: "The item quantity has to be a valid integer"
|
|
710
|
+
});
|
|
711
|
+
case 3:
|
|
712
|
+
_context.n = 4;
|
|
713
|
+
return getActiveCart(fetch);
|
|
714
|
+
case 4:
|
|
715
|
+
activeCart = _context.v;
|
|
716
|
+
if (!(item.quantity && (!Number.isInteger(item.quantity) || item.quantity < 1) || !activeCart)) {
|
|
717
|
+
_context.n = 5;
|
|
718
|
+
break;
|
|
719
|
+
}
|
|
720
|
+
return _context.a(2, undefined);
|
|
721
|
+
case 5:
|
|
722
|
+
_context.n = 6;
|
|
723
|
+
return fetch({
|
|
724
|
+
query: "carts",
|
|
725
|
+
method: "post",
|
|
726
|
+
variables: {
|
|
727
|
+
id: activeCart.id
|
|
728
|
+
},
|
|
729
|
+
body: {
|
|
730
|
+
version: activeCart.version,
|
|
731
|
+
actions: [{
|
|
732
|
+
action: "changeLineItemQuantity",
|
|
733
|
+
lineItemId: itemId,
|
|
734
|
+
quantity: (_item$quantity = item.quantity) != null ? _item$quantity : 1
|
|
735
|
+
}]
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
case 6:
|
|
739
|
+
updatedCart = _context.v;
|
|
740
|
+
if (updatedCart.body) {
|
|
741
|
+
setCartId(updatedCart.body.id);
|
|
742
|
+
} else {
|
|
743
|
+
removeCartCookie();
|
|
744
|
+
}
|
|
745
|
+
return _context.a(2, updatedCart.body ? normalizeCart(updatedCart.body, provider.locale) : undefined);
|
|
746
|
+
}
|
|
747
|
+
}, _callee);
|
|
748
|
+
}))();
|
|
749
|
+
},
|
|
750
|
+
useHook: function useHook(_ref2) {
|
|
751
|
+
var fetch = _ref2.fetch;
|
|
752
|
+
return function (ctx) {
|
|
753
|
+
var _ctx$wait;
|
|
754
|
+
if (ctx === void 0) {
|
|
755
|
+
ctx = {};
|
|
756
|
+
}
|
|
757
|
+
var _ctx = ctx,
|
|
758
|
+
item = _ctx.item;
|
|
759
|
+
var _useCart = commerce.useCart(),
|
|
760
|
+
mutate = _useCart.mutate;
|
|
761
|
+
return React.useCallback(debounce(/*#__PURE__*/function () {
|
|
762
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(input) {
|
|
763
|
+
var _input$id;
|
|
764
|
+
var itemId, data;
|
|
765
|
+
return _regenerator().w(function (_context2) {
|
|
766
|
+
while (1) switch (_context2.n) {
|
|
767
|
+
case 0:
|
|
768
|
+
itemId = (_input$id = input.id) != null ? _input$id : item == null ? void 0 : item.id;
|
|
769
|
+
if (!(!itemId || input.quantity == null)) {
|
|
770
|
+
_context2.n = 1;
|
|
771
|
+
break;
|
|
772
|
+
}
|
|
773
|
+
throw new commerce.ValidationError({
|
|
774
|
+
message: "Invalid input used for this operation"
|
|
775
|
+
});
|
|
776
|
+
case 1:
|
|
777
|
+
_context2.n = 2;
|
|
778
|
+
return fetch({
|
|
779
|
+
input: {
|
|
780
|
+
item: {
|
|
781
|
+
quantity: input.quantity
|
|
782
|
+
},
|
|
783
|
+
itemId: itemId
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
case 2:
|
|
787
|
+
data = _context2.v;
|
|
788
|
+
_context2.n = 3;
|
|
789
|
+
return mutate(data, false);
|
|
790
|
+
case 3:
|
|
791
|
+
return _context2.a(2, data);
|
|
792
|
+
}
|
|
793
|
+
}, _callee2);
|
|
794
|
+
}));
|
|
795
|
+
return function (_x) {
|
|
796
|
+
return _ref3.apply(this, arguments);
|
|
797
|
+
};
|
|
798
|
+
}(), (_ctx$wait = ctx.wait) != null ? _ctx$wait : 500), [fetch, mutate]);
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
|
|
803
|
+
var initCommercetoolsSDKClient = function initCommercetoolsSDKClient(creds) {
|
|
804
|
+
var scopes = ["manage_project:" + creds.projectKey];
|
|
805
|
+
// Configure authMiddlewareOptions
|
|
806
|
+
var authMiddlewareOptions = {
|
|
807
|
+
host: "https://auth." + creds.region + ".commercetools.com",
|
|
808
|
+
projectKey: creds.clientSecret,
|
|
809
|
+
credentials: {
|
|
810
|
+
clientId: creds.clientId,
|
|
811
|
+
clientSecret: creds.clientSecret
|
|
812
|
+
},
|
|
813
|
+
scopes: scopes,
|
|
814
|
+
fetch: fetch
|
|
815
|
+
};
|
|
816
|
+
// Configure httpMiddlewareOptions
|
|
817
|
+
var httpMiddlewareOptions = {
|
|
818
|
+
host: "https://api." + creds.region + ".commercetools.com",
|
|
819
|
+
fetch: fetch
|
|
820
|
+
};
|
|
821
|
+
// Export the ClientBuilder
|
|
822
|
+
return new sdkClientV2.ClientBuilder().withProjectKey(creds.projectKey).withClientCredentialsFlow(authMiddlewareOptions).withHttpMiddleware(httpMiddlewareOptions).build();
|
|
823
|
+
};
|
|
824
|
+
|
|
825
|
+
var getFetcher = function getFetcher(creds) {
|
|
826
|
+
var client = initCommercetoolsSDKClient(creds);
|
|
827
|
+
var apiRoot = platformSdk.createApiBuilderFromCtpClient(client).withProjectKey({
|
|
828
|
+
projectKey: creds.projectKey
|
|
829
|
+
});
|
|
830
|
+
return /*#__PURE__*/function () {
|
|
831
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
|
|
832
|
+
var method, variables, query, body, queryBuilder;
|
|
833
|
+
return _regenerator().w(function (_context) {
|
|
834
|
+
while (1) switch (_context.n) {
|
|
835
|
+
case 0:
|
|
836
|
+
method = _ref.method, variables = _ref.variables, query = _ref.query, body = _ref.body;
|
|
837
|
+
queryBuilder = apiRoot;
|
|
838
|
+
if (query) {
|
|
839
|
+
queryBuilder = queryBuilder[query]();
|
|
840
|
+
}
|
|
841
|
+
if (variables != null && variables.id) {
|
|
842
|
+
queryBuilder = queryBuilder.withId({
|
|
843
|
+
ID: variables.id
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
if (variables != null && variables.search) {
|
|
847
|
+
queryBuilder = queryBuilder.search();
|
|
848
|
+
}
|
|
849
|
+
_context.n = 1;
|
|
850
|
+
return queryBuilder[method]({
|
|
851
|
+
body: body,
|
|
852
|
+
queryArgs: _extends({
|
|
853
|
+
expand: variables == null ? void 0 : variables.expand,
|
|
854
|
+
limit: variables == null ? void 0 : variables.limit
|
|
855
|
+
}, variables != null && variables.sort ? {
|
|
856
|
+
sort: variables.sort
|
|
857
|
+
} : {}, variables != null && variables.search ? variables.search : {}, variables != null && variables.filters ? {
|
|
858
|
+
filter: variables.filters
|
|
859
|
+
} : {}, variables != null && variables.where ? {
|
|
860
|
+
where: variables.where
|
|
861
|
+
} : {})
|
|
862
|
+
}).execute();
|
|
863
|
+
case 1:
|
|
864
|
+
return _context.a(2, _context.v);
|
|
865
|
+
}
|
|
866
|
+
}, _callee);
|
|
867
|
+
}));
|
|
868
|
+
return function (_x) {
|
|
869
|
+
return _ref2.apply(this, arguments);
|
|
870
|
+
};
|
|
871
|
+
}();
|
|
872
|
+
};
|
|
873
|
+
|
|
874
|
+
var handler$4 = {
|
|
875
|
+
fetchOptions: {
|
|
876
|
+
query: "productProjections",
|
|
877
|
+
method: "get"
|
|
878
|
+
},
|
|
879
|
+
fetcher: function fetcher(_ref) {
|
|
880
|
+
return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
881
|
+
var input, options, fetch, provider, id, product;
|
|
882
|
+
return _regenerator().w(function (_context) {
|
|
883
|
+
while (1) switch (_context.n) {
|
|
884
|
+
case 0:
|
|
885
|
+
input = _ref.input, options = _ref.options, fetch = _ref.fetch, provider = _ref.provider;
|
|
886
|
+
id = input.id;
|
|
887
|
+
if (id) {
|
|
888
|
+
_context.n = 1;
|
|
889
|
+
break;
|
|
890
|
+
}
|
|
891
|
+
return _context.a(2, null);
|
|
892
|
+
case 1:
|
|
893
|
+
_context.n = 2;
|
|
894
|
+
return fetch(_extends({}, options, {
|
|
895
|
+
variables: {
|
|
896
|
+
id: id
|
|
897
|
+
}
|
|
898
|
+
}));
|
|
899
|
+
case 2:
|
|
900
|
+
product = _context.v;
|
|
901
|
+
return _context.a(2, product.body ? normalizeProduct(product.body, provider.locale) : null);
|
|
902
|
+
}
|
|
903
|
+
}, _callee);
|
|
904
|
+
}))();
|
|
905
|
+
},
|
|
906
|
+
useHook: function useHook(_ref2) {
|
|
907
|
+
var useData = _ref2.useData;
|
|
908
|
+
return function (input) {
|
|
909
|
+
if (input === void 0) {
|
|
910
|
+
input = {};
|
|
911
|
+
}
|
|
912
|
+
return useData({
|
|
913
|
+
input: [['id', input.id]],
|
|
914
|
+
swrOptions: _extends({
|
|
915
|
+
revalidateOnFocus: false
|
|
916
|
+
}, input.swrOptions)
|
|
917
|
+
});
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
};
|
|
921
|
+
|
|
922
|
+
var handler$5 = {
|
|
923
|
+
fetchOptions: {
|
|
924
|
+
method: "get",
|
|
925
|
+
query: "productProjections"
|
|
926
|
+
},
|
|
927
|
+
fetcher: function fetcher(_ref) {
|
|
928
|
+
return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
929
|
+
var _search;
|
|
930
|
+
var input, options, fetch, provider, search, categoryId, sort, count, response;
|
|
931
|
+
return _regenerator().w(function (_context) {
|
|
932
|
+
while (1) switch (_context.n) {
|
|
933
|
+
case 0:
|
|
934
|
+
input = _ref.input, options = _ref.options, fetch = _ref.fetch, provider = _ref.provider;
|
|
935
|
+
search = input.search, categoryId = input.categoryId, sort = input.sort, count = input.count;
|
|
936
|
+
_context.n = 1;
|
|
937
|
+
return fetch(_extends({}, options, {
|
|
938
|
+
variables: _extends({
|
|
939
|
+
expand: ["masterData.current"],
|
|
940
|
+
sort: getSortVariables(sort),
|
|
941
|
+
limit: count
|
|
942
|
+
}, search ?
|
|
943
|
+
// eslint-disable-next-line
|
|
944
|
+
{
|
|
945
|
+
search: (_search = {}, _search["text." + (provider == null ? void 0 : provider.locale)] = search, _search)
|
|
946
|
+
} : {}, categoryId ? {
|
|
947
|
+
filters: "categories.id: subtree(\"" + categoryId + "\")"
|
|
948
|
+
} : {})
|
|
949
|
+
}));
|
|
950
|
+
case 1:
|
|
951
|
+
response = _context.v;
|
|
952
|
+
return _context.a(2, {
|
|
953
|
+
products: response.body.results.map(function (product) {
|
|
954
|
+
return normalizeProduct(product, provider.locale);
|
|
955
|
+
}),
|
|
956
|
+
found: response.body.count > 0
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
}, _callee);
|
|
960
|
+
}))();
|
|
961
|
+
},
|
|
962
|
+
useHook: function useHook(_ref2) {
|
|
963
|
+
var useData = _ref2.useData;
|
|
964
|
+
return function (input) {
|
|
965
|
+
if (input === void 0) {
|
|
966
|
+
input = {};
|
|
967
|
+
}
|
|
968
|
+
return useData({
|
|
969
|
+
input: [["search", input.search], ["categoryId", input.categoryId], ["brandId", input.brandId], ["sort", input.sort], ["locale", input.locale], ["count", input.count]],
|
|
970
|
+
swrOptions: _extends({
|
|
971
|
+
revalidateOnFocus: false
|
|
972
|
+
}, input.swrOptions)
|
|
973
|
+
});
|
|
974
|
+
};
|
|
975
|
+
}
|
|
976
|
+
};
|
|
977
|
+
|
|
978
|
+
var handler$6 = {
|
|
979
|
+
fetchOptions: {
|
|
980
|
+
query: ""
|
|
981
|
+
},
|
|
982
|
+
fetcher: function fetcher(_ref) {
|
|
983
|
+
return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
984
|
+
return _regenerator().w(function (_context) {
|
|
985
|
+
while (1) switch (_context.n) {
|
|
986
|
+
case 0:
|
|
987
|
+
return _context.a(2, null);
|
|
988
|
+
}
|
|
989
|
+
}, _callee);
|
|
990
|
+
}))();
|
|
991
|
+
},
|
|
992
|
+
useHook: function useHook(_ref2) {
|
|
993
|
+
var useData = _ref2.useData;
|
|
994
|
+
return function (input) {
|
|
995
|
+
var response = useData({
|
|
996
|
+
swrOptions: _extends({
|
|
997
|
+
revalidateOnFocus: false
|
|
998
|
+
}, input == null ? void 0 : input.swrOptions)
|
|
999
|
+
});
|
|
1000
|
+
return React.useMemo(function () {
|
|
1001
|
+
return Object.create(response, {
|
|
1002
|
+
isEmpty: {
|
|
1003
|
+
get: function get() {
|
|
1004
|
+
var _response$data$length, _response$data;
|
|
1005
|
+
return ((_response$data$length = (_response$data = response.data) == null ? void 0 : _response$data.length) != null ? _response$data$length : 0) <= 0;
|
|
1006
|
+
},
|
|
1007
|
+
enumerable: true
|
|
1008
|
+
}
|
|
1009
|
+
});
|
|
1010
|
+
}, [response]);
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
|
|
1015
|
+
var handler$7 = {
|
|
1016
|
+
fetchOptions: {
|
|
1017
|
+
query: "categories",
|
|
1018
|
+
method: "get"
|
|
1019
|
+
},
|
|
1020
|
+
fetcher: function fetcher(_ref) {
|
|
1021
|
+
return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
1022
|
+
var input, options, fetch, provider, categoryId, categories, category;
|
|
1023
|
+
return _regenerator().w(function (_context) {
|
|
1024
|
+
while (1) switch (_context.n) {
|
|
1025
|
+
case 0:
|
|
1026
|
+
input = _ref.input, options = _ref.options, fetch = _ref.fetch, provider = _ref.provider;
|
|
1027
|
+
categoryId = input.categoryId;
|
|
1028
|
+
if (categoryId) {
|
|
1029
|
+
_context.n = 2;
|
|
1030
|
+
break;
|
|
1031
|
+
}
|
|
1032
|
+
_context.n = 1;
|
|
1033
|
+
return fetch(_extends({}, options));
|
|
1034
|
+
case 1:
|
|
1035
|
+
categories = _context.v;
|
|
1036
|
+
return _context.a(2, categories.body ? categories.body.results.map(function (category) {
|
|
1037
|
+
return normalizeCategory(category, provider.locale);
|
|
1038
|
+
}) : []);
|
|
1039
|
+
case 2:
|
|
1040
|
+
_context.n = 3;
|
|
1041
|
+
return fetch(_extends({}, options, {
|
|
1042
|
+
variables: _extends({}, categoryId ? {
|
|
1043
|
+
id: categoryId
|
|
1044
|
+
} : {})
|
|
1045
|
+
}));
|
|
1046
|
+
case 3:
|
|
1047
|
+
category = _context.v;
|
|
1048
|
+
return _context.a(2, category.body ? [normalizeCategory(category.body, provider.locale)] : []);
|
|
1049
|
+
case 4:
|
|
1050
|
+
return _context.a(2);
|
|
1051
|
+
}
|
|
1052
|
+
}, _callee);
|
|
1053
|
+
}))();
|
|
1054
|
+
},
|
|
1055
|
+
useHook: function useHook(_ref2) {
|
|
1056
|
+
var useData = _ref2.useData;
|
|
1057
|
+
return function (input) {
|
|
1058
|
+
var response = useData({
|
|
1059
|
+
input: [["categoryId", input == null ? void 0 : input.categoryId]],
|
|
1060
|
+
swrOptions: _extends({
|
|
1061
|
+
revalidateOnFocus: false
|
|
1062
|
+
}, input == null ? void 0 : input.swrOptions)
|
|
1063
|
+
});
|
|
1064
|
+
return React.useMemo(function () {
|
|
1065
|
+
return Object.create(response, {
|
|
1066
|
+
isEmpty: {
|
|
1067
|
+
get: function get() {
|
|
1068
|
+
var _response$data$length, _response$data;
|
|
1069
|
+
return ((_response$data$length = (_response$data = response.data) == null ? void 0 : _response$data.length) != null ? _response$data$length : 0) <= 0;
|
|
1070
|
+
},
|
|
1071
|
+
enumerable: true
|
|
1072
|
+
}
|
|
1073
|
+
});
|
|
1074
|
+
}, [response]);
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
};
|
|
1078
|
+
|
|
1079
|
+
var getCommercetoolsProvider = function getCommercetoolsProvider(creds, locale) {
|
|
1080
|
+
return {
|
|
1081
|
+
locale: locale,
|
|
1082
|
+
cartCookie: COMMERCETOOLS_CART_COOKIE,
|
|
1083
|
+
cart: {
|
|
1084
|
+
useCart: handler,
|
|
1085
|
+
useAddItem: handler$1,
|
|
1086
|
+
useRemoveItem: handler$2,
|
|
1087
|
+
useUpdateItem: handler$3
|
|
1088
|
+
},
|
|
1089
|
+
fetcher: getFetcher(creds),
|
|
1090
|
+
products: {
|
|
1091
|
+
useSearch: handler$5,
|
|
1092
|
+
useProduct: handler$4
|
|
1093
|
+
},
|
|
1094
|
+
site: {
|
|
1095
|
+
useCategories: handler$7,
|
|
1096
|
+
useBrands: handler$6
|
|
1097
|
+
}
|
|
1098
|
+
};
|
|
1099
|
+
};
|
|
1100
|
+
|
|
1101
|
+
var useCommerce = function useCommerce() {
|
|
1102
|
+
return commerce.useCommerce();
|
|
1103
|
+
};
|
|
1104
|
+
var getCommerceProvider = function getCommerceProvider(creds, locale) {
|
|
1105
|
+
return commerce.getCommerceProvider(getCommercetoolsProvider(creds, locale));
|
|
1106
|
+
};
|
|
1107
|
+
|
|
1108
|
+
var globalContextName = "daouy-commerce-commercetools-provider";
|
|
1109
|
+
var commerceProviderMeta = /*#__PURE__*/_extends({
|
|
1110
|
+
name: globalContextName,
|
|
1111
|
+
displayName: "Commercetools Provider",
|
|
1112
|
+
props: {
|
|
1113
|
+
projectKey: {
|
|
1114
|
+
type: "string",
|
|
1115
|
+
defaultValue: "daouy-demo"
|
|
1116
|
+
},
|
|
1117
|
+
clientId: {
|
|
1118
|
+
type: "string",
|
|
1119
|
+
defaultValue: "B4hmK61xvz5LvdSDtsFmcflM"
|
|
1120
|
+
},
|
|
1121
|
+
clientSecret: {
|
|
1122
|
+
type: "string",
|
|
1123
|
+
defaultValue: "KhzjcjSu1Oul4aomSmOsLZOCZKbvfHqx"
|
|
1124
|
+
},
|
|
1125
|
+
region: {
|
|
1126
|
+
type: "choice",
|
|
1127
|
+
options: ["us-central1.gcp", "us-east-2.aws", "europe-west1.gcp", "eu-central-1.aws", "australia-southeast1.gcp"],
|
|
1128
|
+
defaultValue: "us-central1.gcp"
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
}, {
|
|
1132
|
+
globalActions: commerce.globalActionsRegistrations
|
|
1133
|
+
}, {
|
|
1134
|
+
importPath: "@daouypkgs/commercetools",
|
|
1135
|
+
importName: "CommerceProviderComponent"
|
|
1136
|
+
});
|
|
1137
|
+
function CommerceProviderComponent(props) {
|
|
1138
|
+
var children = props.children,
|
|
1139
|
+
projectKey = props.projectKey,
|
|
1140
|
+
clientId = props.clientId,
|
|
1141
|
+
clientSecret = props.clientSecret,
|
|
1142
|
+
region = props.region;
|
|
1143
|
+
var creds = React__default.useMemo(function () {
|
|
1144
|
+
return {
|
|
1145
|
+
projectKey: projectKey,
|
|
1146
|
+
clientId: clientId,
|
|
1147
|
+
clientSecret: clientSecret,
|
|
1148
|
+
region: region
|
|
1149
|
+
};
|
|
1150
|
+
}, [projectKey, clientId, clientSecret, region]);
|
|
1151
|
+
var _useDaouyQueryData = query.useDaouyQueryData(JSON.stringify({
|
|
1152
|
+
creds: creds
|
|
1153
|
+
}) + "locale", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
1154
|
+
var fetcher, project;
|
|
1155
|
+
return _regenerator().w(function (_context) {
|
|
1156
|
+
while (1) switch (_context.n) {
|
|
1157
|
+
case 0:
|
|
1158
|
+
fetcher = getFetcher(creds);
|
|
1159
|
+
_context.n = 1;
|
|
1160
|
+
return fetcher({
|
|
1161
|
+
method: "get"
|
|
1162
|
+
});
|
|
1163
|
+
case 1:
|
|
1164
|
+
project = _context.v;
|
|
1165
|
+
return _context.a(2, project.body ? project.body.languages[0] : undefined);
|
|
1166
|
+
}
|
|
1167
|
+
}, _callee);
|
|
1168
|
+
}))),
|
|
1169
|
+
locale = _useDaouyQueryData.data,
|
|
1170
|
+
error = _useDaouyQueryData.error,
|
|
1171
|
+
isLoading = _useDaouyQueryData.isLoading;
|
|
1172
|
+
var CommerceProvider = React__default.useMemo(function () {
|
|
1173
|
+
return getCommerceProvider(creds, locale != null ? locale : "");
|
|
1174
|
+
}, [creds, locale]);
|
|
1175
|
+
if (isLoading) {
|
|
1176
|
+
return null;
|
|
1177
|
+
} else if (error || !locale) {
|
|
1178
|
+
throw new Error(error ? error.message : "Project language not found");
|
|
1179
|
+
}
|
|
1180
|
+
return React__default.createElement(CommerceProvider, null, React__default.createElement(commerce.CartActionsProvider, {
|
|
1181
|
+
globalContextName: globalContextName
|
|
1182
|
+
}, children));
|
|
1183
|
+
}
|
|
1184
|
+
function registerCommerceProvider(loader, customCommerceProviderMeta) {
|
|
1185
|
+
var doRegisterComponent = function doRegisterComponent() {
|
|
1186
|
+
return loader ? loader.registerGlobalContext.apply(loader, arguments) : registerGlobalContext.apply(void 0, arguments);
|
|
1187
|
+
};
|
|
1188
|
+
doRegisterComponent(CommerceProviderComponent, customCommerceProviderMeta != null ? customCommerceProviderMeta : commerceProviderMeta);
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
function registerAll(loader) {
|
|
1192
|
+
registerCommerceProvider(loader);
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
exports.CommerceProviderComponent = CommerceProviderComponent;
|
|
1196
|
+
exports.commerceProviderMeta = commerceProviderMeta;
|
|
1197
|
+
exports.getCommerceProvider = getCommerceProvider;
|
|
1198
|
+
exports.registerAll = registerAll;
|
|
1199
|
+
exports.registerCommerceProvider = registerCommerceProvider;
|
|
1200
|
+
exports.useCommerce = useCommerce;
|
|
1201
|
+
//# sourceMappingURL=commerce-commercetools.cjs.development.js.map
|