@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/dist/index.d.ts CHANGED
@@ -324,6 +324,17 @@ interface ProductVariant {
324
324
  /** Units available now (stock minus reservations); never negative. */
325
325
  available: number;
326
326
  }
327
+ /** The text a product asks the buyer for, once per cart line. */
328
+ interface ProductPersonalization {
329
+ /** What to ask for (e.g. the label of the input). */
330
+ label: string;
331
+ /** True when the product cannot be bought without the text. */
332
+ required: boolean;
333
+ /** Shortest accepted text, in characters (Unicode code points). */
334
+ minLength: number;
335
+ /** Longest accepted text, in characters (Unicode code points). */
336
+ maxLength: number;
337
+ }
327
338
  /** A product. Only products with at least one sellable variant are listed. */
328
339
  interface Product {
329
340
  /** Product id. */
@@ -352,6 +363,8 @@ interface Product {
352
363
  } | null;
353
364
  /** Sellable variants. */
354
365
  variants: ProductVariant[];
366
+ /** The text the product asks for per cart line; present only when it asks for one. */
367
+ personalization?: ProductPersonalization;
355
368
  }
356
369
  /** Paging information of a list. */
357
370
  interface Pagination {
@@ -386,10 +399,28 @@ interface ProductListQuery {
386
399
  type CartStatus = 'open' | 'ordered' | 'expired';
387
400
  /** A priced cart line. Prices are always computed server-side. */
388
401
  interface CartLine {
402
+ /**
403
+ * Line id: the argument of `carts.updateLine` / `carts.removeLine`. The
404
+ * variant id for a line without text, `<variantId>~<12 hex>` for a line
405
+ * with text. Required: needs a commerce API that returns it (older servers
406
+ * omit it; there, use `variantId`).
407
+ */
408
+ id: string;
389
409
  /** The variant this line holds. */
390
410
  variantId: string;
391
411
  /** Units, 1–99. */
392
412
  quantity: number;
413
+ /**
414
+ * The buyer's text for this line, or null. Required: older servers omit
415
+ * it (treat it as null there).
416
+ */
417
+ personalization: string | null;
418
+ /**
419
+ * False when the stored text no longer satisfies the product's current
420
+ * rule; checkout then answers `invalid_personalization`. Required: older
421
+ * servers omit it (treat it as true there).
422
+ */
423
+ personalizationValid: boolean;
393
424
  /** False when the variant is no longer sold. Checkout refuses the cart. */
394
425
  purchasable: boolean;
395
426
  /** False when fewer units are available than the line asks for. Checkout refuses the cart. */
@@ -481,8 +512,14 @@ interface Cart {
481
512
  interface AddLineInput {
482
513
  /** Variant id from the catalog. */
483
514
  variantId: string;
484
- /** Units to add, 1–99 (added to an existing line of the same variant). */
515
+ /** Units to add, 1–99 (added to an existing line of the same variant and text). */
485
516
  quantity: number;
517
+ /**
518
+ * The buyer's text, for a product that asks for one (see
519
+ * `Product.personalization`). Refused with `invalid_personalization` (400)
520
+ * when it breaks the product's rule, or when the product asks for none.
521
+ */
522
+ personalization?: string;
486
523
  }
487
524
  /** Body of `carts.updateLine`. */
488
525
  interface UpdateLineInput {
@@ -609,6 +646,8 @@ interface OrderLine {
609
646
  quantity: number;
610
647
  /** Line total, minor units. */
611
648
  lineTotalMinor: number;
649
+ /** The buyer's text for this line; absent or null when there is none. */
650
+ personalization?: string | null;
612
651
  }
613
652
  /** A shipment of an order. */
614
653
  interface OrderShipment {
@@ -831,10 +870,13 @@ interface StorefrontClient {
831
870
  get(cartId: string, options?: RequestOptions): Promise<Cart>;
832
871
  /** `POST /v1/carts/:id/lines`: adds units of a variant. */
833
872
  addLine(cartId: string, input: AddLineInput, options?: RequestOptions): Promise<Cart>;
834
- /** `PATCH /v1/carts/:id/lines/:variantId`: sets a line's quantity. */
835
- updateLine(cartId: string, variantId: string, input: UpdateLineInput, options?: RequestOptions): Promise<Cart>;
836
- /** `DELETE /v1/carts/:id/lines/:variantId`: removes a line. */
837
- removeLine(cartId: string, variantId: string, options?: RequestOptions): Promise<Cart>;
873
+ /**
874
+ * `PATCH /v1/carts/:id/lines/:lineId`: sets a line's quantity. `lineId`
875
+ * is `CartLine.id`; a variant id still matches a line without text.
876
+ */
877
+ updateLine(cartId: string, lineId: string, input: UpdateLineInput, options?: RequestOptions): Promise<Cart>;
878
+ /** `DELETE /v1/carts/:id/lines/:lineId`: removes a line (`CartLine.id`). */
879
+ removeLine(cartId: string, lineId: string, options?: RequestOptions): Promise<Cart>;
838
880
  /** `PUT /v1/carts/:id/customer`: sets the buyer. */
839
881
  setCustomer(cartId: string, input: CustomerInput, options?: RequestOptions): Promise<Cart>;
840
882
  /** `PUT /v1/carts/:id/address`: sets the shipping (and billing) address. */
@@ -952,6 +994,15 @@ interface StorefrontErrorDetailsMap {
952
994
  totalMinor: number;
953
995
  shippingOptions: ShippingOption[];
954
996
  };
997
+ /**
998
+ * A personalization text is refused (400, no details) or, at checkout
999
+ * (409), a line's stored text no longer satisfies the product's rule:
1000
+ * `lineIds` are those lines. Remove the line and add it again with a
1001
+ * valid text.
1002
+ */
1003
+ invalid_personalization: {
1004
+ lineIds?: string[];
1005
+ };
955
1006
  /** These variants cannot be bought (no longer sold, or not enough units). */
956
1007
  out_of_stock: {
957
1008
  variantIds: string[];
@@ -990,7 +1041,7 @@ interface StorefrontErrorDetailsMap {
990
1041
  };
991
1042
  }
992
1043
  /** Error codes of the Storefront API (the `error` field of an error response). */
993
- type StorefrontApiErrorCode = 'store_not_found' | 'origin_not_allowed' | 'invalid_request' | 'invalid_buyer_ip' | 'catalog_unavailable' | 'product_not_found' | 'content_not_found' | 'cart_not_found' | 'variant_not_found' | 'line_not_found' | 'insufficient_stock' | 'quantity_out_of_range' | 'too_many_lines' | 'invalid_shipping_option' | 'cart_read_only' | 'checkout_in_progress' | 'checkout_incomplete' | 'cart_changed' | 'out_of_stock' | 'amount_out_of_range' | 'nothing_to_pay' | 'invalid_buyer_details' | 'payment_unavailable' | 'checkout_unavailable' | 'order_not_found' | 'order_unavailable' | 'media_not_found' | 'media_unavailable' | 'challenge_required' | 'challenge_failed' | 'challenge_unavailable' | 'too_many_open_orders' | 'not_signed_in' | 'invalid_code' | 'profile_required' | 'too_many_code_requests' | 'invalid_recovery_token' | 'cart_not_recoverable' | 'unavailable';
1044
+ type StorefrontApiErrorCode = 'store_not_found' | 'origin_not_allowed' | 'invalid_request' | 'invalid_buyer_ip' | 'catalog_unavailable' | 'product_not_found' | 'content_not_found' | 'cart_not_found' | 'variant_not_found' | 'line_not_found' | 'insufficient_stock' | 'quantity_out_of_range' | 'too_many_lines' | 'invalid_shipping_option' | 'cart_read_only' | 'checkout_in_progress' | 'checkout_incomplete' | 'cart_changed' | 'invalid_personalization' | 'out_of_stock' | 'amount_out_of_range' | 'nothing_to_pay' | 'invalid_buyer_details' | 'payment_unavailable' | 'checkout_unavailable' | 'order_not_found' | 'order_unavailable' | 'media_not_found' | 'media_unavailable' | 'challenge_required' | 'challenge_failed' | 'challenge_unavailable' | 'too_many_open_orders' | 'not_signed_in' | 'invalid_code' | 'profile_required' | 'too_many_code_requests' | 'invalid_recovery_token' | 'cart_not_recoverable' | 'unavailable';
994
1045
  /**
995
1046
  * Codes the SDK assigns itself:
996
1047
  * - `rate_limited` — HTTP 429 without a known `error` code (the throttler);
@@ -1126,6 +1177,20 @@ declare function mountPayment(element: HTMLElement, presentation: PaymentPresent
1126
1177
  */
1127
1178
  declare function formatMoney(minor: number, currency: string, locale?: string | string[]): string;
1128
1179
 
1180
+ /** Whether `host` is a default store domain (`suffix` or below it). */
1181
+ declare function onDefaultDomain(host: string, suffix: string | undefined): boolean;
1182
+ /**
1183
+ * The pixels a host may load: Meta and TikTok write their cookies to the
1184
+ * registrable domain, which every default store domain shares, so they run
1185
+ * only on custom domains.
1186
+ */
1187
+ declare function pixelsForHost(p: AdPixels | undefined, defaultDomain: boolean): AdPixels | undefined;
1188
+ /**
1189
+ * The cookies a host lists: on a default domain without the
1190
+ * `customDomainOnly` ones, and none at all when no optional cookie is left.
1191
+ */
1192
+ declare function cookiesForHost(cookies: CookieInfo[], defaultDomain: boolean): CookieInfo[];
1193
+
1129
1194
  /**
1130
1195
  * The part of the Web Storage API the cart-id helpers use; `localStorage`,
1131
1196
  * `sessionStorage` or any object with these methods (a cookie adapter, a
@@ -1231,6 +1296,6 @@ interface TurnstileWidget {
1231
1296
  declare function renderTurnstile(element: HTMLElement, siteKey: string, options: RenderTurnstileOptions): Promise<TurnstileWidget>;
1232
1297
 
1233
1298
  /** Version of the Storefront SDK package. */
1234
- declare const SDK_VERSION = "0.4.0";
1299
+ declare const SDK_VERSION = "0.6.0";
1235
1300
 
1236
- export { type Account, type AccountOrder, type AccountProfileInput, type AccountSession, type AdPixels, type AddLineInput, type Address, type AddressInput, type AnyStorefrontError, BUYER_IP_HEADER, type BuyerDetailField, type Cart, type CartCustomer, type CartIdStorage, type CartLine, type CartStatus, type Category, type CategoryGridSection, type CategoryList, type CheckoutChallenge, type CheckoutEmbed, type CheckoutEmbedKind, type CheckoutMissing, type CheckoutOptions, type CheckoutResult, type CheckoutResultBase, type ClosedCheckoutPage, type ClosedCheckoutResult, type ClosedPaymentMount, type ContentNode, type CookieCategory, type CookieInfo, type CookieJar, type CustomerInput, DEFAULT_TIMEOUT_MS, type EmbeddedCheckoutResult, type EmbeddedPaymentMount, type FeaturedProductsSection, type HeroSection, type KnownThemeSection, type MountPaymentOptions, type Order, type OrderLine, type OrderShipment, type OrderStatus, PUBLISHABLE_KEY_HEADER, type Pagination, type PaymentMount, type PaymentPresentation, type PaymentStatus, type Product, type ProductListQuery, type ProductPage, type ProductVariant, type RedirectCheckoutResult, type RedirectPaymentMount, type RenderTurnstileOptions, type RequestCodeOptions, type RequestOptions, type RichTextSection, SDK_VERSION, STOREFRONT_KEY_HEADER, type SavedAddress, type SavedAddressInput, type ShipmentStatus, type ShippingOption, type ShippingOptionList, type ShippingRules, type Store, type StoreContact, type StoreContent, type StoreLegal, type StorefrontApiErrorCode, type StorefrontClient, type StorefrontClientErrorCode, type StorefrontClientOptions, StorefrontError, type StorefrontErrorCode, type StorefrontErrorDetails, type StorefrontErrorDetailsMap, THEME_FONTS, TURNSTILE_SCRIPT_URL, TURNSTILE_TOKEN_HEADER, type ThemeColors, type ThemeFont, type ThemeFooter, type ThemeHomepage, type ThemeLink, type ThemeSection, type ThemeSocial, type ThemeV1, type TurnstileApi, type TurnstileWidget, type UnknownThemeSection, type UnsupportedPaymentMount, type UpdateLineInput, type ValidationIssue, cartIdKey, clearCartId, cookieCartStorage, createStorefrontClient, formatMoney, isKnownThemeSection, isStorefrontError, loadCartId, loadTurnstile, mountPayment, renderTurnstile, saveCartId };
1301
+ export { type Account, type AccountOrder, type AccountProfileInput, type AccountSession, type AdPixels, type AddLineInput, type Address, type AddressInput, type AnyStorefrontError, BUYER_IP_HEADER, type BuyerDetailField, type Cart, type CartCustomer, type CartIdStorage, type CartLine, type CartStatus, type Category, type CategoryGridSection, type CategoryList, type CheckoutChallenge, type CheckoutEmbed, type CheckoutEmbedKind, type CheckoutMissing, type CheckoutOptions, type CheckoutResult, type CheckoutResultBase, type ClosedCheckoutPage, type ClosedCheckoutResult, type ClosedPaymentMount, type ContentNode, type CookieCategory, type CookieInfo, type CookieJar, type CustomerInput, DEFAULT_TIMEOUT_MS, type EmbeddedCheckoutResult, type EmbeddedPaymentMount, type FeaturedProductsSection, type HeroSection, type KnownThemeSection, type MountPaymentOptions, type Order, type OrderLine, type OrderShipment, type OrderStatus, PUBLISHABLE_KEY_HEADER, type Pagination, type PaymentMount, type PaymentPresentation, type PaymentStatus, type Product, type ProductListQuery, type ProductPage, type ProductPersonalization, type ProductVariant, type RedirectCheckoutResult, type RedirectPaymentMount, type RenderTurnstileOptions, type RequestCodeOptions, type RequestOptions, type RichTextSection, SDK_VERSION, STOREFRONT_KEY_HEADER, type SavedAddress, type SavedAddressInput, type ShipmentStatus, type ShippingOption, type ShippingOptionList, type ShippingRules, type Store, type StoreContact, type StoreContent, type StoreLegal, type StorefrontApiErrorCode, type StorefrontClient, type StorefrontClientErrorCode, type StorefrontClientOptions, StorefrontError, type StorefrontErrorCode, type StorefrontErrorDetails, type StorefrontErrorDetailsMap, THEME_FONTS, TURNSTILE_SCRIPT_URL, TURNSTILE_TOKEN_HEADER, type ThemeColors, type ThemeFont, type ThemeFooter, type ThemeHomepage, type ThemeLink, type ThemeSection, type ThemeSocial, type ThemeV1, type TurnstileApi, type TurnstileWidget, type UnknownThemeSection, type UnsupportedPaymentMount, type UpdateLineInput, type ValidationIssue, cartIdKey, clearCartId, cookieCartStorage, cookiesForHost, createStorefrontClient, formatMoney, isKnownThemeSection, isStorefrontError, loadCartId, loadTurnstile, mountPayment, onDefaultDomain, pixelsForHost, renderTurnstile, saveCartId };
package/dist/index.js CHANGED
@@ -18,6 +18,7 @@ var API_CODES = /* @__PURE__ */ new Set([
18
18
  "checkout_in_progress",
19
19
  "checkout_incomplete",
20
20
  "cart_changed",
21
+ "invalid_personalization",
21
22
  "out_of_stock",
22
23
  "amount_out_of_range",
23
24
  "nothing_to_pay",
@@ -120,6 +121,13 @@ function apiError(code, status, message, b) {
120
121
  totalMinor: n(b.totalMinor),
121
122
  shippingOptions: arr(b.shippingOptions)
122
123
  });
124
+ case "invalid_personalization":
125
+ return new StorefrontError(
126
+ code,
127
+ status,
128
+ message,
129
+ Array.isArray(b.lineIds) ? { lineIds: arr(b.lineIds) } : {}
130
+ );
123
131
  case "out_of_stock":
124
132
  return new StorefrontError(code, status, message, {
125
133
  variantIds: arr(b.variantIds)
@@ -334,18 +342,24 @@ function createStorefrontClient(options) {
334
342
  addLine: (cartId, input, o) => request(
335
343
  "POST",
336
344
  () => `${cart(cartId)}/lines`,
337
- { variantId: input.variantId, quantity: input.quantity },
345
+ {
346
+ variantId: input.variantId,
347
+ quantity: input.quantity,
348
+ ...input.personalization !== void 0 && {
349
+ personalization: input.personalization
350
+ }
351
+ },
338
352
  o
339
353
  ),
340
- updateLine: (cartId, variantId, input, o) => request(
354
+ updateLine: (cartId, lineId, input, o) => request(
341
355
  "PATCH",
342
- () => `${cart(cartId)}/lines/${seg(variantId)}`,
356
+ () => `${cart(cartId)}/lines/${seg(lineId)}`,
343
357
  { quantity: input.quantity },
344
358
  o
345
359
  ),
346
- removeLine: (cartId, variantId, o) => request(
360
+ removeLine: (cartId, lineId, o) => request(
347
361
  "DELETE",
348
- () => `${cart(cartId)}/lines/${seg(variantId)}`,
362
+ () => `${cart(cartId)}/lines/${seg(lineId)}`,
349
363
  void 0,
350
364
  o
351
365
  ),
@@ -532,6 +546,23 @@ function formatMoney(minor, currency, locale) {
532
546
  return format.format(minor / 10 ** digits);
533
547
  }
534
548
 
549
+ // src/host-pixels.ts
550
+ function onDefaultDomain(host, suffix) {
551
+ if (!suffix) return false;
552
+ const h = host.toLowerCase();
553
+ const s = suffix.toLowerCase();
554
+ return h === s || h.endsWith(`.${s}`);
555
+ }
556
+ function pixelsForHost(p, defaultDomain) {
557
+ if (!p || !defaultDomain) return p;
558
+ return p.google ? { google: p.google } : {};
559
+ }
560
+ function cookiesForHost(cookies, defaultDomain) {
561
+ if (!defaultDomain) return cookies;
562
+ const kept = cookies.filter((c) => !c.customDomainOnly);
563
+ return kept.some((c) => c.category !== "necessary") ? kept : [];
564
+ }
565
+
535
566
  // src/cart-storage.ts
536
567
  function cartIdKey(storeId) {
537
568
  return `storefront:cart:${storeId}`;
@@ -684,7 +715,7 @@ async function renderTurnstile(element, siteKey, options) {
684
715
  }
685
716
 
686
717
  // src/index.ts
687
- var SDK_VERSION = "0.4.0";
718
+ var SDK_VERSION = "0.6.0";
688
719
  export {
689
720
  BUYER_IP_HEADER,
690
721
  DEFAULT_TIMEOUT_MS,
@@ -698,6 +729,7 @@ export {
698
729
  cartIdKey,
699
730
  clearCartId,
700
731
  cookieCartStorage,
732
+ cookiesForHost,
701
733
  createStorefrontClient,
702
734
  formatMoney,
703
735
  isKnownThemeSection,
@@ -705,6 +737,8 @@ export {
705
737
  loadCartId,
706
738
  loadTurnstile,
707
739
  mountPayment,
740
+ onDefaultDomain,
741
+ pixelsForHost,
708
742
  renderTurnstile,
709
743
  saveCartId
710
744
  };