@konfiqo/storefront-sdk 0.4.0 → 0.6.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 CHANGED
@@ -181,31 +181,31 @@ with the response body or rejects with a [`StorefrontError`](#errors).
181
181
  Path arguments (slugs, ids) that are empty, `.` or `..` reject with a
182
182
  `TypeError` (`invalid_path_segment`) without a request.
183
183
 
184
- | Method | Route | Resolves with |
185
- | ----------------------------------------------------------------------------- | --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
186
- | `store.get()` | `GET /v1/store` | `Store`: name, `defaultLanguage`, `currency`, `logo`/`logoUrl`, `theme` (`ThemeV1`, as stored), contact, legal texts, shipping rules |
187
- | `categories.list()` | `GET /v1/categories` | `{ items: Category[] }` (active tree, sorted) |
188
- | `products.list({ category?, q?, page?, pageSize? })` | `GET /v1/products` | `{ items: Product[], pagination }` (`pageSize` 1–100, default 20) |
189
- | `products.get(slug)` | `GET /v1/products/:slug` | `Product` |
190
- | `carts.create()` | `POST /v1/carts` | `Cart` |
191
- | `carts.get(cartId)` | `GET /v1/carts/:id` | `Cart` |
192
- | `carts.addLine(cartId, { variantId, quantity })` | `POST /v1/carts/:id/lines` | `Cart` |
193
- | `carts.updateLine(cartId, variantId, { quantity })` | `PATCH /v1/carts/:id/lines/:variantId` | `Cart` |
194
- | `carts.removeLine(cartId, variantId)` | `DELETE /v1/carts/:id/lines/:variantId` | `Cart` |
195
- | `carts.setCustomer(cartId, { email, name, surname, phone, identityNumber? })` | `PUT /v1/carts/:id/customer` | `Cart` |
196
- | `carts.setAddress(cartId, { shipping, billing? })` | `PUT /v1/carts/:id/address` | `Cart` |
197
- | `carts.shippingOptions(cartId)` | `GET /v1/carts/:id/shipping-options` | `{ items: { id, priceMinor }[] }` |
198
- | `carts.setShippingOption(cartId, optionId)` | `PUT /v1/carts/:id/shipping-option` | `Cart` |
199
- | `carts.checkout(cartId, { turnstileToken? })` | `POST /v1/carts/:id/checkout` | `CheckoutResult` (see [Checkout challenge](#checkout-challenge)) |
200
- | `orders.get(orderId, token)` | `GET /v1/orders/:id?token=` | `Order` |
201
- | `carts.recover(token)` | `POST /v1/carts/recover` | `{ cartId }`: a **new** cart with the abandoned cart's lines (see [Accounts](#accounts-and-cart-recovery)) |
202
- | `account.requestCode(email, { turnstileToken? })` | `POST /v1/account/code` | nothing (202 for any address) |
203
- | `account.signIn(email, code)` | `POST /v1/account/session` | `{ customer: { email } }`; sets the session cookie |
204
- | `account.signOut()` | `DELETE /v1/account/session` | nothing |
205
- | `account.get(page?)` | `GET /v1/account` | `Account`: profile, `savedAddresses`, one page of `orders` (20, newest first) |
206
- | `account.updateProfile({ name, surname, phone })` | `PUT /v1/account/profile` | nothing |
207
- | `account.saveAddresses(list)` | `PUT /v1/account/addresses` | `SavedAddress[]` as stored (max 5, one default) |
208
- | `media.url(key)` | — | absolute image URL (synchronous) |
184
+ | Method | Route | Resolves with |
185
+ | ----------------------------------------------------------------------------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------ |
186
+ | `store.get()` | `GET /v1/store` | `Store`: name, `defaultLanguage`, `currency`, `logo`/`logoUrl`, `theme` (`ThemeV1`, as stored), contact, legal texts, shipping rules |
187
+ | `categories.list()` | `GET /v1/categories` | `{ items: Category[] }` (active tree, sorted) |
188
+ | `products.list({ category?, q?, page?, pageSize? })` | `GET /v1/products` | `{ items: Product[], pagination }` (`pageSize` 1–100, default 20) |
189
+ | `products.get(slug)` | `GET /v1/products/:slug` | `Product` |
190
+ | `carts.create()` | `POST /v1/carts` | `Cart` |
191
+ | `carts.get(cartId)` | `GET /v1/carts/:id` | `Cart` |
192
+ | `carts.addLine(cartId, { variantId, quantity, personalization? })` | `POST /v1/carts/:id/lines` | `Cart` |
193
+ | `carts.updateLine(cartId, lineId, { quantity })` | `PATCH /v1/carts/:id/lines/:lineId` | `Cart` |
194
+ | `carts.removeLine(cartId, lineId)` | `DELETE /v1/carts/:id/lines/:lineId` | `Cart` |
195
+ | `carts.setCustomer(cartId, { email, name, surname, phone, identityNumber? })` | `PUT /v1/carts/:id/customer` | `Cart` |
196
+ | `carts.setAddress(cartId, { shipping, billing? })` | `PUT /v1/carts/:id/address` | `Cart` |
197
+ | `carts.shippingOptions(cartId)` | `GET /v1/carts/:id/shipping-options` | `{ items: { id, priceMinor }[] }` |
198
+ | `carts.setShippingOption(cartId, optionId)` | `PUT /v1/carts/:id/shipping-option` | `Cart` |
199
+ | `carts.checkout(cartId, { turnstileToken? })` | `POST /v1/carts/:id/checkout` | `CheckoutResult` (see [Checkout challenge](#checkout-challenge)) |
200
+ | `orders.get(orderId, token)` | `GET /v1/orders/:id?token=` | `Order` |
201
+ | `carts.recover(token)` | `POST /v1/carts/recover` | `{ cartId }`: a **new** cart with the abandoned cart's lines (see [Accounts](#accounts-and-cart-recovery)) |
202
+ | `account.requestCode(email, { turnstileToken? })` | `POST /v1/account/code` | nothing (202 for any address) |
203
+ | `account.signIn(email, code)` | `POST /v1/account/session` | `{ customer: { email } }`; sets the session cookie |
204
+ | `account.signOut()` | `DELETE /v1/account/session` | nothing |
205
+ | `account.get(page?)` | `GET /v1/account` | `Account`: profile, `savedAddresses`, one page of `orders` (20, newest first) |
206
+ | `account.updateProfile({ name, surname, phone })` | `PUT /v1/account/profile` | nothing |
207
+ | `account.saveAddresses(list)` | `PUT /v1/account/addresses` | `SavedAddress[]` as stored (max 5, one default) |
208
+ | `media.url(key)` | — | absolute image URL (synchronous) |
209
209
 
210
210
  ### Accounts and cart recovery
211
211
 
@@ -263,6 +263,24 @@ rides in the query because `<img>` cannot send headers; it is public.
263
263
  (false = fewer units left than the line asks for). Checkout refuses both:
264
264
  show them and let the buyer remove or reduce.
265
265
 
266
+ **Line personalization.** A product can ask the buyer for one short text per
267
+ line (an engraving, a pet's name). When it does, `product.personalization` is
268
+ `{ label, required, minLength, maxLength }` (absent otherwise; lengths count
269
+ characters). Pass the text to `carts.addLine(cartId, { variantId, quantity,
270
+ personalization: 'Sarı' })`; a refused text rejects with
271
+ `invalid_personalization` (400). The same variant with a different text is a
272
+ separate line, so address lines by `line.id` in `updateLine` / `removeLine`:
273
+ it is the variant id for a line without text and `<variantId>~<12 hex>` for a
274
+ line with text (a variant id still matches a line without text). Each line
275
+ carries `personalization: string | null` and `personalizationValid`, which is
276
+ false when the stored text no longer satisfies the product's current rule;
277
+ checkout then rejects with `invalid_personalization` and `details.lineIds`:
278
+ remove those lines and add them again. `id`, `personalization` and
279
+ `personalizationValid` are required in the types and need a Konfiqo commerce
280
+ API that returns them; an older server omits them, and against one treat `id`
281
+ as `variantId`, `personalization` as null and `personalizationValid` as true.
282
+ Order lines carry `personalization?: string | null`.
283
+
266
284
  **Customer.** `phone` must be a Turkish mobile number (`05XX…`, `5XX…`,
267
285
  `+90…`, `0090…`; spaces, dashes, parentheses allowed); it comes back as
268
286
  `90XXXXXXXXXX`. Addresses use `{ name?, phone?, email?, address | line1 +
@@ -293,6 +311,9 @@ try {
293
311
  case 'out_of_stock':
294
312
  markLines(e.details.variantIds);
295
313
  break;
314
+ case 'invalid_personalization':
315
+ askAgain(e.details.lineIds ?? []);
316
+ break;
296
317
  case 'checkout_incomplete':
297
318
  ask(e.details.missing);
298
319
  break;
@@ -312,46 +333,47 @@ try {
312
333
 
313
334
  ### API error codes
314
335
 
315
- | Code | Status | `details` | Meaning / what to do |
316
- | ------------------------- | ------ | ----------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
317
- | `store_not_found` | 404 | | bad / revoked publishable key, or the store is disabled |
318
- | `origin_not_allowed` | 403 | | browser `Origin` not in the store's `allowedOrigins` |
319
- | `invalid_request` | 400 | `{ issues: [{ path, message }] }` | a body or query field is invalid (e.g. `path: "phone"`) |
320
- | `invalid_buyer_ip` | 400 | | trusted key sent without a valid `buyerIp` |
321
- | `catalog_unavailable` | 503 | | the catalog could not be read; retry later |
322
- | `product_not_found` | 404 | | no sellable product with that slug |
323
- | `cart_not_found` | 404 | | unknown cart (or another store's): start a new cart |
324
- | `variant_not_found` | 404 | | the variant is not sold |
325
- | `insufficient_stock` | 409 | `{ available }` | only `available` units can be added |
326
- | `quantity_out_of_range` | 400 | | a line holds 1–99 units |
327
- | `too_many_lines` | 400 | | a cart holds at most 50 lines |
328
- | `line_not_found` | 404 | | no line for that variant |
329
- | `invalid_shipping_option` | 400 | | the option is not offered for this cart |
330
- | `cart_read_only` | 409 | `{ status: 'ordered' \| 'expired' }` | the cart can no longer change: `expired` → start a new cart; `ordered` → check out again (re-opens payment) or show the order |
331
- | `checkout_in_progress` | 409 | | another checkout of this cart is running: retry in a few seconds |
332
- | `checkout_incomplete` | 400 | `{ missing: ('lines' \| 'customer' \| 'identityNumber' \| 'shippingAddress' \| 'shippingOption')[] }` | collect what is missing |
333
- | `cart_changed` | 409 | `{ lines, subtotalMinor, shippingMinor, totalMinor, shippingOptions }` | prices or shipping changed: show the new totals, let the buyer confirm, then call checkout again. Never re-submit silently. |
334
- | `out_of_stock` | 409 | `{ variantIds }` | remove / reduce those lines |
335
- | `amount_out_of_range` | 400 | | total not representable |
336
- | `nothing_to_pay` | 400 | | total is zero |
337
- | `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 |
338
- | `payment_unavailable` | 502 | | payments refused or unreachable; the order was cancelled, the cart is still open |
339
- | `checkout_unavailable` | 503 | | store backend unreachable; the cart is still open, retry |
340
- | `order_not_found` | 404 | | unknown order or wrong token |
341
- | `order_unavailable` | 503 | | order status temporarily unavailable; poll again |
342
- | `media_not_found` | 404 | | (image route) key not referenced by the catalog |
343
- | `media_unavailable` | 503 | | (image route) image temporarily unavailable |
344
- | `challenge_required` | 400 | | the store requires a checkout challenge and no token was sent: render the widget, check out with its token |
345
- | `challenge_failed` | 403 | | the token was invalid, expired or already used: `reset()` the widget, check out with the new token |
346
- | `challenge_unavailable` | 503 | | the challenge could not be verified right now: retry in a moment |
347
- | `too_many_open_orders` | 429 | | too many unpaid orders from this buyer: finish or wait out a pending payment |
348
- | `not_signed_in` | 401 | | no valid session cookie: sign in |
349
- | `invalid_code` | 401 | | wrong, spent or expired sign-in code (5 attempts per code) |
350
- | `profile_required` | 409 | | save the profile before saving addresses |
351
- | `too_many_code_requests` | 429 | | too many codes requested from this address: wait |
352
- | `invalid_recovery_token` | 400 | | the recovery link is malformed or not for this store |
353
- | `cart_not_recoverable` | 410 | | the abandoned cart was ordered, expired or deleted: start a new cart |
354
- | `unavailable` | 503 | | the account could not be read or saved right now: retry |
336
+ | Code | Status | `details` | Meaning / what to do |
337
+ | ------------------------- | -------- | ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
338
+ | `store_not_found` | 404 | | bad / revoked publishable key, or the store is disabled |
339
+ | `origin_not_allowed` | 403 | | browser `Origin` not in the store's `allowedOrigins` |
340
+ | `invalid_request` | 400 | `{ issues: [{ path, message }] }` | a body or query field is invalid (e.g. `path: "phone"`) |
341
+ | `invalid_buyer_ip` | 400 | | trusted key sent without a valid `buyerIp` |
342
+ | `catalog_unavailable` | 503 | | the catalog could not be read; retry later |
343
+ | `product_not_found` | 404 | | no sellable product with that slug |
344
+ | `cart_not_found` | 404 | | unknown cart (or another store's): start a new cart |
345
+ | `variant_not_found` | 404 | | the variant is not sold |
346
+ | `insufficient_stock` | 409 | `{ available }` | only `available` units can be added |
347
+ | `quantity_out_of_range` | 400 | | a line holds 1–99 units |
348
+ | `too_many_lines` | 400 | | a cart holds at most 50 lines |
349
+ | `line_not_found` | 404 | | no line with that id |
350
+ | `invalid_shipping_option` | 400 | | the option is not offered for this cart |
351
+ | `cart_read_only` | 409 | `{ status: 'ordered' \| 'expired' }` | the cart can no longer change: `expired` → start a new cart; `ordered` → check out again (re-opens payment) or show the order |
352
+ | `checkout_in_progress` | 409 | | another checkout of this cart is running: retry in a few seconds |
353
+ | `checkout_incomplete` | 400 | `{ missing: ('lines' \| 'customer' \| 'identityNumber' \| 'shippingAddress' \| 'shippingOption')[] }` | collect what is missing |
354
+ | `cart_changed` | 409 | `{ lines, subtotalMinor, shippingMinor, totalMinor, shippingOptions }` | prices or shipping changed: show the new totals, let the buyer confirm, then call checkout again. Never re-submit silently. |
355
+ | `out_of_stock` | 409 | `{ variantIds }` | remove / reduce those lines |
356
+ | `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 |
357
+ | `amount_out_of_range` | 400 | | total not representable |
358
+ | `nothing_to_pay` | 400 | | total is zero |
359
+ | `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 |
360
+ | `payment_unavailable` | 502 | | payments refused or unreachable; the order was cancelled, the cart is still open |
361
+ | `checkout_unavailable` | 503 | | store backend unreachable; the cart is still open, retry |
362
+ | `order_not_found` | 404 | | unknown order or wrong token |
363
+ | `order_unavailable` | 503 | | order status temporarily unavailable; poll again |
364
+ | `media_not_found` | 404 | | (image route) key not referenced by the catalog |
365
+ | `media_unavailable` | 503 | | (image route) image temporarily unavailable |
366
+ | `challenge_required` | 400 | | the store requires a checkout challenge and no token was sent: render the widget, check out with its token |
367
+ | `challenge_failed` | 403 | | the token was invalid, expired or already used: `reset()` the widget, check out with the new token |
368
+ | `challenge_unavailable` | 503 | | the challenge could not be verified right now: retry in a moment |
369
+ | `too_many_open_orders` | 429 | | too many unpaid orders from this buyer: finish or wait out a pending payment |
370
+ | `not_signed_in` | 401 | | no valid session cookie: sign in |
371
+ | `invalid_code` | 401 | | wrong, spent or expired sign-in code (5 attempts per code) |
372
+ | `profile_required` | 409 | | save the profile before saving addresses |
373
+ | `too_many_code_requests` | 429 | | too many codes requested from this address: wait |
374
+ | `invalid_recovery_token` | 400 | | the recovery link is malformed or not for this store |
375
+ | `cart_not_recoverable` | 410 | | the abandoned cart was ordered, expired or deleted: start a new cart |
376
+ | `unavailable` | 503 | | the account could not be read or saved right now: retry |
355
377
 
356
378
  ### Codes the SDK assigns
357
379
 
@@ -482,6 +504,22 @@ none); a consent given for another version should be asked again.
482
504
  `store.legal.cookies` is the cookie policy text. All three are absent from
483
505
  API versions before cookie consent: treat them as `[]`, `''` and no text.
484
506
 
507
+ Server-safe helpers for a store's own host (no browser API, so they run in
508
+ server components):
509
+
510
+ - `onDefaultDomain(host, suffix)` — whether `host` is the default store
511
+ domain suffix or a subdomain of it (case-insensitive; `false` without a
512
+ suffix).
513
+ - `pixelsForHost(adPixels, defaultDomain)` — on a default domain only
514
+ Google is kept: Meta and TikTok write their cookies to the registrable
515
+ domain, which every default store domain shares.
516
+ - `cookiesForHost(cookies, defaultDomain)` — on a default domain the
517
+ `customDomainOnly` cookies are dropped, and the list is empty when no
518
+ optional cookie is left (then no consent is needed).
519
+
520
+ `@konfiqo/storefront-react` has the consent state, store analytics and
521
+ the ad-pixel loader that use them.
522
+
485
523
  ## Helpers
486
524
 
487
525
  - `formatMoney(minor, currency, locale?)` — `Intl.NumberFormat` with the
package/dist/index.cjs CHANGED
@@ -32,6 +32,7 @@ __export(index_exports, {
32
32
  cartIdKey: () => cartIdKey,
33
33
  clearCartId: () => clearCartId,
34
34
  cookieCartStorage: () => cookieCartStorage,
35
+ cookiesForHost: () => cookiesForHost,
35
36
  createStorefrontClient: () => createStorefrontClient,
36
37
  formatMoney: () => formatMoney,
37
38
  isKnownThemeSection: () => isKnownThemeSection,
@@ -39,6 +40,8 @@ __export(index_exports, {
39
40
  loadCartId: () => loadCartId,
40
41
  loadTurnstile: () => loadTurnstile,
41
42
  mountPayment: () => mountPayment,
43
+ onDefaultDomain: () => onDefaultDomain,
44
+ pixelsForHost: () => pixelsForHost,
42
45
  renderTurnstile: () => renderTurnstile,
43
46
  saveCartId: () => saveCartId
44
47
  });
@@ -64,6 +67,7 @@ var API_CODES = /* @__PURE__ */ new Set([
64
67
  "checkout_in_progress",
65
68
  "checkout_incomplete",
66
69
  "cart_changed",
70
+ "invalid_personalization",
67
71
  "out_of_stock",
68
72
  "amount_out_of_range",
69
73
  "nothing_to_pay",
@@ -166,6 +170,13 @@ function apiError(code, status, message, b) {
166
170
  totalMinor: n(b.totalMinor),
167
171
  shippingOptions: arr(b.shippingOptions)
168
172
  });
173
+ case "invalid_personalization":
174
+ return new StorefrontError(
175
+ code,
176
+ status,
177
+ message,
178
+ Array.isArray(b.lineIds) ? { lineIds: arr(b.lineIds) } : {}
179
+ );
169
180
  case "out_of_stock":
170
181
  return new StorefrontError(code, status, message, {
171
182
  variantIds: arr(b.variantIds)
@@ -380,18 +391,24 @@ function createStorefrontClient(options) {
380
391
  addLine: (cartId, input, o) => request(
381
392
  "POST",
382
393
  () => `${cart(cartId)}/lines`,
383
- { variantId: input.variantId, quantity: input.quantity },
394
+ {
395
+ variantId: input.variantId,
396
+ quantity: input.quantity,
397
+ ...input.personalization !== void 0 && {
398
+ personalization: input.personalization
399
+ }
400
+ },
384
401
  o
385
402
  ),
386
- updateLine: (cartId, variantId, input, o) => request(
403
+ updateLine: (cartId, lineId, input, o) => request(
387
404
  "PATCH",
388
- () => `${cart(cartId)}/lines/${seg(variantId)}`,
405
+ () => `${cart(cartId)}/lines/${seg(lineId)}`,
389
406
  { quantity: input.quantity },
390
407
  o
391
408
  ),
392
- removeLine: (cartId, variantId, o) => request(
409
+ removeLine: (cartId, lineId, o) => request(
393
410
  "DELETE",
394
- () => `${cart(cartId)}/lines/${seg(variantId)}`,
411
+ () => `${cart(cartId)}/lines/${seg(lineId)}`,
395
412
  void 0,
396
413
  o
397
414
  ),
@@ -578,6 +595,23 @@ function formatMoney(minor, currency, locale) {
578
595
  return format.format(minor / 10 ** digits);
579
596
  }
580
597
 
598
+ // src/host-pixels.ts
599
+ function onDefaultDomain(host, suffix) {
600
+ if (!suffix) return false;
601
+ const h = host.toLowerCase();
602
+ const s = suffix.toLowerCase();
603
+ return h === s || h.endsWith(`.${s}`);
604
+ }
605
+ function pixelsForHost(p, defaultDomain) {
606
+ if (!p || !defaultDomain) return p;
607
+ return p.google ? { google: p.google } : {};
608
+ }
609
+ function cookiesForHost(cookies, defaultDomain) {
610
+ if (!defaultDomain) return cookies;
611
+ const kept = cookies.filter((c) => !c.customDomainOnly);
612
+ return kept.some((c) => c.category !== "necessary") ? kept : [];
613
+ }
614
+
581
615
  // src/cart-storage.ts
582
616
  function cartIdKey(storeId) {
583
617
  return `storefront:cart:${storeId}`;
@@ -730,7 +764,7 @@ async function renderTurnstile(element, siteKey, options) {
730
764
  }
731
765
 
732
766
  // src/index.ts
733
- var SDK_VERSION = "0.4.0";
767
+ var SDK_VERSION = "0.6.0";
734
768
  // Annotate the CommonJS export names for ESM import in node:
735
769
  0 && (module.exports = {
736
770
  BUYER_IP_HEADER,
@@ -745,6 +779,7 @@ var SDK_VERSION = "0.4.0";
745
779
  cartIdKey,
746
780
  clearCartId,
747
781
  cookieCartStorage,
782
+ cookiesForHost,
748
783
  createStorefrontClient,
749
784
  formatMoney,
750
785
  isKnownThemeSection,
@@ -752,6 +787,8 @@ var SDK_VERSION = "0.4.0";
752
787
  loadCartId,
753
788
  loadTurnstile,
754
789
  mountPayment,
790
+ onDefaultDomain,
791
+ pixelsForHost,
755
792
  renderTurnstile,
756
793
  saveCartId
757
794
  });