@thorprovider/storefront 0.6.3 → 0.7.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.
Files changed (29) hide show
  1. package/assets/templates/app/lib/__STOREFRONT__/actions.ts +649 -31
  2. package/assets/templates/app/lib/__STOREFRONT__/checkout-shipping.core.ts +37 -0
  3. package/assets/templates/app/lib/__STOREFRONT__/commerce.ts +117 -0
  4. package/assets/templates/app/lib/__STOREFRONT__/state.core.ts +7 -1
  5. package/assets/templates/app/lib/__STOREFRONT__/views/sections/index.ts +2 -1
  6. package/assets/templates/app/lib/__STOREFRONT__/views/sections/marketplace-product-card.client.tsx +19 -0
  7. package/assets/templates/app/lib/__STOREFRONT__/views/sections/marketplace-product-card.tsx +16 -0
  8. package/assets/templates/app/lib/__STOREFRONT__/views/sections/schema.marketplace-product-card.ts +67 -0
  9. package/assets/templates/features/checkout/handlers.ts +326 -31
  10. package/assets/templates/features/checkout/state.ts +79 -3
  11. package/assets/templates/views/checkout/content.ts +63 -2
  12. package/assets/templates/views/checkout/index.tsx +6 -0
  13. package/assets/templates/views/checkout/props.ts +61 -21
  14. package/assets/templates/views/checkout/registry.tsx +300 -33
  15. package/assets/templates/views/checkout/schema.ts +184 -8
  16. package/assets/templates/views/home/assembly/assembly.json +7 -0
  17. package/package.json +1 -1
  18. package/recipes/recipe.schema.json +1 -1
  19. package/recipes/recipes.json +2 -1
  20. package/recipes/sections.json +9 -0
  21. package/scripts/file-classes.mjs +1 -0
  22. package/skills/sf-view-gen/SKILL.md +6 -0
  23. package/skills/sf-view-gen/assets/recipes/recipes.json +2 -1
  24. package/skills/sf-view-gen/assets/recipes/sections.json +9 -0
  25. package/skills/sf-view-gen/assets/templates/views/sections/marketplace-product-card.client.tsx +19 -0
  26. package/skills/sf-view-gen/assets/templates/views/sections/marketplace-product-card.tsx +16 -0
  27. package/skills/sf-view-gen/assets/templates/views/sections/schema.marketplace-product-card.ts +67 -0
  28. package/skills/storefront-workflow/SKILL.md +5 -0
  29. package/skills/storefront-workflow/references/mobile-responsiveness.md +1 -1
@@ -0,0 +1,37 @@
1
+ // Generated by Thor Commerce storefront (template: assets/templates/app/lib/__STOREFRONT__/checkout-shipping.core.ts)
2
+ //
3
+ // Pure grouped-delivery host helper. No React, no Next, no I/O: the checkout
4
+ // handlers and their tests share exactly one selection rule, and the module
5
+ // stays importable from a client component.
6
+ //
7
+ // The backend owns grouping. This helper only translates the presentation
8
+ // groups the mapper produced plus their backend `products` identity into the
9
+ // selection payload the grouped apply expects; it never groups, splits, ranks,
10
+ // or re-prices a shipment.
11
+
12
+ import type { CheckoutShippingGroup } from '@thorprovider/blocks'
13
+ import type { ShippingGroupSelection } from '@thorprovider/types'
14
+
15
+ /**
16
+ * Build ONE backend selection per returned delivery group, or `null` while any
17
+ * group is still incomplete: an empty group list (no delivery to select), a
18
+ * group without options, a group without a chosen option, or a group without
19
+ * its backend `products` identity. Otherwise the selections keep the backend's
20
+ * group order and carry that group's `products` verbatim plus its chosen
21
+ * `option_id`.
22
+ */
23
+ export function toShippingSelections(
24
+ groups: CheckoutShippingGroup[],
25
+ productsByGroup: Record<string, string[]>,
26
+ ): ShippingGroupSelection[] | null {
27
+ if (groups.length === 0) return null
28
+ const selections: ShippingGroupSelection[] = []
29
+ for (const group of groups) {
30
+ const products = productsByGroup[group.id]
31
+ if (!Array.isArray(products) || products.length === 0) return null
32
+ if (group.options.length === 0) return null
33
+ if (!group.selectedOptionId) return null
34
+ selections.push({ products, option_id: group.selectedOptionId })
35
+ }
36
+ return selections
37
+ }
@@ -19,26 +19,38 @@
19
19
  // the committed cart (`completedAt`) and completes once only when committed.
20
20
 
21
21
  import type {
22
+ Address,
22
23
  Cart,
24
+ CheckoutAddressOption,
25
+ CreateAddressData,
23
26
  Customer,
24
27
  LoginCredentials,
25
28
  Money,
26
29
  Order,
27
30
  PaymentMethod,
28
31
  RegisterData,
32
+ ShippingGroupQuote,
33
+ ShippingGroupSelection,
29
34
  ShippingMethod,
30
35
  StoreContext,
31
36
  } from '@thorprovider/types'
32
37
  import {
33
38
  addCartLines,
39
+ applyCheckoutShippingGroups as applyCheckoutShippingGroupsRuntime,
34
40
  applyManualTestPayment,
35
41
  APPROVED_MANUAL_TEST_PROVIDER_ID,
36
42
  completeCart,
37
43
  createCartForContext,
44
+ createCustomerAddress as createCustomerAddressRuntime,
45
+ deleteCustomerAddress as deleteCustomerAddressRuntime,
38
46
  getCartForContext,
39
47
  getCheckoutCountries,
48
+ getCheckoutShippingGroups as getCheckoutShippingGroupsRuntime,
40
49
  getCurrentCustomer,
50
+ getCustomerAddresses,
41
51
  getCustomerOrders,
52
+ getGeoCities,
53
+ getGeoProvinces,
42
54
  getCustomerOrdersPage,
43
55
  getEligibleShippingMethods,
44
56
  getPaymentMethodsByRegion,
@@ -51,14 +63,19 @@ import {
51
63
  selectFirstPurchasableVariant,
52
64
  selectShippingMethod,
53
65
  toAbsoluteLineUpdate,
66
+ updateBillingAddress,
54
67
  updateCartLines,
68
+ updateCustomerAddress as updateCustomerAddressRuntime,
55
69
  updateShippingAddress,
56
70
  validateCheckoutAddressForRegion,
71
+ validateCustomerAddressForRegion,
57
72
  type AuthOutcome,
73
+ type BillingAddressInput,
58
74
  type CartCompletionResult,
59
75
  type CatalogProductDetail,
60
76
  type CheckoutAddress,
61
77
  type CheckoutFieldError,
78
+ type CustomerAddress,
62
79
  type RegionCountry,
63
80
  } from '@thorprovider/runtime/commerce'
64
81
  import type {
@@ -227,6 +244,14 @@ export function toCheckoutShippingOptions(methods: ShippingMethod[]): CheckoutSh
227
244
  }))
228
245
  }
229
246
 
247
+ export {
248
+ checkoutShippingGroupId,
249
+ toCheckoutShippingGroups,
250
+ } from '@thorprovider/runtime/commerce/shipping'
251
+ export type {
252
+ CheckoutShippingGroupsMapping,
253
+ } from '@thorprovider/runtime/commerce/shipping'
254
+
230
255
  /** Group the runtime's structured field errors into the block's error shape. */
231
256
  export function toCheckoutFieldErrors(errors: CheckoutFieldError[]): CheckoutFieldErrors {
232
257
  const grouped: CheckoutFieldErrors = {}
@@ -306,6 +331,16 @@ export async function loadCheckoutCountries(
306
331
  return getCheckoutCountries(cartId, context)
307
332
  }
308
333
 
334
+ /** Persist a billing address distinct from shipping, when the provider supports it. */
335
+ export async function saveBillingAddress(
336
+ cartId: string,
337
+ address: BillingAddressInput,
338
+ context: StoreContext = getStoreContext(),
339
+ ): Promise<void> {
340
+ assertBackendConfigured()
341
+ return updateBillingAddress(cartId, address)
342
+ }
343
+
309
344
  export async function loadShippingMethods(
310
345
  cartId: string,
311
346
  context: StoreContext = getStoreContext(),
@@ -314,6 +349,19 @@ export async function loadShippingMethods(
314
349
  return getEligibleShippingMethods(cartId, context)
315
350
  }
316
351
 
352
+ /**
353
+ * Request the backend's grouped shipping quote for the cart. The groups are the
354
+ * backend's own decision (no local grouping); a provider without the grouped
355
+ * capability fails explicitly instead of returning an empty quote.
356
+ */
357
+ export async function loadCheckoutShippingGroups(
358
+ cartId: string,
359
+ context: StoreContext = getStoreContext(),
360
+ ): Promise<ShippingGroupQuote> {
361
+ assertBackendConfigured()
362
+ return getCheckoutShippingGroupsRuntime(cartId, context)
363
+ }
364
+
317
365
  /**
318
366
  * Label the approved manual TEST provider explicitly as a test provider rather
319
367
  * than letting a legacy adapter name (for example "Cash on Delivery") imply a
@@ -473,6 +521,21 @@ export async function selectShipping(
473
521
  return toCartState(cart)
474
522
  }
475
523
 
524
+ /**
525
+ * Apply one selected option per backend group, then return the RE-READ cart.
526
+ * The runtime validates the cart context and re-reads the authoritative cart;
527
+ * per-group amounts are never summed here or used as totals.
528
+ */
529
+ export async function applyCheckoutShippingGroups(
530
+ cartId: string,
531
+ selections: ShippingGroupSelection[],
532
+ context: StoreContext = getStoreContext(),
533
+ ): Promise<CartStatePatch> {
534
+ assertBackendConfigured()
535
+ const cart = await applyCheckoutShippingGroupsRuntime(cartId, selections, context)
536
+ return toCartState(cart)
537
+ }
538
+
476
539
  /** Apply the single approved manual TEST payment provider (requires shipping). */
477
540
  export async function applyPayment(
478
541
  cartId: string,
@@ -534,6 +597,60 @@ export async function loadCustomer(token: string | null): Promise<Customer | nul
534
597
  return getCurrentCustomer(token)
535
598
  }
536
599
 
600
+ /** Read the authenticated customer's saved addresses for checkout prefill. */
601
+ export async function loadCustomerAddresses(token: string): Promise<Address[]> {
602
+ return getCustomerAddresses(token)
603
+ }
604
+
605
+ /** Validate a saved-address payload without the contact email. */
606
+ export function validateCustomerAddress(
607
+ input: unknown,
608
+ countries: RegionCountry[],
609
+ ): { success: true; address: CustomerAddress } | { success: false; errors: CheckoutFieldError[] } {
610
+ return validateCustomerAddressForRegion(input, countries)
611
+ }
612
+
613
+ /** Create an address in the authenticated customer's address book. */
614
+ export async function createCustomerAddress(
615
+ token: string,
616
+ data: CreateAddressData,
617
+ ): Promise<Address> {
618
+ return createCustomerAddressRuntime(token, data)
619
+ }
620
+
621
+ /** Update an address in the authenticated customer's address book. */
622
+ export async function updateCustomerAddress(
623
+ token: string,
624
+ addressId: string,
625
+ data: Partial<CreateAddressData>,
626
+ ): Promise<Address> {
627
+ return updateCustomerAddressRuntime(token, addressId, data)
628
+ }
629
+
630
+ /** Delete an address from the authenticated customer's address book. */
631
+ export async function deleteCustomerAddress(
632
+ token: string,
633
+ addressId: string,
634
+ ): Promise<void> {
635
+ return deleteCustomerAddressRuntime(token, addressId)
636
+ }
637
+
638
+ /** Provinces/states from the provider geo-reference for one country. */
639
+ export async function loadGeoProvinces(countryCode: string): Promise<CheckoutAddressOption[]> {
640
+ return getGeoProvinces(countryCode)
641
+ }
642
+
643
+ /** Cities from the provider geo-reference for one province (name or code). */
644
+ export async function loadGeoCities(
645
+ province: string,
646
+ countryCode?: string,
647
+ ): Promise<CheckoutAddressOption[]> {
648
+ return getGeoCities(province, countryCode)
649
+ }
650
+
651
+ /** Pure checkout prefill helper; the host supplies cart, profile, and addresses. */
652
+ export { prefillCheckoutFromCustomer } from '@thorprovider/runtime/commerce'
653
+
537
654
  // ---------------------------------------------------------------------------
538
655
  // Customer orders boundary (change: storefront-customer-orders, task 2.1)
539
656
  // ---------------------------------------------------------------------------
@@ -7,6 +7,8 @@
7
7
 
8
8
  import type {
9
9
  CardProduct,
10
+ CheckoutReadState,
11
+ CheckoutShippingGroup,
10
12
  FeaturedProduct,
11
13
  ProductCategoryItem,
12
14
  ProductDetailProduct,
@@ -211,9 +213,13 @@ export interface StorefrontState extends StorefrontCoreState {
211
213
  checkout: CheckoutFormData
212
214
  checkoutCountries: { value: string; label: string }[]
213
215
  checkoutPaymentMethods: PaymentMethod[]
214
- checkoutShippingOptions: CheckoutShippingOption[]
215
216
  checkoutSelectedShippingOptionId: string | null
216
217
  checkoutFieldErrors: CheckoutFieldErrors
218
+ /** The backend's own delivery groups, mapped 1:1 (never locally grouped). */
219
+ checkoutShippingGroups: CheckoutShippingGroup[]
220
+ /** Presentation group id -> the backend `products` for that group's apply. */
221
+ checkoutShippingGroupProducts: Record<string, string[]>
222
+ checkoutShippingState: CheckoutReadState
217
223
  checkoutPending: boolean
218
224
  checkoutError: string | null
219
225
  /**
@@ -8,9 +8,10 @@
8
8
  import type { ViewCatalogEntry } from '../types'
9
9
  import { ctaSection } from './cta'
10
10
  import { faqSection } from './faq'
11
+ import { marketplaceProductCardSection } from './marketplace-product-card'
11
12
  import { productsSection } from './products'
12
13
 
13
- export const sectionModules = [faqSection, ctaSection, productsSection] as const
14
+ export const sectionModules = [faqSection, ctaSection, marketplaceProductCardSection, productsSection] as const
14
15
 
15
16
  export const sectionCatalog: Record<string, ViewCatalogEntry> = Object.assign(
16
17
  {},
@@ -0,0 +1,19 @@
1
+ 'use client'
2
+
3
+ // Optional section fragment: marketplace-product-card (mirrors recipes/sections.json) — client registry.
4
+ // The matching server-side catalog lives in marketplace-product-card.tsx. @thorprovider/blocks ships a
5
+ // 'use client' boundary, but this fragment keeps the explicit client boundary so
6
+ // the section composes identically in any consumer graph.
7
+
8
+ import { MarketplaceProductCard } from '@thorprovider/blocks'
9
+ import { marketplaceProductCardPropsSchema } from './schema.marketplace-product-card'
10
+ import type { ViewRegistry } from '../types'
11
+
12
+ export const marketplaceProductCardRegistry: ViewRegistry = {
13
+ MarketplaceProductCard: ({ props }) => {
14
+ const parsed = marketplaceProductCardPropsSchema.parse(props)
15
+ return (
16
+ <MarketplaceProductCard data={parsed.data} />
17
+ )
18
+ },
19
+ }
@@ -0,0 +1,16 @@
1
+ // Optional section fragment: marketplace-product-card (mirrors recipes/sections.json)
2
+ // Server-side part: element type + zod catalog entry. The component mappings
3
+ // live in marketplace-product-card.client.tsx ('use client') and are composed via composeSections.
4
+
5
+ import { marketplaceProductCardPropsSchema } from './schema.marketplace-product-card'
6
+
7
+ export const marketplaceProductCardSection = {
8
+ elementType: 'MarketplaceProductCard',
9
+ catalog: {
10
+ MarketplaceProductCard: {
11
+ props: marketplaceProductCardPropsSchema,
12
+ description: 'MarketplaceProductCard presentation section (delivery marketplace-product-card).',
13
+ },
14
+ },
15
+ registryModule: './marketplace-product-card.client',
16
+ }
@@ -0,0 +1,67 @@
1
+ // Optional section fragment: marketplace-product-card (mirrors recipes/sections.json) — shared contract.
2
+ // Zod mirror of the MarketplaceProductCard block contract (delivery marketplace-product-card, contract version 3.0.2),
3
+ // shared by the server catalog (marketplace-product-card.tsx) and the client registry (marketplace-product-card.client.tsx).
4
+ // The React-only callbacks (onAddToCart, onToggleWishlist) are intentionally excluded.
5
+
6
+ import { z } from 'zod'
7
+ import { moneySchema } from '@thorprovider/blocks/contracts'
8
+
9
+ export const marketplaceProductCardDataSchema = z.object({
10
+ product: z.object({
11
+ id: z.string(),
12
+ brand: z.string(),
13
+ title: z.string(),
14
+ badge: z.string(),
15
+ rating: z.number(),
16
+ reviewCount: z.number(),
17
+ ratingLabel: z.string(),
18
+ soldLabel: z.string(),
19
+ shippingLabel: z.string(),
20
+ deliveryLabel: z.string(),
21
+ currency: z.string(),
22
+ locale: z.string(),
23
+ image: z.string(),
24
+ options: z.array(z.object({
25
+ id: z.string(),
26
+ name: z.string(),
27
+ kind: z.enum(["image", "text"]),
28
+ values: z.array(z.object({
29
+ id: z.string(),
30
+ label: z.string(),
31
+ image: z.string().optional(),
32
+ imageAlt: z.string().optional(),
33
+ })),
34
+ })),
35
+ variants: z.array(z.object({
36
+ id: z.string(),
37
+ optionValueIds: z.object({
38
+ colour: z.string(),
39
+ size: z.string(),
40
+ }),
41
+ price: moneySchema,
42
+ compareAtPrice: moneySchema.optional(),
43
+ available: z.boolean(),
44
+ stockLabel: z.string(),
45
+ stockTone: z.enum(["success", "attention", "destructive"]),
46
+ })),
47
+ }),
48
+ labels: z.object({
49
+ addToCart: z.string(),
50
+ wishlistAdd: z.string(),
51
+ wishlistRemove: z.string(),
52
+ unavailableOption: z.string(),
53
+ unavailablePrice: z.string(),
54
+ outOfStock: z.string(),
55
+ optionSeparator: z.string(),
56
+ }),
57
+ glyphs: z.object({
58
+ fullStar: z.string(),
59
+ emptyStar: z.string(),
60
+ heart: z.string(),
61
+ heartFilled: z.string(),
62
+ }),
63
+ })
64
+
65
+ export const marketplaceProductCardPropsSchema = z.object({
66
+ data: marketplaceProductCardDataSchema,
67
+ })