-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/storefront data #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feat/storefront data #290
Changes from all commits
Commits
Show all changes
191 commits
Select commit
Hold shift + click to select a range
28750a4
feat(storefront-data): add shared data fetching library
KaiUweCZE 993b5c1
docs(storefront-data): add README and AGENTS.md
KaiUweCZE 560d81d
fix(storefront-data): align dist types with package exports
KaiUweCZE 4094a3d
test(storefront-data): add smoke tests with vitest infrastructure
KaiUweCZE 3279bf5
test(storefront-data): add MSW infrastructure and nx test target
KaiUweCZE 93fdedb
feat(storefront-data): add RegionInfo shared type
KaiUweCZE fe14eab
feat(storefront-data): add auth module
KaiUweCZE d5c2e4a
feat(storefront-data): add cart module
KaiUweCZE 83cc0b7
feat(storefront-data): add checkout module
KaiUweCZE 8ba0042
feat(storefront-data): add useCreateCustomer hook and fix Rules of Hooks
KaiUweCZE abe1a57
fix(storefront-data): avoid hooks-in-callback by resolving region in …
KaiUweCZE 64945ba
feat(storefront-data): add update cart address helper
KaiUweCZE 79a454b
feat(storefront-data): add infinite products hook
KaiUweCZE 0909c97
feat(storefront-data): add orders and customer hooks
KaiUweCZE 862c00e
test(storefront-data): add MSW smoke test for new hooks
KaiUweCZE 80c12c6
merge master into feat/storefront-data
KaiUweCZE 1e5cb05
update AGENTS.md and README.md
KaiUweCZE 897dfda
build(storefront-data): use rslib build
KaiUweCZE a57d5f1
fix(auth): remove redundant invalidate on logout
KaiUweCZE a83f26f
refactor(cart): strip address-only fields before update
KaiUweCZE 8fc3be7
feat(cart): optionally clear cart on update
KaiUweCZE e5bfa67
refactor(cart): memoize prefetchCart
KaiUweCZE fff3c4f
chore(cart/types): drop add line item metadata field
KaiUweCZE b7909ac
fix(categories): omit enabled from query params
KaiUweCZE 655ac15
refactor(categories): tighten query key types
KaiUweCZE 44481b0
feat(checkout):expose payment providers loading state
KaiUweCZE db41aa8
fix(collections): clear prefetch timeouts on unmount
KaiUweCZE 1e794ed
docs(customer): add createCustomerHooks example
KaiUweCZE 3a887ba
refactor(orders): type-safe suspense inputs
KaiUweCZE 3710827
fix(products): strip enabled + clear prefetch timeouts
KaiUweCZE 9c5e022
perf(products): memoize base input in prefetch
KaiUweCZE cea9949
fix(pagination): derive page from explicit offset
KaiUweCZE f8f0fbc
refactor(products): require namespace in query keys
KaiUweCZE ab05fa4
fix(products): pass abort signal to global fetcher
KaiUweCZE 0f52d73
fix(regions): omit enabled from query params
KaiUweCZE e7d69cc
fix(regions): omit enabled and add abort signal
KaiUweCZE 476d0a0
docs(shared): clarify query client config merge
KaiUweCZE d823f4b
chore(query-client): warn on ignored overrides
KaiUweCZE 8664d2c
fix(query-keys): detect circular inputs
KaiUweCZE 9fde8e1
test(network): type json response
KaiUweCZE a5edd71
fix: omit enabled from query paramas in collections and products
KaiUweCZE 9a697ad
refactor(cart): normalize paylod inputs
KaiUweCZE 6eb811c
feat(products): allow prefetch skipIfCached override
KaiUweCZE 87491be
fix(query-client): redact errors on dehydrate
KaiUweCZE ae54a4f
fix(cart): sync active cart cache key
KaiUweCZE b228935
test(storefront-data): add missing hook coverage
KaiUweCZE 4ec9168
docs(storefront-data): add notes and test command
KaiUweCZE 2e3f11e
refactor(storefront-data): improve types and cleanup patterns
KaiUweCZE 38e8f0d
refactor(storefront-data): improve types and avoid object mutation
KaiUweCZE 596641e
feat(storefrontd-data): pass AbortSignal to detail fetchers
KaiUweCZE e1516f9
refactor(storefront-data): simplify checkout shipping logic
KaiUweCZE 2a5506a
test(storefront-data): add QueryClient cleanup in smoke tests
KaiUweCZE 740fc87
fix(storefront-data): enable refetchOnReconnect for realtime cache
KaiUweCZE 8d98798
feat(storefront-data): export createOrderQueryKeys from orders
KaiUweCZE 764b7c9
refactor(storefront-data): formatting and stricter type casts
KaiUweCZE 230b035
feat(cart): add mutation lifecycle hooks and cache invalidation option
KaiUweCZE 3d76987
feat(region): add RegionProvider and integrate context into product h…
KaiUweCZE 8cf9927
feat(checkout): add full mutation lifecycle support
KaiUweCZE 104baf0
feat(auth): add Medusa auth service and export types for SDK integration
KaiUweCZE 0f0aa2b
feat(storefront-data): add Medusa region service and exports
KaiUweCZE 2b2917f
chore(deps): add @medusajs/types peerDependency to storefront-data pa…
KaiUweCZE 78a84ca
feat(orders): add Medusa order service and exports
KaiUweCZE 8873d42
feat(cart): add Medusa cart service and re-export types
KaiUweCZE 461f299
feat(customers): add Medusa customer and types
KaiUweCZE 830ea62
refactor: expose query objects and add queryOptions support
KaiUweCZE 221c19f
Merge branch 'master' into feat/storefront-data
KaiUweCZE 8594d60
chore(deps): bump React type packages to patch versions to fix type e…
KaiUweCZE 19cc1d4
feat(products): support initialLimit for infinite product lists
KaiUweCZE 4ce34b9
feat(checkout): add Med checkout service and payment provider hooks
KaiUweCZE fff3888
feat(auth): add cross-domain query invalidation on auth changes and
KaiUweCZE a185fd2
feat(cart): normalize query key and improve prefetch behavior
KaiUweCZE 2000109
feat(categories): add prefetch skip modes and signal-aware calls
KaiUweCZE 0c49a4a
feat(checkout): strengthen types and normalize query keys
KaiUweCZE 2174b28
feat(collections): add Medusa collection service with abortable calls
KaiUweCZE 04fb305
feat(customers): improve auth handling and query key normalization
KaiUweCZE 7680ab2
feat(products): normalize query keys and improve prefetch logic
KaiUweCZE b566d08
feat(storefront-data): normalize order query key params
KaiUweCZE b5ba9a7
feat(regions): normalize query keys and improve prefetch logic
KaiUweCZE 8cb61ad
fix(query-client): remove unused getQueryClient export
KaiUweCZE c58ad95
feat(prefetch,query-keys): add prefetch helpers and normalize query p…
KaiUweCZE 6d938b8
test: add Medusa service unit tests for checkout and categories
KaiUweCZE 6981064
docs(storefront-data): expand hooks, docs, and query-key helpers
KaiUweCZE 508846e
fix(cart-service): respect isNotFoundError config and recover stale c…
KaiUweCZE 8d65f4e
Merge pull request #322 from NMIT-WR/lchylik/feat/slect-open-state-tr…
BleedingDev 7575c57
Merge pull request #324 from NMIT-WR/chore/react19-monorepo-consistency
BleedingDev 76d2428
feat(storefront-data): add shared data fetching library
KaiUweCZE c5bc0e2
docs(storefront-data): add README and AGENTS.md
KaiUweCZE 1ea7eee
fix(storefront-data): align dist types with package exports
KaiUweCZE 6c5b09b
test(storefront-data): add smoke tests with vitest infrastructure
KaiUweCZE ef2234d
test(storefront-data): add MSW infrastructure and nx test target
KaiUweCZE 8741e4b
feat(storefront-data): add RegionInfo shared type
KaiUweCZE b0a545c
feat(storefront-data): add auth module
KaiUweCZE c07cc1b
feat(storefront-data): add cart module
KaiUweCZE e6e5cbf
feat(storefront-data): add checkout module
KaiUweCZE 44847a9
feat(storefront-data): add useCreateCustomer hook and fix Rules of Hooks
KaiUweCZE a14eaab
fix(storefront-data): avoid hooks-in-callback by resolving region in …
KaiUweCZE a265fc4
feat(storefront-data): add update cart address helper
KaiUweCZE a4f29b0
feat(storefront-data): add infinite products hook
KaiUweCZE b5d8900
feat(storefront-data): add orders and customer hooks
KaiUweCZE 1096aa3
test(storefront-data): add MSW smoke test for new hooks
KaiUweCZE d6e0fa9
merge master into feat/storefront-data
KaiUweCZE 4e8ccd7
update AGENTS.md and README.md
KaiUweCZE c5985b6
build(storefront-data): use rslib build
KaiUweCZE 8c03db8
fix(auth): remove redundant invalidate on logout
KaiUweCZE 42354d2
refactor(cart): strip address-only fields before update
KaiUweCZE dd89de0
feat(cart): optionally clear cart on update
KaiUweCZE cb2a486
refactor(cart): memoize prefetchCart
KaiUweCZE 40800e4
chore(cart/types): drop add line item metadata field
KaiUweCZE c0b03fc
fix(categories): omit enabled from query params
KaiUweCZE 1c2cc57
refactor(categories): tighten query key types
KaiUweCZE 72449cb
feat(checkout):expose payment providers loading state
KaiUweCZE b2bfe91
fix(collections): clear prefetch timeouts on unmount
KaiUweCZE 363f249
docs(customer): add createCustomerHooks example
KaiUweCZE edd4bfe
refactor(orders): type-safe suspense inputs
KaiUweCZE b2b450b
fix(products): strip enabled + clear prefetch timeouts
KaiUweCZE 85d607d
perf(products): memoize base input in prefetch
KaiUweCZE a19f985
fix(pagination): derive page from explicit offset
KaiUweCZE 78b6772
refactor(products): require namespace in query keys
KaiUweCZE a3a3c15
fix(products): pass abort signal to global fetcher
KaiUweCZE 2451886
fix(regions): omit enabled from query params
KaiUweCZE 1c9a6b0
fix(regions): omit enabled and add abort signal
KaiUweCZE a5b8251
docs(shared): clarify query client config merge
KaiUweCZE 9ed9755
chore(query-client): warn on ignored overrides
KaiUweCZE ab10c5b
fix(query-keys): detect circular inputs
KaiUweCZE 4455c41
test(network): type json response
KaiUweCZE cb6c252
fix: omit enabled from query paramas in collections and products
KaiUweCZE 4a7e341
refactor(cart): normalize paylod inputs
KaiUweCZE 1a1bda8
feat(products): allow prefetch skipIfCached override
KaiUweCZE 5ae3645
fix(query-client): redact errors on dehydrate
KaiUweCZE 300ad0f
fix(cart): sync active cart cache key
KaiUweCZE 04d3bb5
test(storefront-data): add missing hook coverage
KaiUweCZE 12e9216
docs(storefront-data): add notes and test command
KaiUweCZE 072b14e
refactor(storefront-data): improve types and cleanup patterns
KaiUweCZE 98360a4
refactor(storefront-data): improve types and avoid object mutation
KaiUweCZE 4df18db
feat(storefrontd-data): pass AbortSignal to detail fetchers
KaiUweCZE 2c2d3a1
refactor(storefront-data): simplify checkout shipping logic
KaiUweCZE 0c1dca1
test(storefront-data): add QueryClient cleanup in smoke tests
KaiUweCZE 7a8ad6c
fix(storefront-data): enable refetchOnReconnect for realtime cache
KaiUweCZE d04cb7e
feat(storefront-data): export createOrderQueryKeys from orders
KaiUweCZE b777c30
refactor(storefront-data): formatting and stricter type casts
KaiUweCZE 8ddf7d3
feat(cart): add mutation lifecycle hooks and cache invalidation option
KaiUweCZE 9d8aecd
feat(region): add RegionProvider and integrate context into product h…
KaiUweCZE aef021d
feat(checkout): add full mutation lifecycle support
KaiUweCZE 990dcd3
feat(auth): add Medusa auth service and export types for SDK integration
KaiUweCZE 837f80f
feat(storefront-data): add Medusa region service and exports
KaiUweCZE 6dbe22a
chore(deps): add @medusajs/types peerDependency to storefront-data pa…
KaiUweCZE b2c4d82
feat(orders): add Medusa order service and exports
KaiUweCZE 438e1ef
feat(cart): add Medusa cart service and re-export types
KaiUweCZE ef11aea
feat(customers): add Medusa customer and types
KaiUweCZE 08f7386
refactor: expose query objects and add queryOptions support
KaiUweCZE 91fe1db
Merge branch 'master' into feat/storefront-data
KaiUweCZE 8a502d0
chore(deps): bump React type packages to patch versions to fix type e…
KaiUweCZE d3a28f3
feat(products): support initialLimit for infinite product lists
KaiUweCZE 4169625
feat(checkout): add Med checkout service and payment provider hooks
KaiUweCZE 3111e41
feat(auth): add cross-domain query invalidation on auth changes and
KaiUweCZE 78193c8
feat(cart): normalize query key and improve prefetch behavior
KaiUweCZE 03333c2
feat(categories): add prefetch skip modes and signal-aware calls
KaiUweCZE de80457
feat(checkout): strengthen types and normalize query keys
KaiUweCZE 05fad69
feat(collections): add Medusa collection service with abortable calls
KaiUweCZE 1275651
feat(customers): improve auth handling and query key normalization
KaiUweCZE 21dadd2
feat(products): normalize query keys and improve prefetch logic
KaiUweCZE ca3086e
feat(storefront-data): normalize order query key params
KaiUweCZE da471ca
feat(regions): normalize query keys and improve prefetch logic
KaiUweCZE 44bb2ee
fix(query-client): remove unused getQueryClient export
KaiUweCZE 4c0fc32
feat(prefetch,query-keys): add prefetch helpers and normalize query p…
KaiUweCZE 102a87a
test: add Medusa service unit tests for checkout and categories
KaiUweCZE 061e3f7
docs(storefront-data): expand hooks, docs, and query-key helpers
KaiUweCZE 7265779
fix(cart-service): respect isNotFoundError config and recover stale c…
KaiUweCZE 130f676
merge master into storefront-data
KaiUweCZE fbdd198
Merge branch 'feat/storefront-data' of https://github.com/NMIT-WR/new…
KaiUweCZE 33a1759
fix(storefront-data): report auth logout errors and sanitize cart pay…
KaiUweCZE a1ce79b
refactor(storefront-data): remove barrel exports and normalize cart p…
KaiUweCZE 35f32f3
docs(auth): add tanstack source link
KaiUweCZE cbe5324
merge master into storefront-data
KaiUweCZE 5a33e01
feat(storefront-data): add catalog module with hooks, query keys, and…
KaiUweCZE 73bca5b
remove BOM
KaiUweCZE e93b1c2
fix(storefront-data): stabilize ssr hydration, hooks safety and custo…
KaiUweCZE 1ce2b37
fix(storefront-data): add abort-signal cancellation across medusa rea…
KaiUweCZE 1c2c451
fix: contracts and abort-signal reads
KaiUweCZE 562053f
fix(storefront-data): harden exports and normalize customer validatio…
KaiUweCZE bb02b83
fix(storefront-data): harden server query-client server-only boundary
KaiUweCZE 51a3ec5
refactor(storefront-data): centralize medusa error handling helpers
KaiUweCZE c4b9ff8
refactor(storefront-data): unify query-key utilities and shared key t…
KaiUweCZE 03d35c8
refactor(storefront-data): improve catalog/product query ergonomics a…
KaiUweCZE 28a84e4
refactor(storefront-data): reduce hook and service duplication across…
KaiUweCZE 4055285
docs(storefront-data): clarify provider singleton config behavior
KaiUweCZE 5479a16
test(storefront-data): improve edge-case coverage and test setup hygiene
KaiUweCZE 2f43e4a
refactor(products): cache ergonomics and docs clarify products
KaiUweCZE 2130648
fix(storefront-data): harden hooks and services and add phase regress…
KaiUweCZE c44da93
fix(storefront-data): harden auth/customer flows and fix hook invariants
KaiUweCZE cb4ff05
refactor(storefront-data): consolidate hook error/prefetch utilities …
KaiUweCZE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| # Storefront Data Library (`@techsio/storefront-data`) | ||
|
|
||
| TanStack Query + Medusa.js data fetching library with factory pattern hooks. | ||
|
|
||
| NOTE: `libs/storefront-data/AGENTS.md` is the canonical source of truth. | ||
| - `libs/storefront-data/CLAUDE.md` is a symlink to this file. | ||
| - Edit only `AGENTS.md`. | ||
| - Windows: enable Developer Mode and set `git config core.symlinks true` so symlinks work. | ||
|
|
||
| ## Stack | ||
|
|
||
| | Technology | Version | Purpose | | ||
| |------------|---------|---------| | ||
| | TanStack Query | 5+ | Data fetching, caching, SSR hydration | | ||
| | Medusa.js SDK | 2.12+ | E-commerce backend API | | ||
| | React | 19+ | UI framework | | ||
| | TypeScript | 5+ | Type safety with generics | | ||
|
|
||
| ## Structure | ||
|
|
||
| ```text | ||
| src/ | ||
| client/ # StorefrontDataProvider, browser QueryClient | ||
| server/ # getServerQueryClient, hydration helpers | ||
| shared/ # cache-config, query-keys, medusa-client | ||
| products/ # createProductHooks factory | ||
| collections/ # createCollectionHooks factory | ||
| categories/ # createCategoryHooks factory | ||
| regions/ # createRegionHooks factory | ||
| auth/ # createAuthHooks factory + Medusa auth adapter | ||
| cart/ # createCartHooks factory + cart lifecycle | ||
| checkout/ # createCheckoutHooks factory + checkout flow | ||
| orders/ # createOrderHooks factory | ||
| customers/ # createCustomerHooks factory | ||
| ``` | ||
|
|
||
| ## Commands | ||
|
|
||
| - `pnpm -C libs/storefront-data build` # Build (tsc) | ||
| - `pnpm -C libs/storefront-data dev` # Watch mode | ||
| - `pnpm -C libs/storefront-data lint` # Biome lint | ||
|
|
||
| ## Critical Rules (Do not break these) | ||
|
|
||
| **NEVER:** | ||
| - Import from `./dist/` paths - use source | ||
| - Use `any` type - use proper generics | ||
| - Hardcode query keys - use `createQueryKey()` utility | ||
| - Mix server/client code in same file | ||
| - Create barrel files (`index.ts` re-exports) | ||
|
|
||
| **ALWAYS:** | ||
| - Use factory pattern (`createProductHooks`, etc.) | ||
| - Type service interfaces with generics | ||
| - Use cache strategies from `CacheConfig` (static, semiStatic, realtime, userData) | ||
| - Keep `"use client"` directive only in client components | ||
| - Use `getServerQueryClient` from `server/get-query-client` for Server Components | ||
|
|
||
| ## Hook Factory Pattern | ||
|
|
||
| ```typescript | ||
| import { createProductHooks } from "@techsio/storefront-data/products/hooks" | ||
| import type { ProductService } from "@techsio/storefront-data/products/types" | ||
|
|
||
| const productService: ProductService<Product, ListParams, DetailParams> = { | ||
| getProducts: (params, signal) => api.getProducts(params, signal), | ||
| getProductByHandle: (params) => api.getProduct(params), | ||
| } | ||
|
|
||
| export const { | ||
| useProducts, | ||
| useSuspenseProducts, | ||
| useProduct, | ||
| useSuspenseProduct, | ||
| usePrefetchProducts, | ||
| usePrefetchProduct, | ||
| usePrefetchPages, | ||
| } = createProductHooks({ | ||
| service: productService, | ||
| queryKeyNamespace: "my-app", | ||
| buildListParams: (input) => ({ ...input, offset: (input.page - 1) * input.limit }), | ||
| }) | ||
| ``` | ||
|
|
||
| ## Cache Strategies | ||
|
|
||
| | Strategy | Stale Time | Use Case | | ||
| |----------|------------|----------| | ||
| | `static` | 24h | Regions, rarely-changing data | | ||
| | `semiStatic` | 1h | Products, collections (default) | | ||
| | `realtime` | 30s | Cart, inventory | | ||
| | `userData` | 5 min | User profile, orders | | ||
|
|
||
| ## SSR Hydration | ||
|
|
||
| ```typescript | ||
| // Server Component | ||
| import { dehydrate, HydrationBoundary } from "@tanstack/react-query" | ||
| import { getServerQueryClient } from "@techsio/storefront-data/server/get-query-client" | ||
|
|
||
| export default async function Page() { | ||
| const queryClient = getServerQueryClient() | ||
| await queryClient.prefetchQuery({ queryKey, queryFn }) | ||
|
|
||
| return ( | ||
| <HydrationBoundary state={dehydrate(queryClient)}> | ||
| <ClientComponent /> | ||
| </HydrationBoundary> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ## App Integration | ||
|
|
||
| ```json | ||
| // package.json | ||
| { "dependencies": { "@techsio/storefront-data": "workspace:*" } } | ||
| ``` | ||
|
|
||
| ```javascript | ||
| // next.config.js | ||
| { transpilePackages: ['@techsio/storefront-data'] } | ||
| ``` | ||
|
|
||
| ## Research | ||
|
|
||
| - TanStack Query: check official docs before implementing SSR/prefetch patterns. | ||
| - Medusa SDK: reference for API response types and SDK methods. | ||
| - Before changing or modifying the code, please read this: clone repos to `~/.local/share/tanstack-query` and `~/.local/share/medusa` for local search. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| AGENTS.md | ||
|
KaiUweCZE marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| # Storefront Data Library (`@techsio/storefront-data`) | ||
|
|
||
| Shared data fetching library for Medusa.js e-commerce storefronts using TanStack Query. | ||
|
|
||
| ## Overview | ||
|
|
||
| This library provides a unified data fetching layer with: | ||
| - **Factory pattern hooks** for products, collections, categories, regions, auth, cart, checkout, orders, and customers | ||
| - **Smart caching** with configurable cache strategies | ||
| - **Prefetching utilities** for optimized navigation | ||
| - **SSR support** with hydration helpers | ||
| - **Type-safe** generics for custom product/entity types | ||
|
|
||
| Behavior notes: | ||
| - Prefetch helpers default to skipping only **fresh** cache entries (`skipMode: "fresh"`), not merely existing entries. | ||
| - TanStack Query cancellation does not apply to Suspense hooks (`useSuspenseQuery` / `useSuspenseQueries`). | ||
| - Query-key factories normalize plain-object params and keep primitive detail params (for example, `id: string`) as-is. | ||
|
|
||
| ## Installation | ||
|
|
||
| ### In Nx Monorepo Applications | ||
|
|
||
| Add to your app's `package.json`: | ||
|
|
||
| ```json | ||
| { | ||
| "dependencies": { | ||
| "@techsio/storefront-data": "workspace:*" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Then run: | ||
|
|
||
| ```bash | ||
| pnpm install | ||
| ``` | ||
|
|
||
| ### Next.js Configuration | ||
|
|
||
| Add to `next.config.js` transpilePackages: | ||
|
|
||
| ```javascript | ||
| const nextConfig = { | ||
| transpilePackages: ['@techsio/storefront-data'], | ||
| } | ||
| ``` | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ### 1. Setup Provider (Client-Side) | ||
|
|
||
| ```tsx | ||
| // app/layout.tsx or providers.tsx | ||
| import { StorefrontDataProvider } from "@techsio/storefront-data/client/provider" | ||
|
|
||
| export function Providers({ children }) { | ||
| return ( | ||
| <StorefrontDataProvider | ||
| clientConfig={{ | ||
| defaultOptions: { | ||
| queries: { retry: 2 }, | ||
| }, | ||
| }} | ||
| > | ||
| {children} | ||
| </StorefrontDataProvider> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| `clientConfig` is only applied when the internal singleton QueryClient is first created; later renders do not reconfigure it. | ||
|
KaiUweCZE marked this conversation as resolved.
|
||
|
|
||
| ### 2. Create Domain Hooks | ||
|
|
||
| ```tsx | ||
| // hooks/storefront-products.ts | ||
| import { createProductHooks } from "@techsio/storefront-data/products/hooks" | ||
| import type { ProductService } from "@techsio/storefront-data/products/types" | ||
| import type { Product } from "@/types/product" | ||
| import { getProducts, getProduct } from "@/services/product-service" | ||
|
|
||
| type ProductListParams = { | ||
| page?: number | ||
| limit?: number | ||
| region_id?: string | ||
| } | ||
|
|
||
| type ProductDetailParams = { | ||
| handle: string | ||
| region_id?: string | ||
| } | ||
|
|
||
| const productService: ProductService< | ||
| Product, | ||
| ProductListParams, | ||
| ProductDetailParams | ||
| > = { | ||
| getProducts: (params) => getProducts(params), | ||
| getProductByHandle: (params) => getProduct(params.handle, params.region_id), | ||
| } | ||
|
|
||
| export const { | ||
| useProducts, | ||
| useProduct, | ||
| useSuspenseProducts, | ||
| useSuspenseProduct, | ||
| usePrefetchProducts, | ||
| usePrefetchProduct, | ||
| usePrefetchPages, | ||
| } = createProductHooks({ | ||
| service: productService, | ||
| queryKeyNamespace: "my-app", | ||
| }) | ||
| ``` | ||
|
|
||
| ### 3. Use in Components | ||
|
|
||
| ```tsx | ||
| // Client component | ||
| "use client" | ||
| import { useProducts } from "@/hooks/storefront-products" | ||
|
|
||
| function ProductList() { | ||
| const { products, isLoading, totalPages, currentPage } = useProducts({ | ||
| page: 1, | ||
| limit: 20, | ||
| region_id: "reg_123", | ||
| }) | ||
|
|
||
| if (isLoading) return <Skeleton /> | ||
| return <Grid items={products} /> | ||
| } | ||
| ``` | ||
|
|
||
| ### 4. Server-Side Prefetching (SSR) | ||
|
|
||
| ```tsx | ||
| // app/products/page.tsx | ||
| import { dehydrate, HydrationBoundary } from "@tanstack/react-query" | ||
| import { getServerQueryClient } from "@techsio/storefront-data/server/get-query-client" | ||
| import { createProductQueryKeys } from "@techsio/storefront-data/products/query-keys" | ||
|
|
||
| export default async function ProductsPage() { | ||
| const queryClient = getServerQueryClient() | ||
| const productQueryKeys = createProductQueryKeys("my-app") | ||
| const listParams = { limit: 20, offset: 0, region_id: "reg_123" } | ||
|
|
||
| await queryClient.prefetchQuery({ | ||
| queryKey: productQueryKeys.list(listParams), | ||
| queryFn: () => fetchProducts(listParams), | ||
| }) | ||
|
|
||
| return ( | ||
| <HydrationBoundary state={dehydrate(queryClient)}> | ||
| <ProductList /> | ||
| </HydrationBoundary> | ||
| ) | ||
| } | ||
| ``` | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| ## Package Structure | ||
|
|
||
| ```text | ||
| src/ | ||
| auth/ # auth hooks + medusa service | ||
| cart/ # cart hooks + medusa service | ||
| catalog/ # catalog hooks + medusa service | ||
| categories/ # category hooks factory | ||
| checkout/ # checkout hooks + medusa service | ||
| client/ # client-side utilities | ||
| provider.tsx # StorefrontDataProvider | ||
| collections/ # collection hooks factory | ||
| customers/ # customer hooks + medusa service | ||
| orders/ # order hooks + medusa service | ||
| products/ # product hooks factory | ||
| regions/ # region hooks factory | ||
| server/ # server-side utilities | ||
| get-query-client.ts # per-request QueryClient | ||
| shared/ # shared utilities | ||
| cache-config.ts # cache strategy configs | ||
| medusa-client.ts # Medusa SDK factory | ||
| query-client.ts # QueryClient factory | ||
| query-keys.ts # query key utilities | ||
| ``` | ||
|
|
||
| ## Exports | ||
|
|
||
| Use explicit file-level subpaths (no barrel entrypoints), for example: | ||
| - `@techsio/storefront-data/auth/hooks` | ||
| - `@techsio/storefront-data/cart/hooks` | ||
| - `@techsio/storefront-data/catalog/hooks` | ||
| - `@techsio/storefront-data/checkout/hooks` | ||
| - `@techsio/storefront-data/customers/hooks` | ||
| - `@techsio/storefront-data/orders/hooks` | ||
| - `@techsio/storefront-data/products/hooks` | ||
| - `@techsio/storefront-data/products/types` | ||
| - `@techsio/storefront-data/client/provider` | ||
| - `@techsio/storefront-data/server/get-query-client` | ||
| - `@techsio/storefront-data/shared/cache-config` | ||
| - `@techsio/storefront-data/shared/query-keys` | ||
|
KaiUweCZE marked this conversation as resolved.
|
||
|
|
||
| ## Cache Strategies | ||
|
|
||
| ```typescript | ||
| import { createCacheConfig } from "@techsio/storefront-data/shared/cache-config" | ||
|
|
||
| const cacheConfig = createCacheConfig({ | ||
| // Override defaults as needed | ||
| semiStatic: { staleTime: 30 * 60 * 1000 }, | ||
| }) | ||
| ``` | ||
|
|
||
| | Strategy | Stale Time | Use Case | | ||
| |----------|------------|----------| | ||
| | `static` | 24 hours | Regions, rarely changing data | | ||
| | `semiStatic` | 1 hour | Products, collections | | ||
| | `realtime` | 30 seconds | Cart, inventory | | ||
| | `userData` | 5 minutes | User profile, orders | | ||
|
|
||
| ## Notes (Short) | ||
|
|
||
| - `enabled` is stripped from list/detail inputs before building query params/keys. | ||
| - Prefer plain objects for list/detail params in custom builders. Primitive detail params are supported and preserved in keys. | ||
| - Cart payloads are normalized by default (Medusa-friendly field names). | ||
| - Prefetch default is `skipMode: "fresh"` with `skipIfCached: true`; use `skipMode: "any"` to skip whenever any cache entry exists. | ||
| - Prefetch respects `skipIfCached`; pass `false` to force prefetch regardless of cache. | ||
| - Some service methods accept `signal` for aborting in-flight requests. | ||
| - SSR: use `getServerQueryClient` + `dehydrate` on server, `StorefrontDataProvider` + `HydrationBoundary` on client. | ||
|
|
||
| ## Peer Dependencies | ||
|
|
||
| ```json | ||
| { | ||
| "@medusajs/js-sdk": ">=2.12.0", | ||
| "@tanstack/react-query": ">=5.0.0", | ||
| "react": ">=19.2.0", | ||
| "react-dom": ">=19.2.0" | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ``` | ||
|
|
||
| ## Development | ||
|
|
||
| ```bash | ||
| # Build library | ||
| pnpm -C libs/storefront-data build | ||
|
|
||
| # Watch mode | ||
| pnpm -C libs/storefront-data dev | ||
|
|
||
| # Lint | ||
| pnpm -C libs/storefront-data lint | ||
|
|
||
| # Tests | ||
| pnpm -C libs/storefront-data test | ||
| ``` | ||
|
|
||
| ## Related Documentation | ||
|
|
||
| - [TanStack Query Docs](https://tanstack.com/query/latest) | ||
| - [Medusa.js SDK Docs](https://docs.medusajs.com/js-sdk) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.