@konfiqo/storefront-sdk 0.9.0 → 0.10.0
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 +114 -75
- package/dist/index.cjs +127 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +140 -8
- package/dist/index.d.ts +140 -8
- package/dist/index.js +127 -27
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,85 @@
|
|
|
1
|
+
// src/normalize.ts
|
|
2
|
+
var isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
3
|
+
var num = (v) => typeof v === "number" ? v : 0;
|
|
4
|
+
var text = (v) => typeof v === "string" ? v : "";
|
|
5
|
+
var REASONS = /* @__PURE__ */ new Set([
|
|
6
|
+
"expired",
|
|
7
|
+
"not_started",
|
|
8
|
+
"min_subtotal",
|
|
9
|
+
"first_order_only",
|
|
10
|
+
"used_up",
|
|
11
|
+
"not_combinable_better",
|
|
12
|
+
"unknown"
|
|
13
|
+
]);
|
|
14
|
+
function normalizeReason(v) {
|
|
15
|
+
return typeof v === "string" && REASONS.has(v) ? v : null;
|
|
16
|
+
}
|
|
17
|
+
function normalizeCampaigns(v) {
|
|
18
|
+
if (!Array.isArray(v)) return [];
|
|
19
|
+
return v.filter(isObject).map((c) => ({
|
|
20
|
+
...typeof c.campaignId === "string" && { campaignId: c.campaignId },
|
|
21
|
+
buyerLabel: text(c.buyerLabel),
|
|
22
|
+
discountMinor: num(c.discountMinor),
|
|
23
|
+
shippingDiscountMinor: num(c.shippingDiscountMinor)
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
function normalizePending(v) {
|
|
27
|
+
if (!Array.isArray(v)) return [];
|
|
28
|
+
return v.filter(isObject).map((c) => ({
|
|
29
|
+
...typeof c.campaignId === "string" && { campaignId: c.campaignId },
|
|
30
|
+
buyerLabel: text(c.buyerLabel)
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
function normalizeCartLine(l) {
|
|
34
|
+
return { ...l, discountMinor: num(l.discountMinor) };
|
|
35
|
+
}
|
|
36
|
+
function normalizeCart(c) {
|
|
37
|
+
const r = c;
|
|
38
|
+
return {
|
|
39
|
+
...c,
|
|
40
|
+
lines: (Array.isArray(r.lines) ? r.lines : []).map(
|
|
41
|
+
(l) => normalizeCartLine(l)
|
|
42
|
+
),
|
|
43
|
+
discountMinor: num(r.discountMinor),
|
|
44
|
+
shippingDiscountMinor: num(r.shippingDiscountMinor),
|
|
45
|
+
appliedCampaigns: normalizeCampaigns(r.appliedCampaigns),
|
|
46
|
+
pendingCampaigns: normalizePending(r.pendingCampaigns),
|
|
47
|
+
couponCode: typeof r.couponCode === "string" ? r.couponCode : null,
|
|
48
|
+
codeReason: normalizeReason(r.codeReason)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function normalizeOrder(o) {
|
|
52
|
+
const r = o;
|
|
53
|
+
return {
|
|
54
|
+
...o,
|
|
55
|
+
discountMinor: num(r.discountMinor),
|
|
56
|
+
shippingDiscountMinor: num(r.shippingDiscountMinor),
|
|
57
|
+
appliedCampaigns: normalizeCampaigns(r.appliedCampaigns),
|
|
58
|
+
lines: (Array.isArray(r.lines) ? r.lines : []).map((l) => ({
|
|
59
|
+
...l,
|
|
60
|
+
discountMinor: num(l.discountMinor)
|
|
61
|
+
}))
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function normalizeAgreements(a) {
|
|
65
|
+
const q = a.quote;
|
|
66
|
+
const quote = isObject(q) ? {
|
|
67
|
+
subtotalMinor: num(q.subtotalMinor),
|
|
68
|
+
discountMinor: num(q.discountMinor),
|
|
69
|
+
shippingMinor: num(q.shippingMinor),
|
|
70
|
+
shippingDiscountMinor: num(q.shippingDiscountMinor),
|
|
71
|
+
totalMinor: num(q.totalMinor),
|
|
72
|
+
appliedCampaigns: normalizeCampaigns(q.appliedCampaigns),
|
|
73
|
+
pendingCampaigns: normalizePending(q.pendingCampaigns),
|
|
74
|
+
codeReason: normalizeReason(q.codeReason)
|
|
75
|
+
} : null;
|
|
76
|
+
return { ...a, quote };
|
|
77
|
+
}
|
|
78
|
+
function normalizeStore(s) {
|
|
79
|
+
const r = s;
|
|
80
|
+
return { ...s, hasCodeCampaigns: r.hasCodeCampaigns !== false };
|
|
81
|
+
}
|
|
82
|
+
|
|
1
83
|
// src/errors.ts
|
|
2
84
|
var API_CODES = /* @__PURE__ */ new Set([
|
|
3
85
|
"store_not_found",
|
|
@@ -22,6 +104,7 @@ var API_CODES = /* @__PURE__ */ new Set([
|
|
|
22
104
|
"agreements_changed",
|
|
23
105
|
"cart_incomplete",
|
|
24
106
|
"cart_changed",
|
|
107
|
+
"coupon_invalid",
|
|
25
108
|
"invalid_personalization",
|
|
26
109
|
"out_of_stock",
|
|
27
110
|
"amount_out_of_range",
|
|
@@ -79,7 +162,7 @@ var StorefrontError = class extends Error {
|
|
|
79
162
|
function isStorefrontError(e) {
|
|
80
163
|
return e instanceof StorefrontError || typeof e === "object" && e !== null && e[ERROR_BRAND] === true;
|
|
81
164
|
}
|
|
82
|
-
var
|
|
165
|
+
var isObject2 = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
83
166
|
function parseRetryAfter(value) {
|
|
84
167
|
if (value === null) return void 0;
|
|
85
168
|
const trimmed = value.trim();
|
|
@@ -89,7 +172,7 @@ function parseRetryAfter(value) {
|
|
|
89
172
|
return Math.max(0, Math.ceil((at - Date.now()) / 1e3));
|
|
90
173
|
}
|
|
91
174
|
function errorFromResponse(status, body, headers) {
|
|
92
|
-
const b =
|
|
175
|
+
const b = isObject2(body) ? body : {};
|
|
93
176
|
const message = typeof b.message === "string" && b.message !== "" ? b.message : `Storefront API request failed (HTTP ${status})`;
|
|
94
177
|
const error = typeof b.error === "string" ? b.error : null;
|
|
95
178
|
if (status === 429 && (error === null || !API_CODES.has(error))) {
|
|
@@ -122,11 +205,20 @@ function apiError(code, status, message, b) {
|
|
|
122
205
|
switch (code) {
|
|
123
206
|
case "cart_changed":
|
|
124
207
|
return new StorefrontError(code, status, message, {
|
|
125
|
-
lines: arr(b.lines),
|
|
208
|
+
lines: arr(b.lines).map(normalizeCartLine),
|
|
126
209
|
subtotalMinor: n(b.subtotalMinor),
|
|
210
|
+
discountMinor: n(b.discountMinor),
|
|
127
211
|
shippingMinor: n(b.shippingMinor),
|
|
212
|
+
shippingDiscountMinor: n(b.shippingDiscountMinor),
|
|
128
213
|
totalMinor: n(b.totalMinor),
|
|
129
|
-
shippingOptions: arr(b.shippingOptions)
|
|
214
|
+
shippingOptions: arr(b.shippingOptions),
|
|
215
|
+
appliedCampaigns: normalizeCampaigns(b.appliedCampaigns),
|
|
216
|
+
pendingCampaigns: normalizePending(b.pendingCampaigns),
|
|
217
|
+
codeReason: normalizeReason(b.codeReason)
|
|
218
|
+
});
|
|
219
|
+
case "coupon_invalid":
|
|
220
|
+
return new StorefrontError(code, status, message, {
|
|
221
|
+
reason: b.reason === "used_up" ? "used_up" : "unknown"
|
|
130
222
|
});
|
|
131
223
|
case "invalid_personalization":
|
|
132
224
|
return new StorefrontError(
|
|
@@ -291,26 +383,26 @@ function createStorefrontClient(options) {
|
|
|
291
383
|
callerSignal?.addEventListener("abort", onCallerAbort, { once: true });
|
|
292
384
|
try {
|
|
293
385
|
let res;
|
|
294
|
-
let
|
|
386
|
+
let text2;
|
|
295
387
|
try {
|
|
296
388
|
res = await Promise.race([doFetch(url, init), deadline]);
|
|
297
|
-
|
|
389
|
+
text2 = await Promise.race([res.text(), deadline]);
|
|
298
390
|
} catch (e) {
|
|
299
391
|
if (e instanceof StorefrontError) throw e;
|
|
300
392
|
throw transportError(e, timedOut, timeoutMs);
|
|
301
393
|
}
|
|
302
394
|
let parsed;
|
|
303
395
|
let isJson = false;
|
|
304
|
-
if (
|
|
396
|
+
if (text2 !== "") {
|
|
305
397
|
try {
|
|
306
|
-
parsed = JSON.parse(
|
|
398
|
+
parsed = JSON.parse(text2);
|
|
307
399
|
isJson = true;
|
|
308
400
|
} catch {
|
|
309
401
|
parsed = void 0;
|
|
310
402
|
}
|
|
311
403
|
}
|
|
312
404
|
if (!res.ok) throw errorFromResponse(res.status, parsed, res.headers);
|
|
313
|
-
if (!isJson && flags.emptyOk &&
|
|
405
|
+
if (!isJson && flags.emptyOk && text2 === "") return void 0;
|
|
314
406
|
if (!isJson) {
|
|
315
407
|
throw new StorefrontError(
|
|
316
408
|
"invalid_response",
|
|
@@ -329,10 +421,12 @@ function createStorefrontClient(options) {
|
|
|
329
421
|
const acct = { credentials: true };
|
|
330
422
|
const send = (method, path, body, o, extra = {}, emptyOk = false) => request(method, () => path, body, o, extra, { ...acct, emptyOk });
|
|
331
423
|
const cart = (id) => `/v1/carts/${seg(id)}`;
|
|
424
|
+
const cartRequest = async (method, path, body, o) => normalizeCart(await request(method, path, body, o));
|
|
425
|
+
const cartGet = (id, o) => cartRequest("GET", () => cart(id), void 0, o);
|
|
332
426
|
return {
|
|
333
427
|
getContent: (key, o) => get(() => `/v1/content/${seg(key)}`, o),
|
|
334
428
|
store: {
|
|
335
|
-
get: (o) => get(() => "/v1/store", o)
|
|
429
|
+
get: async (o) => normalizeStore(await get(() => "/v1/store", o))
|
|
336
430
|
},
|
|
337
431
|
blog: {
|
|
338
432
|
list: (query = {}, o) => get(() => {
|
|
@@ -363,15 +457,15 @@ function createStorefrontClient(options) {
|
|
|
363
457
|
get: (slug, o) => get(() => `/v1/products/${seg(slug)}`, o)
|
|
364
458
|
},
|
|
365
459
|
carts: {
|
|
366
|
-
create: (o) =>
|
|
460
|
+
create: (o) => cartRequest("POST", () => "/v1/carts", void 0, o),
|
|
367
461
|
recover: (token, o) => request(
|
|
368
462
|
"POST",
|
|
369
463
|
() => "/v1/carts/recover",
|
|
370
464
|
{ token },
|
|
371
465
|
o
|
|
372
466
|
),
|
|
373
|
-
get: (cartId, o) =>
|
|
374
|
-
addLine: (cartId, input, o) =>
|
|
467
|
+
get: (cartId, o) => cartGet(cartId, o),
|
|
468
|
+
addLine: (cartId, input, o) => cartRequest(
|
|
375
469
|
"POST",
|
|
376
470
|
() => `${cart(cartId)}/lines`,
|
|
377
471
|
{
|
|
@@ -383,32 +477,36 @@ function createStorefrontClient(options) {
|
|
|
383
477
|
},
|
|
384
478
|
o
|
|
385
479
|
),
|
|
386
|
-
updateLine: (cartId, lineId, input, o) =>
|
|
480
|
+
updateLine: (cartId, lineId, input, o) => cartRequest(
|
|
387
481
|
"PATCH",
|
|
388
482
|
() => `${cart(cartId)}/lines/${seg(lineId)}`,
|
|
389
483
|
{ quantity: input.quantity },
|
|
390
484
|
o
|
|
391
485
|
),
|
|
392
|
-
removeLine: (cartId, lineId, o) =>
|
|
486
|
+
removeLine: (cartId, lineId, o) => cartRequest(
|
|
393
487
|
"DELETE",
|
|
394
488
|
() => `${cart(cartId)}/lines/${seg(lineId)}`,
|
|
395
489
|
void 0,
|
|
396
490
|
o
|
|
397
491
|
),
|
|
398
|
-
|
|
399
|
-
|
|
492
|
+
applyCode: (cartId, code, o) => cartRequest("PUT", () => `${cart(cartId)}/coupon`, { code }, o),
|
|
493
|
+
removeCode: (cartId, o) => cartRequest("DELETE", () => `${cart(cartId)}/coupon`, void 0, o),
|
|
494
|
+
setCustomer: (cartId, input, o) => cartRequest("PUT", () => `${cart(cartId)}/customer`, input, o),
|
|
495
|
+
setAddress: (cartId, input, o) => cartRequest("PUT", () => `${cart(cartId)}/address`, input, o),
|
|
400
496
|
shippingOptions: (cartId, o) => get(() => `${cart(cartId)}/shipping-options`, o),
|
|
401
|
-
setShippingOption: (cartId, optionId, o) =>
|
|
497
|
+
setShippingOption: (cartId, optionId, o) => cartRequest(
|
|
402
498
|
"PUT",
|
|
403
499
|
() => `${cart(cartId)}/shipping-option`,
|
|
404
500
|
{ id: optionId },
|
|
405
501
|
o
|
|
406
502
|
),
|
|
407
|
-
agreements: (cartId, o) =>
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
503
|
+
agreements: async (cartId, o) => normalizeAgreements(
|
|
504
|
+
await request(
|
|
505
|
+
"POST",
|
|
506
|
+
() => `${cart(cartId)}/agreements`,
|
|
507
|
+
void 0,
|
|
508
|
+
o
|
|
509
|
+
)
|
|
412
510
|
),
|
|
413
511
|
checkout: async (cartId, o) => {
|
|
414
512
|
const hash = o?.agreementsHash;
|
|
@@ -437,9 +535,11 @@ function createStorefrontClient(options) {
|
|
|
437
535
|
}
|
|
438
536
|
},
|
|
439
537
|
orders: {
|
|
440
|
-
get: (orderId, token, o) =>
|
|
441
|
-
|
|
442
|
-
|
|
538
|
+
get: async (orderId, token, o) => normalizeOrder(
|
|
539
|
+
await get(
|
|
540
|
+
() => `/v1/orders/${seg(orderId)}?token=${encodeURIComponent(token)}`,
|
|
541
|
+
o
|
|
542
|
+
)
|
|
443
543
|
),
|
|
444
544
|
createReturn: (orderId, token, input, o) => request(
|
|
445
545
|
"POST",
|
|
@@ -797,7 +897,7 @@ async function renderTurnstile(element, siteKey, options) {
|
|
|
797
897
|
}
|
|
798
898
|
|
|
799
899
|
// src/index.ts
|
|
800
|
-
var SDK_VERSION = "0.
|
|
900
|
+
var SDK_VERSION = "0.10.0";
|
|
801
901
|
export {
|
|
802
902
|
BUYER_IP_HEADER,
|
|
803
903
|
BUYER_USER_AGENT_HEADER,
|