@konfiqo/storefront-sdk 0.8.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 +144 -73
- package/dist/index.cjs +152 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +238 -8
- package/dist/index.d.ts +238 -8
- package/dist/index.js +152 -26
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -183,34 +183,38 @@ with the response body or rejects with a [`StorefrontError`](#errors).
|
|
|
183
183
|
Path arguments (slugs, ids) that are empty, `.` or `..` reject with a
|
|
184
184
|
`TypeError` (`invalid_path_segment`) without a request.
|
|
185
185
|
|
|
186
|
-
| Method | Route
|
|
187
|
-
| ----------------------------------------------------------------------------- |
|
|
188
|
-
| `store.get()` | `GET /v1/store`
|
|
189
|
-
| `categories.list()` | `GET /v1/categories`
|
|
190
|
-
| `products.list({ category?, q?, page?, pageSize? })` | `GET /v1/products`
|
|
191
|
-
| `products.get(slug)` | `GET /v1/products/:slug`
|
|
192
|
-
| `blog.list({ page?, pageSize? })` | `GET /v1/blog`
|
|
193
|
-
| `blog.get(slug)` | `GET /v1/blog/:slug`
|
|
194
|
-
| `carts.create()` | `POST /v1/carts`
|
|
195
|
-
| `carts.get(cartId)` | `GET /v1/carts/:id`
|
|
196
|
-
| `carts.addLine(cartId, { variantId, quantity, personalization? })` | `POST /v1/carts/:id/lines`
|
|
197
|
-
| `carts.updateLine(cartId, lineId, { quantity })` | `PATCH /v1/carts/:id/lines/:lineId`
|
|
198
|
-
| `carts.removeLine(cartId, lineId)` | `DELETE /v1/carts/:id/lines/:lineId`
|
|
199
|
-
| `carts.setCustomer(cartId, { email, name, surname, phone, identityNumber? })` | `PUT /v1/carts/:id/customer`
|
|
200
|
-
| `carts.setAddress(cartId, { shipping, billing? })` | `PUT /v1/carts/:id/address`
|
|
201
|
-
| `carts.shippingOptions(cartId)` | `GET /v1/carts/:id/shipping-options`
|
|
202
|
-
| `carts.setShippingOption(cartId, optionId)` | `PUT /v1/carts/:id/shipping-option`
|
|
203
|
-
| `carts.
|
|
204
|
-
| `carts.
|
|
205
|
-
| `
|
|
206
|
-
| `carts.
|
|
207
|
-
| `
|
|
208
|
-
| `
|
|
209
|
-
| `
|
|
210
|
-
| `
|
|
211
|
-
| `account.
|
|
212
|
-
| `account.
|
|
213
|
-
| `
|
|
186
|
+
| Method | Route | Resolves with |
|
|
187
|
+
| ----------------------------------------------------------------------------- | ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
188
|
+
| `store.get()` | `GET /v1/store` | `Store`: name, `defaultLanguage`, `currency`, `logo`/`logoUrl`, `theme` (`ThemeV1`, as stored), contact, `company`, shipping rules, `hasCodeCampaigns` |
|
|
189
|
+
| `categories.list()` | `GET /v1/categories` | `{ items: Category[] }` (active tree, sorted) |
|
|
190
|
+
| `products.list({ category?, q?, page?, pageSize? })` | `GET /v1/products` | `{ items: Product[], pagination }` (`pageSize` 1–100, default 20) |
|
|
191
|
+
| `products.get(slug)` | `GET /v1/products/:slug` | `Product` |
|
|
192
|
+
| `blog.list({ page?, pageSize? })` | `GET /v1/blog` | `{ items: BlogPostSummary[], pagination }` (newest first, `pageSize` 1–50, default 12) |
|
|
193
|
+
| `blog.get(slug)` | `GET /v1/blog/:slug` | `BlogPost`: the summary fields, `metaDescription` and the body `tree` |
|
|
194
|
+
| `carts.create()` | `POST /v1/carts` | `Cart` |
|
|
195
|
+
| `carts.get(cartId)` | `GET /v1/carts/:id` | `Cart` |
|
|
196
|
+
| `carts.addLine(cartId, { variantId, quantity, personalization? })` | `POST /v1/carts/:id/lines` | `Cart` |
|
|
197
|
+
| `carts.updateLine(cartId, lineId, { quantity })` | `PATCH /v1/carts/:id/lines/:lineId` | `Cart` |
|
|
198
|
+
| `carts.removeLine(cartId, lineId)` | `DELETE /v1/carts/:id/lines/:lineId` | `Cart` |
|
|
199
|
+
| `carts.setCustomer(cartId, { email, name, surname, phone, identityNumber? })` | `PUT /v1/carts/:id/customer` | `Cart` |
|
|
200
|
+
| `carts.setAddress(cartId, { shipping, billing? })` | `PUT /v1/carts/:id/address` | `Cart` |
|
|
201
|
+
| `carts.shippingOptions(cartId)` | `GET /v1/carts/:id/shipping-options` | `{ items: { id, priceMinor }[] }` |
|
|
202
|
+
| `carts.setShippingOption(cartId, optionId)` | `PUT /v1/carts/:id/shipping-option` | `Cart` |
|
|
203
|
+
| `carts.applyCode(cartId, code)` | `PUT /v1/carts/:id/coupon` | `Cart` with the code applied (see [Discount codes](#discount-codes)) |
|
|
204
|
+
| `carts.removeCode(cartId)` | `DELETE /v1/carts/:id/coupon` | `Cart` without the code |
|
|
205
|
+
| `carts.agreements(cartId)` | `POST /v1/carts/:id/agreements` | `CartAgreements` (see [Legal texts and agreements](#legal-texts-and-agreements)) |
|
|
206
|
+
| `carts.checkout(cartId, { agreementsHash, turnstileToken? })` | `POST /v1/carts/:id/checkout` | `CheckoutResult` (see [Checkout challenge](#checkout-challenge)) |
|
|
207
|
+
| `orders.get(orderId, token)` | `GET /v1/orders/:id?token=` | `Order` |
|
|
208
|
+
| `orders.createReturn(orderId, token, { lines, note? })` | `POST /v1/orders/:id/returns` | `OrderReturn` (see [Returns](#returns)); the token goes in the body |
|
|
209
|
+
| `orders.cancelReturn(orderId, returnId, token)` | `POST /v1/orders/:id/returns/:returnId/cancel` | `OrderReturn`, now `cancelled` |
|
|
210
|
+
| `carts.recover(token)` | `POST /v1/carts/recover` | `{ cartId }`: a **new** cart with the abandoned cart's lines (see [Accounts](#accounts-and-cart-recovery)) |
|
|
211
|
+
| `account.requestCode(email, { turnstileToken? })` | `POST /v1/account/code` | nothing (202 for any address) |
|
|
212
|
+
| `account.signIn(email, code)` | `POST /v1/account/session` | `{ customer: { email } }`; sets the session cookie |
|
|
213
|
+
| `account.signOut()` | `DELETE /v1/account/session` | nothing |
|
|
214
|
+
| `account.get(page?)` | `GET /v1/account` | `Account`: profile, `savedAddresses`, one page of `orders` (20, newest first) |
|
|
215
|
+
| `account.updateProfile({ name, surname, phone })` | `PUT /v1/account/profile` | nothing |
|
|
216
|
+
| `account.saveAddresses(list)` | `PUT /v1/account/addresses` | `SavedAddress[]` as stored (max 5, one default) |
|
|
217
|
+
| `media.url(key)` | none | absolute image URL (synchronous) |
|
|
214
218
|
|
|
215
219
|
### Accounts and cart recovery
|
|
216
220
|
|
|
@@ -286,6 +290,70 @@ API that returns them; an older server omits them, and against one treat `id`
|
|
|
286
290
|
as `variantId`, `personalization` as null and `personalizationValid` as true.
|
|
287
291
|
Order lines carry `personalization?: string | null`.
|
|
288
292
|
|
|
293
|
+
### Returns
|
|
294
|
+
|
|
295
|
+
`order.returnable` says whether the buyer can open a return now:
|
|
296
|
+
`{ eligible, reason, deadline }`. `deadline` is 14 days after the latest
|
|
297
|
+
delivery, or null when no delivery date is known. Show the return form only
|
|
298
|
+
when `eligible` is true. Each order line has an `id` (the `lineId` to send) and
|
|
299
|
+
`returnableQuantity` (units still open for return). Lines with a
|
|
300
|
+
personalization text cannot be returned.
|
|
301
|
+
|
|
302
|
+
```ts
|
|
303
|
+
const returned = await client.orders.createReturn(order.id, token, {
|
|
304
|
+
lines: [{ lineId: order.lines[0].id, quantity: 1, reason: 'defective' }],
|
|
305
|
+
note: 'The lid is cracked.',
|
|
306
|
+
});
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
`order.returns` lists the returns, oldest first. A return starts as
|
|
310
|
+
`requested`. The store may approve it at once or leave that to the seller.
|
|
311
|
+
From `approved` on, the return carries `carrier`, `returnCode` and
|
|
312
|
+
`instructions` for sending the parcel back. Later statuses are `received`,
|
|
313
|
+
`refunded`, `rejected` (see `rejectionReason`) and `cancelled`. The buyer can
|
|
314
|
+
cancel while a return is `requested` or `approved`.
|
|
315
|
+
|
|
316
|
+
Errors: `invalid_return` (400) for a line that is not of this order, a repeated
|
|
317
|
+
line or too many units. `return_not_allowed` (409) when the order takes no
|
|
318
|
+
return now; `details.reason` is one of `unpaid`, `refunded`, `cancelled`,
|
|
319
|
+
`window_closed`, `open_return`, `too_many_returns` or `nothing_left`, and it is
|
|
320
|
+
absent when a cancel is refused because the return is no longer open.
|
|
321
|
+
`return_not_found` (404) for a return of another order.
|
|
322
|
+
|
|
323
|
+
### Discount codes
|
|
324
|
+
|
|
325
|
+
A cart carries one code. `carts.applyCode(cartId, code)` enters it (a second
|
|
326
|
+
code replaces the first) and answers the cart with the discount applied;
|
|
327
|
+
`carts.removeCode(cartId)` takes it off. Codes are matched without regard to
|
|
328
|
+
case or surrounding spaces.
|
|
329
|
+
|
|
330
|
+
```ts
|
|
331
|
+
try {
|
|
332
|
+
const cart = await client.carts.applyCode(cartId, 'SUMMER10');
|
|
333
|
+
if (cart.codeReason)
|
|
334
|
+
showCodeReason(cart.codeReason); // kept, not applied
|
|
335
|
+
else showApplied(cart.appliedCampaigns);
|
|
336
|
+
} catch (e) {
|
|
337
|
+
if (isStorefrontError(e) && e.is('coupon_invalid')) {
|
|
338
|
+
showCodeReason(e.details.reason); // 'unknown' | 'used_up'
|
|
339
|
+
} else throw e;
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
A known code whose conditions do not hold now (a minimum subtotal, a date)
|
|
344
|
+
is kept on the cart with `codeReason` set and no discount; it applies once
|
|
345
|
+
they hold. `codeReason` is `expired`, `not_started`, `min_subtotal`,
|
|
346
|
+
`first_order_only`, `used_up`, `not_combinable_better` or `unknown`. A code
|
|
347
|
+
found no longer usable at checkout (another order used it, it was switched
|
|
348
|
+
off) stays on the cart with `codeReason` `used_up` or `unknown` until the
|
|
349
|
+
buyer applies a code again.
|
|
350
|
+
|
|
351
|
+
The cart is priced without the buyer: a campaign that needs the buyer (first
|
|
352
|
+
order only, a per-customer limit) is listed in `pendingCampaigns` until the
|
|
353
|
+
agreements are loaded. `carts.agreements` answers with `quote`, the totals
|
|
354
|
+
for this buyer: show those at the review step. Offer the code field only
|
|
355
|
+
when `store.hasCodeCampaigns` is true.
|
|
356
|
+
|
|
289
357
|
**Customer.** `phone` must be a Turkish mobile number (`05XX…`, `5XX…`,
|
|
290
358
|
`+90…`, `0090…`; spaces, dashes, parentheses allowed); it comes back as
|
|
291
359
|
`90XXXXXXXXXX`. Addresses use `{ name?, phone?, email?, address | line1 +
|
|
@@ -344,50 +412,51 @@ try {
|
|
|
344
412
|
|
|
345
413
|
### API error codes
|
|
346
414
|
|
|
347
|
-
| Code | Status | `details`
|
|
348
|
-
| ------------------------- | -------- |
|
|
349
|
-
| `store_not_found` | 404 |
|
|
350
|
-
| `origin_not_allowed` | 403 |
|
|
351
|
-
| `invalid_request` | 400 | `{ issues: [{ path, message }] }`
|
|
352
|
-
| `invalid_buyer_ip` | 400 |
|
|
353
|
-
| `catalog_unavailable` | 503 |
|
|
354
|
-
| `product_not_found` | 404 |
|
|
355
|
-
| `cart_not_found` | 404 |
|
|
356
|
-
| `variant_not_found` | 404 |
|
|
357
|
-
| `insufficient_stock` | 409 | `{ available }`
|
|
358
|
-
| `quantity_out_of_range` | 400 |
|
|
359
|
-
| `too_many_lines` | 400 |
|
|
360
|
-
| `line_not_found` | 404 |
|
|
361
|
-
| `invalid_shipping_option` | 400 |
|
|
362
|
-
| `cart_read_only` | 409 | `{ status: 'ordered' \| 'expired' }`
|
|
363
|
-
| `checkout_in_progress` | 409 |
|
|
364
|
-
| `checkout_incomplete` | 400 | `{ missing: ('lines' \| 'customer' \| 'identityNumber' \| 'shippingAddress' \| 'shippingOption')[] }`
|
|
365
|
-
| `legal_not_confirmed` | 409 |
|
|
366
|
-
| `agreements_changed` | 409 |
|
|
367
|
-
| `cart_incomplete` | 409 | `{ missing }` (as `checkout_incomplete`)
|
|
368
|
-
| `cart_changed` | 409 | `{ lines, subtotalMinor, shippingMinor, totalMinor, shippingOptions }`
|
|
369
|
-
| `
|
|
370
|
-
| `
|
|
371
|
-
| `
|
|
372
|
-
| `
|
|
373
|
-
| `
|
|
374
|
-
| `
|
|
375
|
-
| `
|
|
376
|
-
| `
|
|
377
|
-
| `
|
|
378
|
-
| `
|
|
379
|
-
| `
|
|
380
|
-
| `
|
|
381
|
-
| `
|
|
382
|
-
| `
|
|
383
|
-
| `
|
|
384
|
-
| `
|
|
385
|
-
| `
|
|
386
|
-
| `
|
|
387
|
-
| `
|
|
388
|
-
| `
|
|
389
|
-
| `
|
|
390
|
-
| `
|
|
415
|
+
| Code | Status | `details` | Meaning / what to do |
|
|
416
|
+
| ------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
417
|
+
| `store_not_found` | 404 | | bad / revoked publishable key, or the store is disabled |
|
|
418
|
+
| `origin_not_allowed` | 403 | | browser `Origin` not in the store's `allowedOrigins` |
|
|
419
|
+
| `invalid_request` | 400 | `{ issues: [{ path, message }] }` | a body or query field is invalid (e.g. `path: "phone"`) |
|
|
420
|
+
| `invalid_buyer_ip` | 400 | | trusted key sent without a valid `buyerIp` |
|
|
421
|
+
| `catalog_unavailable` | 503 | | the catalog could not be read; retry later |
|
|
422
|
+
| `product_not_found` | 404 | | no sellable product with that slug |
|
|
423
|
+
| `cart_not_found` | 404 | | unknown cart (or another store's): start a new cart |
|
|
424
|
+
| `variant_not_found` | 404 | | the variant is not sold |
|
|
425
|
+
| `insufficient_stock` | 409 | `{ available }` | only `available` units can be added |
|
|
426
|
+
| `quantity_out_of_range` | 400 | | a line holds 1–99 units |
|
|
427
|
+
| `too_many_lines` | 400 | | a cart holds at most 50 lines |
|
|
428
|
+
| `line_not_found` | 404 | | no line with that id |
|
|
429
|
+
| `invalid_shipping_option` | 400 | | the option is not offered for this cart |
|
|
430
|
+
| `cart_read_only` | 409 | `{ status: 'ordered' \| 'expired' }` | the cart can no longer change: `expired` → start a new cart; `ordered` → show the order, or re-open payment with `REOPEN_AGREEMENTS_HASH` (step 9) |
|
|
431
|
+
| `checkout_in_progress` | 409 | | another checkout of this cart is running: retry in a few seconds |
|
|
432
|
+
| `checkout_incomplete` | 400 | `{ missing: ('lines' \| 'customer' \| 'identityNumber' \| 'shippingAddress' \| 'shippingOption')[] }` | collect what is missing |
|
|
433
|
+
| `legal_not_confirmed` | 409 | | the store has not confirmed its legal texts and takes no orders yet: show a clear "not taking orders" message |
|
|
434
|
+
| `agreements_changed` | 409 | | the cart or a legal text changed after `carts.agreements`: load the agreements again, show them, then check out with the new hash |
|
|
435
|
+
| `cart_incomplete` | 409 | `{ missing }` (as `checkout_incomplete`) | `carts.agreements` on a cart without lines, customer or address: collect what is missing, then load the agreements |
|
|
436
|
+
| `cart_changed` | 409 | `{ lines, subtotalMinor, discountMinor, shippingMinor, shippingDiscountMinor, totalMinor, shippingOptions, appliedCampaigns, pendingCampaigns, codeReason }` | prices, shipping or a discount changed: show the new totals, let the buyer confirm, then call checkout again. Never re-submit silently. |
|
|
437
|
+
| `coupon_invalid` | 422 | `{ reason: 'unknown' \| 'used_up' }` | `carts.applyCode`: no such active code (`unknown`), or a single-use code already used or reserved (`used_up`). The cart is unchanged. |
|
|
438
|
+
| `out_of_stock` | 409 | `{ variantIds }` | remove / reduce those lines |
|
|
439
|
+
| `invalid_personalization` | 400, 409 | `{ lineIds? }` (409 at checkout) | 400: the text breaks the product's rule (or the product asks for none). 409: those lines' stored text is no longer valid: remove each line and add it again with a valid text |
|
|
440
|
+
| `amount_out_of_range` | 400 | | total not representable |
|
|
441
|
+
| `nothing_to_pay` | 400 | | total is zero |
|
|
442
|
+
| `invalid_buyer_details` | 422 | `{ field }` | the payment provider refused `name`, `surname`, `email`, `phone`, `identityNumber`, `shippingAddress.<f>` or `billingAddress.<f>`: let the buyer fix it, check out again |
|
|
443
|
+
| `payment_unavailable` | 502 | | payments refused or unreachable; the order was cancelled, the cart is still open |
|
|
444
|
+
| `checkout_unavailable` | 503 | | store backend unreachable; the cart is still open, retry |
|
|
445
|
+
| `order_not_found` | 404 | | unknown order or wrong token |
|
|
446
|
+
| `order_unavailable` | 503 | | order status temporarily unavailable; poll again |
|
|
447
|
+
| `media_not_found` | 404 | | (image route) key not referenced by the catalog |
|
|
448
|
+
| `media_unavailable` | 503 | | (image route) image temporarily unavailable |
|
|
449
|
+
| `challenge_required` | 400 | | the store requires a checkout challenge and no token was sent: render the widget, check out with its token |
|
|
450
|
+
| `challenge_failed` | 403 | | the token was invalid, expired or already used: `reset()` the widget, check out with the new token |
|
|
451
|
+
| `challenge_unavailable` | 503 | | the challenge could not be verified right now: retry in a moment |
|
|
452
|
+
| `too_many_open_orders` | 429 | | too many unpaid orders from this buyer: finish or wait out a pending payment |
|
|
453
|
+
| `not_signed_in` | 401 | | no valid session cookie: sign in |
|
|
454
|
+
| `invalid_code` | 401 | | wrong, spent or expired sign-in code (5 attempts per code) |
|
|
455
|
+
| `profile_required` | 409 | | save the profile before saving addresses |
|
|
456
|
+
| `too_many_code_requests` | 429 | | too many codes requested from this address: wait |
|
|
457
|
+
| `invalid_recovery_token` | 400 | | the recovery link is malformed or not for this store |
|
|
458
|
+
| `cart_not_recoverable` | 410 | | the abandoned cart was ordered, expired or deleted: start a new cart |
|
|
459
|
+
| `unavailable` | 503 | | the account could not be read or saved right now: retry |
|
|
391
460
|
|
|
392
461
|
### Codes the SDK assigns
|
|
393
462
|
|
|
@@ -502,7 +571,9 @@ items, so they must be shown **filled in for the cart** before payment:
|
|
|
502
571
|
|
|
503
572
|
1. Once the cart has lines, a customer and an address, call
|
|
504
573
|
`carts.agreements(cartId)`. It answers `{ orderNumber, documents,
|
|
505
|
-
agreementsHash }`. The `documents` are `{ key, title, tree }
|
|
574
|
+
agreementsHash, quote }`. The `documents` are `{ key, title, tree }`;
|
|
575
|
+
`quote` holds the totals for this buyer (see
|
|
576
|
+
[Discount codes](#discount-codes)), null on older APIs.
|
|
506
577
|
2. Show the documents and ask the buyer to accept them. Call
|
|
507
578
|
`carts.agreements` again after every change to the cart.
|
|
508
579
|
3. Check out with the hash:
|
package/dist/index.cjs
CHANGED
|
@@ -50,6 +50,88 @@ __export(index_exports, {
|
|
|
50
50
|
});
|
|
51
51
|
module.exports = __toCommonJS(index_exports);
|
|
52
52
|
|
|
53
|
+
// src/normalize.ts
|
|
54
|
+
var isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
55
|
+
var num = (v) => typeof v === "number" ? v : 0;
|
|
56
|
+
var text = (v) => typeof v === "string" ? v : "";
|
|
57
|
+
var REASONS = /* @__PURE__ */ new Set([
|
|
58
|
+
"expired",
|
|
59
|
+
"not_started",
|
|
60
|
+
"min_subtotal",
|
|
61
|
+
"first_order_only",
|
|
62
|
+
"used_up",
|
|
63
|
+
"not_combinable_better",
|
|
64
|
+
"unknown"
|
|
65
|
+
]);
|
|
66
|
+
function normalizeReason(v) {
|
|
67
|
+
return typeof v === "string" && REASONS.has(v) ? v : null;
|
|
68
|
+
}
|
|
69
|
+
function normalizeCampaigns(v) {
|
|
70
|
+
if (!Array.isArray(v)) return [];
|
|
71
|
+
return v.filter(isObject).map((c) => ({
|
|
72
|
+
...typeof c.campaignId === "string" && { campaignId: c.campaignId },
|
|
73
|
+
buyerLabel: text(c.buyerLabel),
|
|
74
|
+
discountMinor: num(c.discountMinor),
|
|
75
|
+
shippingDiscountMinor: num(c.shippingDiscountMinor)
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
function normalizePending(v) {
|
|
79
|
+
if (!Array.isArray(v)) return [];
|
|
80
|
+
return v.filter(isObject).map((c) => ({
|
|
81
|
+
...typeof c.campaignId === "string" && { campaignId: c.campaignId },
|
|
82
|
+
buyerLabel: text(c.buyerLabel)
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
function normalizeCartLine(l) {
|
|
86
|
+
return { ...l, discountMinor: num(l.discountMinor) };
|
|
87
|
+
}
|
|
88
|
+
function normalizeCart(c) {
|
|
89
|
+
const r = c;
|
|
90
|
+
return {
|
|
91
|
+
...c,
|
|
92
|
+
lines: (Array.isArray(r.lines) ? r.lines : []).map(
|
|
93
|
+
(l) => normalizeCartLine(l)
|
|
94
|
+
),
|
|
95
|
+
discountMinor: num(r.discountMinor),
|
|
96
|
+
shippingDiscountMinor: num(r.shippingDiscountMinor),
|
|
97
|
+
appliedCampaigns: normalizeCampaigns(r.appliedCampaigns),
|
|
98
|
+
pendingCampaigns: normalizePending(r.pendingCampaigns),
|
|
99
|
+
couponCode: typeof r.couponCode === "string" ? r.couponCode : null,
|
|
100
|
+
codeReason: normalizeReason(r.codeReason)
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function normalizeOrder(o) {
|
|
104
|
+
const r = o;
|
|
105
|
+
return {
|
|
106
|
+
...o,
|
|
107
|
+
discountMinor: num(r.discountMinor),
|
|
108
|
+
shippingDiscountMinor: num(r.shippingDiscountMinor),
|
|
109
|
+
appliedCampaigns: normalizeCampaigns(r.appliedCampaigns),
|
|
110
|
+
lines: (Array.isArray(r.lines) ? r.lines : []).map((l) => ({
|
|
111
|
+
...l,
|
|
112
|
+
discountMinor: num(l.discountMinor)
|
|
113
|
+
}))
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function normalizeAgreements(a) {
|
|
117
|
+
const q = a.quote;
|
|
118
|
+
const quote = isObject(q) ? {
|
|
119
|
+
subtotalMinor: num(q.subtotalMinor),
|
|
120
|
+
discountMinor: num(q.discountMinor),
|
|
121
|
+
shippingMinor: num(q.shippingMinor),
|
|
122
|
+
shippingDiscountMinor: num(q.shippingDiscountMinor),
|
|
123
|
+
totalMinor: num(q.totalMinor),
|
|
124
|
+
appliedCampaigns: normalizeCampaigns(q.appliedCampaigns),
|
|
125
|
+
pendingCampaigns: normalizePending(q.pendingCampaigns),
|
|
126
|
+
codeReason: normalizeReason(q.codeReason)
|
|
127
|
+
} : null;
|
|
128
|
+
return { ...a, quote };
|
|
129
|
+
}
|
|
130
|
+
function normalizeStore(s) {
|
|
131
|
+
const r = s;
|
|
132
|
+
return { ...s, hasCodeCampaigns: r.hasCodeCampaigns !== false };
|
|
133
|
+
}
|
|
134
|
+
|
|
53
135
|
// src/errors.ts
|
|
54
136
|
var API_CODES = /* @__PURE__ */ new Set([
|
|
55
137
|
"store_not_found",
|
|
@@ -74,6 +156,7 @@ var API_CODES = /* @__PURE__ */ new Set([
|
|
|
74
156
|
"agreements_changed",
|
|
75
157
|
"cart_incomplete",
|
|
76
158
|
"cart_changed",
|
|
159
|
+
"coupon_invalid",
|
|
77
160
|
"invalid_personalization",
|
|
78
161
|
"out_of_stock",
|
|
79
162
|
"amount_out_of_range",
|
|
@@ -95,6 +178,9 @@ var API_CODES = /* @__PURE__ */ new Set([
|
|
|
95
178
|
"too_many_code_requests",
|
|
96
179
|
"invalid_recovery_token",
|
|
97
180
|
"cart_not_recoverable",
|
|
181
|
+
"invalid_return",
|
|
182
|
+
"return_not_allowed",
|
|
183
|
+
"return_not_found",
|
|
98
184
|
"unavailable"
|
|
99
185
|
]);
|
|
100
186
|
var ERROR_BRAND = /* @__PURE__ */ Symbol.for("@konfiqo/storefront-sdk/error");
|
|
@@ -128,7 +214,7 @@ var StorefrontError = class extends Error {
|
|
|
128
214
|
function isStorefrontError(e) {
|
|
129
215
|
return e instanceof StorefrontError || typeof e === "object" && e !== null && e[ERROR_BRAND] === true;
|
|
130
216
|
}
|
|
131
|
-
var
|
|
217
|
+
var isObject2 = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
132
218
|
function parseRetryAfter(value) {
|
|
133
219
|
if (value === null) return void 0;
|
|
134
220
|
const trimmed = value.trim();
|
|
@@ -138,7 +224,7 @@ function parseRetryAfter(value) {
|
|
|
138
224
|
return Math.max(0, Math.ceil((at - Date.now()) / 1e3));
|
|
139
225
|
}
|
|
140
226
|
function errorFromResponse(status, body, headers) {
|
|
141
|
-
const b =
|
|
227
|
+
const b = isObject2(body) ? body : {};
|
|
142
228
|
const message = typeof b.message === "string" && b.message !== "" ? b.message : `Storefront API request failed (HTTP ${status})`;
|
|
143
229
|
const error = typeof b.error === "string" ? b.error : null;
|
|
144
230
|
if (status === 429 && (error === null || !API_CODES.has(error))) {
|
|
@@ -171,11 +257,20 @@ function apiError(code, status, message, b) {
|
|
|
171
257
|
switch (code) {
|
|
172
258
|
case "cart_changed":
|
|
173
259
|
return new StorefrontError(code, status, message, {
|
|
174
|
-
lines: arr(b.lines),
|
|
260
|
+
lines: arr(b.lines).map(normalizeCartLine),
|
|
175
261
|
subtotalMinor: n(b.subtotalMinor),
|
|
262
|
+
discountMinor: n(b.discountMinor),
|
|
176
263
|
shippingMinor: n(b.shippingMinor),
|
|
264
|
+
shippingDiscountMinor: n(b.shippingDiscountMinor),
|
|
177
265
|
totalMinor: n(b.totalMinor),
|
|
178
|
-
shippingOptions: arr(b.shippingOptions)
|
|
266
|
+
shippingOptions: arr(b.shippingOptions),
|
|
267
|
+
appliedCampaigns: normalizeCampaigns(b.appliedCampaigns),
|
|
268
|
+
pendingCampaigns: normalizePending(b.pendingCampaigns),
|
|
269
|
+
codeReason: normalizeReason(b.codeReason)
|
|
270
|
+
});
|
|
271
|
+
case "coupon_invalid":
|
|
272
|
+
return new StorefrontError(code, status, message, {
|
|
273
|
+
reason: b.reason === "used_up" ? "used_up" : "unknown"
|
|
179
274
|
});
|
|
180
275
|
case "invalid_personalization":
|
|
181
276
|
return new StorefrontError(
|
|
@@ -209,6 +304,13 @@ function apiError(code, status, message, b) {
|
|
|
209
304
|
return new StorefrontError(code, status, message, {
|
|
210
305
|
status: b.status === "ordered" ? "ordered" : "expired"
|
|
211
306
|
});
|
|
307
|
+
case "return_not_allowed":
|
|
308
|
+
return new StorefrontError(
|
|
309
|
+
code,
|
|
310
|
+
status,
|
|
311
|
+
message,
|
|
312
|
+
typeof b.reason === "string" ? { reason: b.reason } : {}
|
|
313
|
+
);
|
|
212
314
|
default:
|
|
213
315
|
return new StorefrontError(
|
|
214
316
|
code,
|
|
@@ -333,26 +435,26 @@ function createStorefrontClient(options) {
|
|
|
333
435
|
callerSignal?.addEventListener("abort", onCallerAbort, { once: true });
|
|
334
436
|
try {
|
|
335
437
|
let res;
|
|
336
|
-
let
|
|
438
|
+
let text2;
|
|
337
439
|
try {
|
|
338
440
|
res = await Promise.race([doFetch(url, init), deadline]);
|
|
339
|
-
|
|
441
|
+
text2 = await Promise.race([res.text(), deadline]);
|
|
340
442
|
} catch (e) {
|
|
341
443
|
if (e instanceof StorefrontError) throw e;
|
|
342
444
|
throw transportError(e, timedOut, timeoutMs);
|
|
343
445
|
}
|
|
344
446
|
let parsed;
|
|
345
447
|
let isJson = false;
|
|
346
|
-
if (
|
|
448
|
+
if (text2 !== "") {
|
|
347
449
|
try {
|
|
348
|
-
parsed = JSON.parse(
|
|
450
|
+
parsed = JSON.parse(text2);
|
|
349
451
|
isJson = true;
|
|
350
452
|
} catch {
|
|
351
453
|
parsed = void 0;
|
|
352
454
|
}
|
|
353
455
|
}
|
|
354
456
|
if (!res.ok) throw errorFromResponse(res.status, parsed, res.headers);
|
|
355
|
-
if (!isJson && flags.emptyOk &&
|
|
457
|
+
if (!isJson && flags.emptyOk && text2 === "") return void 0;
|
|
356
458
|
if (!isJson) {
|
|
357
459
|
throw new StorefrontError(
|
|
358
460
|
"invalid_response",
|
|
@@ -371,10 +473,12 @@ function createStorefrontClient(options) {
|
|
|
371
473
|
const acct = { credentials: true };
|
|
372
474
|
const send = (method, path, body, o, extra = {}, emptyOk = false) => request(method, () => path, body, o, extra, { ...acct, emptyOk });
|
|
373
475
|
const cart = (id) => `/v1/carts/${seg(id)}`;
|
|
476
|
+
const cartRequest = async (method, path, body, o) => normalizeCart(await request(method, path, body, o));
|
|
477
|
+
const cartGet = (id, o) => cartRequest("GET", () => cart(id), void 0, o);
|
|
374
478
|
return {
|
|
375
479
|
getContent: (key, o) => get(() => `/v1/content/${seg(key)}`, o),
|
|
376
480
|
store: {
|
|
377
|
-
get: (o) => get(() => "/v1/store", o)
|
|
481
|
+
get: async (o) => normalizeStore(await get(() => "/v1/store", o))
|
|
378
482
|
},
|
|
379
483
|
blog: {
|
|
380
484
|
list: (query = {}, o) => get(() => {
|
|
@@ -405,15 +509,15 @@ function createStorefrontClient(options) {
|
|
|
405
509
|
get: (slug, o) => get(() => `/v1/products/${seg(slug)}`, o)
|
|
406
510
|
},
|
|
407
511
|
carts: {
|
|
408
|
-
create: (o) =>
|
|
512
|
+
create: (o) => cartRequest("POST", () => "/v1/carts", void 0, o),
|
|
409
513
|
recover: (token, o) => request(
|
|
410
514
|
"POST",
|
|
411
515
|
() => "/v1/carts/recover",
|
|
412
516
|
{ token },
|
|
413
517
|
o
|
|
414
518
|
),
|
|
415
|
-
get: (cartId, o) =>
|
|
416
|
-
addLine: (cartId, input, o) =>
|
|
519
|
+
get: (cartId, o) => cartGet(cartId, o),
|
|
520
|
+
addLine: (cartId, input, o) => cartRequest(
|
|
417
521
|
"POST",
|
|
418
522
|
() => `${cart(cartId)}/lines`,
|
|
419
523
|
{
|
|
@@ -425,32 +529,36 @@ function createStorefrontClient(options) {
|
|
|
425
529
|
},
|
|
426
530
|
o
|
|
427
531
|
),
|
|
428
|
-
updateLine: (cartId, lineId, input, o) =>
|
|
532
|
+
updateLine: (cartId, lineId, input, o) => cartRequest(
|
|
429
533
|
"PATCH",
|
|
430
534
|
() => `${cart(cartId)}/lines/${seg(lineId)}`,
|
|
431
535
|
{ quantity: input.quantity },
|
|
432
536
|
o
|
|
433
537
|
),
|
|
434
|
-
removeLine: (cartId, lineId, o) =>
|
|
538
|
+
removeLine: (cartId, lineId, o) => cartRequest(
|
|
435
539
|
"DELETE",
|
|
436
540
|
() => `${cart(cartId)}/lines/${seg(lineId)}`,
|
|
437
541
|
void 0,
|
|
438
542
|
o
|
|
439
543
|
),
|
|
440
|
-
|
|
441
|
-
|
|
544
|
+
applyCode: (cartId, code, o) => cartRequest("PUT", () => `${cart(cartId)}/coupon`, { code }, o),
|
|
545
|
+
removeCode: (cartId, o) => cartRequest("DELETE", () => `${cart(cartId)}/coupon`, void 0, o),
|
|
546
|
+
setCustomer: (cartId, input, o) => cartRequest("PUT", () => `${cart(cartId)}/customer`, input, o),
|
|
547
|
+
setAddress: (cartId, input, o) => cartRequest("PUT", () => `${cart(cartId)}/address`, input, o),
|
|
442
548
|
shippingOptions: (cartId, o) => get(() => `${cart(cartId)}/shipping-options`, o),
|
|
443
|
-
setShippingOption: (cartId, optionId, o) =>
|
|
549
|
+
setShippingOption: (cartId, optionId, o) => cartRequest(
|
|
444
550
|
"PUT",
|
|
445
551
|
() => `${cart(cartId)}/shipping-option`,
|
|
446
552
|
{ id: optionId },
|
|
447
553
|
o
|
|
448
554
|
),
|
|
449
|
-
agreements: (cartId, o) =>
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
555
|
+
agreements: async (cartId, o) => normalizeAgreements(
|
|
556
|
+
await request(
|
|
557
|
+
"POST",
|
|
558
|
+
() => `${cart(cartId)}/agreements`,
|
|
559
|
+
void 0,
|
|
560
|
+
o
|
|
561
|
+
)
|
|
454
562
|
),
|
|
455
563
|
checkout: async (cartId, o) => {
|
|
456
564
|
const hash = o?.agreementsHash;
|
|
@@ -479,8 +587,26 @@ function createStorefrontClient(options) {
|
|
|
479
587
|
}
|
|
480
588
|
},
|
|
481
589
|
orders: {
|
|
482
|
-
get: (orderId, token, o) =>
|
|
483
|
-
|
|
590
|
+
get: async (orderId, token, o) => normalizeOrder(
|
|
591
|
+
await get(
|
|
592
|
+
() => `/v1/orders/${seg(orderId)}?token=${encodeURIComponent(token)}`,
|
|
593
|
+
o
|
|
594
|
+
)
|
|
595
|
+
),
|
|
596
|
+
createReturn: (orderId, token, input, o) => request(
|
|
597
|
+
"POST",
|
|
598
|
+
() => `/v1/orders/${seg(orderId)}/returns`,
|
|
599
|
+
{
|
|
600
|
+
token,
|
|
601
|
+
lines: input.lines,
|
|
602
|
+
...input.note !== void 0 && { note: input.note }
|
|
603
|
+
},
|
|
604
|
+
o
|
|
605
|
+
),
|
|
606
|
+
cancelReturn: (orderId, returnId, token, o) => request(
|
|
607
|
+
"POST",
|
|
608
|
+
() => `/v1/orders/${seg(orderId)}/returns/${seg(returnId)}/cancel`,
|
|
609
|
+
{ token },
|
|
484
610
|
o
|
|
485
611
|
)
|
|
486
612
|
},
|
|
@@ -823,7 +949,7 @@ async function renderTurnstile(element, siteKey, options) {
|
|
|
823
949
|
}
|
|
824
950
|
|
|
825
951
|
// src/index.ts
|
|
826
|
-
var SDK_VERSION = "0.
|
|
952
|
+
var SDK_VERSION = "0.10.0";
|
|
827
953
|
// Annotate the CommonJS export names for ESM import in node:
|
|
828
954
|
0 && (module.exports = {
|
|
829
955
|
BUYER_IP_HEADER,
|