Skip to content
Merged
Show file tree
Hide file tree
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 Jan 19, 2026
993b5c1
docs(storefront-data): add README and AGENTS.md
KaiUweCZE Jan 19, 2026
560d81d
fix(storefront-data): align dist types with package exports
KaiUweCZE Jan 19, 2026
4094a3d
test(storefront-data): add smoke tests with vitest infrastructure
KaiUweCZE Jan 20, 2026
3279bf5
test(storefront-data): add MSW infrastructure and nx test target
KaiUweCZE Jan 20, 2026
93fdedb
feat(storefront-data): add RegionInfo shared type
KaiUweCZE Jan 20, 2026
fe14eab
feat(storefront-data): add auth module
KaiUweCZE Jan 20, 2026
d5c2e4a
feat(storefront-data): add cart module
KaiUweCZE Jan 20, 2026
83cc0b7
feat(storefront-data): add checkout module
KaiUweCZE Jan 20, 2026
8ba0042
feat(storefront-data): add useCreateCustomer hook and fix Rules of Hooks
KaiUweCZE Jan 20, 2026
abe1a57
fix(storefront-data): avoid hooks-in-callback by resolving region in …
KaiUweCZE Jan 20, 2026
64945ba
feat(storefront-data): add update cart address helper
KaiUweCZE Jan 20, 2026
79a454b
feat(storefront-data): add infinite products hook
KaiUweCZE Jan 20, 2026
0909c97
feat(storefront-data): add orders and customer hooks
KaiUweCZE Jan 20, 2026
862c00e
test(storefront-data): add MSW smoke test for new hooks
KaiUweCZE Jan 20, 2026
80c12c6
merge master into feat/storefront-data
KaiUweCZE Jan 28, 2026
1e5cb05
update AGENTS.md and README.md
KaiUweCZE Jan 28, 2026
897dfda
build(storefront-data): use rslib build
KaiUweCZE Jan 28, 2026
a57d5f1
fix(auth): remove redundant invalidate on logout
KaiUweCZE Jan 28, 2026
a83f26f
refactor(cart): strip address-only fields before update
KaiUweCZE Jan 28, 2026
8fc3be7
feat(cart): optionally clear cart on update
KaiUweCZE Jan 28, 2026
e5bfa67
refactor(cart): memoize prefetchCart
KaiUweCZE Jan 28, 2026
fff3c4f
chore(cart/types): drop add line item metadata field
KaiUweCZE Jan 28, 2026
b7909ac
fix(categories): omit enabled from query params
KaiUweCZE Jan 28, 2026
655ac15
refactor(categories): tighten query key types
KaiUweCZE Jan 28, 2026
44481b0
feat(checkout):expose payment providers loading state
KaiUweCZE Jan 28, 2026
db41aa8
fix(collections): clear prefetch timeouts on unmount
KaiUweCZE Jan 28, 2026
1e794ed
docs(customer): add createCustomerHooks example
KaiUweCZE Jan 28, 2026
3a887ba
refactor(orders): type-safe suspense inputs
KaiUweCZE Jan 28, 2026
3710827
fix(products): strip enabled + clear prefetch timeouts
KaiUweCZE Jan 28, 2026
9c5e022
perf(products): memoize base input in prefetch
KaiUweCZE Jan 28, 2026
cea9949
fix(pagination): derive page from explicit offset
KaiUweCZE Jan 28, 2026
f8f0fbc
refactor(products): require namespace in query keys
KaiUweCZE Jan 28, 2026
ab05fa4
fix(products): pass abort signal to global fetcher
KaiUweCZE Jan 28, 2026
0f52d73
fix(regions): omit enabled from query params
KaiUweCZE Jan 28, 2026
e7d69cc
fix(regions): omit enabled and add abort signal
KaiUweCZE Jan 28, 2026
476d0a0
docs(shared): clarify query client config merge
KaiUweCZE Jan 28, 2026
d823f4b
chore(query-client): warn on ignored overrides
KaiUweCZE Jan 28, 2026
8664d2c
fix(query-keys): detect circular inputs
KaiUweCZE Jan 28, 2026
9fde8e1
test(network): type json response
KaiUweCZE Jan 28, 2026
a5edd71
fix: omit enabled from query paramas in collections and products
KaiUweCZE Jan 29, 2026
9a697ad
refactor(cart): normalize paylod inputs
KaiUweCZE Jan 29, 2026
6eb811c
feat(products): allow prefetch skipIfCached override
KaiUweCZE Jan 29, 2026
87491be
fix(query-client): redact errors on dehydrate
KaiUweCZE Jan 29, 2026
ae54a4f
fix(cart): sync active cart cache key
KaiUweCZE Jan 29, 2026
b228935
test(storefront-data): add missing hook coverage
KaiUweCZE Jan 29, 2026
4ec9168
docs(storefront-data): add notes and test command
KaiUweCZE Jan 29, 2026
2e3f11e
refactor(storefront-data): improve types and cleanup patterns
KaiUweCZE Jan 29, 2026
38e8f0d
refactor(storefront-data): improve types and avoid object mutation
KaiUweCZE Jan 29, 2026
596641e
feat(storefrontd-data): pass AbortSignal to detail fetchers
KaiUweCZE Jan 29, 2026
e1516f9
refactor(storefront-data): simplify checkout shipping logic
KaiUweCZE Jan 29, 2026
2a5506a
test(storefront-data): add QueryClient cleanup in smoke tests
KaiUweCZE Jan 29, 2026
740fc87
fix(storefront-data): enable refetchOnReconnect for realtime cache
KaiUweCZE Jan 29, 2026
8d98798
feat(storefront-data): export createOrderQueryKeys from orders
KaiUweCZE Jan 29, 2026
764b7c9
refactor(storefront-data): formatting and stricter type casts
KaiUweCZE Jan 29, 2026
230b035
feat(cart): add mutation lifecycle hooks and cache invalidation option
KaiUweCZE Jan 31, 2026
3d76987
feat(region): add RegionProvider and integrate context into product h…
KaiUweCZE Feb 1, 2026
8cf9927
feat(checkout): add full mutation lifecycle support
KaiUweCZE Feb 1, 2026
104baf0
feat(auth): add Medusa auth service and export types for SDK integration
KaiUweCZE Feb 2, 2026
0f0aa2b
feat(storefront-data): add Medusa region service and exports
KaiUweCZE Feb 2, 2026
2b2917f
chore(deps): add @medusajs/types peerDependency to storefront-data pa…
KaiUweCZE Feb 2, 2026
78a84ca
feat(orders): add Medusa order service and exports
KaiUweCZE Feb 2, 2026
8873d42
feat(cart): add Medusa cart service and re-export types
KaiUweCZE Feb 2, 2026
461f299
feat(customers): add Medusa customer and types
KaiUweCZE Feb 3, 2026
830ea62
refactor: expose query objects and add queryOptions support
KaiUweCZE Feb 3, 2026
221c19f
Merge branch 'master' into feat/storefront-data
KaiUweCZE Feb 4, 2026
8594d60
chore(deps): bump React type packages to patch versions to fix type e…
KaiUweCZE Feb 5, 2026
19cc1d4
feat(products): support initialLimit for infinite product lists
KaiUweCZE Feb 5, 2026
4ce34b9
feat(checkout): add Med checkout service and payment provider hooks
KaiUweCZE Feb 6, 2026
fff3888
feat(auth): add cross-domain query invalidation on auth changes and
KaiUweCZE Feb 8, 2026
a185fd2
feat(cart): normalize query key and improve prefetch behavior
KaiUweCZE Feb 9, 2026
2000109
feat(categories): add prefetch skip modes and signal-aware calls
KaiUweCZE Feb 9, 2026
0c49a4a
feat(checkout): strengthen types and normalize query keys
KaiUweCZE Feb 9, 2026
2174b28
feat(collections): add Medusa collection service with abortable calls
KaiUweCZE Feb 9, 2026
04fb305
feat(customers): improve auth handling and query key normalization
KaiUweCZE Feb 9, 2026
7680ab2
feat(products): normalize query keys and improve prefetch logic
KaiUweCZE Feb 9, 2026
b566d08
feat(storefront-data): normalize order query key params
KaiUweCZE Feb 9, 2026
b5ba9a7
feat(regions): normalize query keys and improve prefetch logic
KaiUweCZE Feb 9, 2026
8cb61ad
fix(query-client): remove unused getQueryClient export
KaiUweCZE Feb 9, 2026
c58ad95
feat(prefetch,query-keys): add prefetch helpers and normalize query p…
KaiUweCZE Feb 9, 2026
6d938b8
test: add Medusa service unit tests for checkout and categories
KaiUweCZE Feb 9, 2026
6981064
docs(storefront-data): expand hooks, docs, and query-key helpers
KaiUweCZE Feb 9, 2026
508846e
fix(cart-service): respect isNotFoundError config and recover stale c…
KaiUweCZE Feb 13, 2026
8d65f4e
Merge pull request #322 from NMIT-WR/lchylik/feat/slect-open-state-tr…
BleedingDev Feb 6, 2026
7575c57
Merge pull request #324 from NMIT-WR/chore/react19-monorepo-consistency
BleedingDev Feb 6, 2026
76d2428
feat(storefront-data): add shared data fetching library
KaiUweCZE Jan 19, 2026
c5bc0e2
docs(storefront-data): add README and AGENTS.md
KaiUweCZE Jan 19, 2026
1ea7eee
fix(storefront-data): align dist types with package exports
KaiUweCZE Jan 19, 2026
6c5b09b
test(storefront-data): add smoke tests with vitest infrastructure
KaiUweCZE Jan 20, 2026
ef2234d
test(storefront-data): add MSW infrastructure and nx test target
KaiUweCZE Jan 20, 2026
8741e4b
feat(storefront-data): add RegionInfo shared type
KaiUweCZE Jan 20, 2026
b0a545c
feat(storefront-data): add auth module
KaiUweCZE Jan 20, 2026
c07cc1b
feat(storefront-data): add cart module
KaiUweCZE Jan 20, 2026
e6e5cbf
feat(storefront-data): add checkout module
KaiUweCZE Jan 20, 2026
44847a9
feat(storefront-data): add useCreateCustomer hook and fix Rules of Hooks
KaiUweCZE Jan 20, 2026
a14eaab
fix(storefront-data): avoid hooks-in-callback by resolving region in …
KaiUweCZE Jan 20, 2026
a265fc4
feat(storefront-data): add update cart address helper
KaiUweCZE Jan 20, 2026
a4f29b0
feat(storefront-data): add infinite products hook
KaiUweCZE Jan 20, 2026
b5d8900
feat(storefront-data): add orders and customer hooks
KaiUweCZE Jan 20, 2026
1096aa3
test(storefront-data): add MSW smoke test for new hooks
KaiUweCZE Jan 20, 2026
d6e0fa9
merge master into feat/storefront-data
KaiUweCZE Jan 28, 2026
4e8ccd7
update AGENTS.md and README.md
KaiUweCZE Jan 28, 2026
c5985b6
build(storefront-data): use rslib build
KaiUweCZE Jan 28, 2026
8c03db8
fix(auth): remove redundant invalidate on logout
KaiUweCZE Jan 28, 2026
42354d2
refactor(cart): strip address-only fields before update
KaiUweCZE Jan 28, 2026
dd89de0
feat(cart): optionally clear cart on update
KaiUweCZE Jan 28, 2026
cb2a486
refactor(cart): memoize prefetchCart
KaiUweCZE Jan 28, 2026
40800e4
chore(cart/types): drop add line item metadata field
KaiUweCZE Jan 28, 2026
c0b03fc
fix(categories): omit enabled from query params
KaiUweCZE Jan 28, 2026
1c2cc57
refactor(categories): tighten query key types
KaiUweCZE Jan 28, 2026
72449cb
feat(checkout):expose payment providers loading state
KaiUweCZE Jan 28, 2026
b2bfe91
fix(collections): clear prefetch timeouts on unmount
KaiUweCZE Jan 28, 2026
363f249
docs(customer): add createCustomerHooks example
KaiUweCZE Jan 28, 2026
edd4bfe
refactor(orders): type-safe suspense inputs
KaiUweCZE Jan 28, 2026
b2b450b
fix(products): strip enabled + clear prefetch timeouts
KaiUweCZE Jan 28, 2026
85d607d
perf(products): memoize base input in prefetch
KaiUweCZE Jan 28, 2026
a19f985
fix(pagination): derive page from explicit offset
KaiUweCZE Jan 28, 2026
78b6772
refactor(products): require namespace in query keys
KaiUweCZE Jan 28, 2026
a3a3c15
fix(products): pass abort signal to global fetcher
KaiUweCZE Jan 28, 2026
2451886
fix(regions): omit enabled from query params
KaiUweCZE Jan 28, 2026
1c9a6b0
fix(regions): omit enabled and add abort signal
KaiUweCZE Jan 28, 2026
a5b8251
docs(shared): clarify query client config merge
KaiUweCZE Jan 28, 2026
9ed9755
chore(query-client): warn on ignored overrides
KaiUweCZE Jan 28, 2026
ab10c5b
fix(query-keys): detect circular inputs
KaiUweCZE Jan 28, 2026
4455c41
test(network): type json response
KaiUweCZE Jan 28, 2026
cb6c252
fix: omit enabled from query paramas in collections and products
KaiUweCZE Jan 29, 2026
4a7e341
refactor(cart): normalize paylod inputs
KaiUweCZE Jan 29, 2026
1a1bda8
feat(products): allow prefetch skipIfCached override
KaiUweCZE Jan 29, 2026
5ae3645
fix(query-client): redact errors on dehydrate
KaiUweCZE Jan 29, 2026
300ad0f
fix(cart): sync active cart cache key
KaiUweCZE Jan 29, 2026
04d3bb5
test(storefront-data): add missing hook coverage
KaiUweCZE Jan 29, 2026
12e9216
docs(storefront-data): add notes and test command
KaiUweCZE Jan 29, 2026
072b14e
refactor(storefront-data): improve types and cleanup patterns
KaiUweCZE Jan 29, 2026
98360a4
refactor(storefront-data): improve types and avoid object mutation
KaiUweCZE Jan 29, 2026
4df18db
feat(storefrontd-data): pass AbortSignal to detail fetchers
KaiUweCZE Jan 29, 2026
2c2d3a1
refactor(storefront-data): simplify checkout shipping logic
KaiUweCZE Jan 29, 2026
0c1dca1
test(storefront-data): add QueryClient cleanup in smoke tests
KaiUweCZE Jan 29, 2026
7a8ad6c
fix(storefront-data): enable refetchOnReconnect for realtime cache
KaiUweCZE Jan 29, 2026
d04cb7e
feat(storefront-data): export createOrderQueryKeys from orders
KaiUweCZE Jan 29, 2026
b777c30
refactor(storefront-data): formatting and stricter type casts
KaiUweCZE Jan 29, 2026
8ddf7d3
feat(cart): add mutation lifecycle hooks and cache invalidation option
KaiUweCZE Jan 31, 2026
9d8aecd
feat(region): add RegionProvider and integrate context into product h…
KaiUweCZE Feb 1, 2026
aef021d
feat(checkout): add full mutation lifecycle support
KaiUweCZE Feb 1, 2026
990dcd3
feat(auth): add Medusa auth service and export types for SDK integration
KaiUweCZE Feb 2, 2026
837f80f
feat(storefront-data): add Medusa region service and exports
KaiUweCZE Feb 2, 2026
6dbe22a
chore(deps): add @medusajs/types peerDependency to storefront-data pa…
KaiUweCZE Feb 2, 2026
b2c4d82
feat(orders): add Medusa order service and exports
KaiUweCZE Feb 2, 2026
438e1ef
feat(cart): add Medusa cart service and re-export types
KaiUweCZE Feb 2, 2026
ef11aea
feat(customers): add Medusa customer and types
KaiUweCZE Feb 3, 2026
08f7386
refactor: expose query objects and add queryOptions support
KaiUweCZE Feb 3, 2026
91fe1db
Merge branch 'master' into feat/storefront-data
KaiUweCZE Feb 4, 2026
8a502d0
chore(deps): bump React type packages to patch versions to fix type e…
KaiUweCZE Feb 5, 2026
d3a28f3
feat(products): support initialLimit for infinite product lists
KaiUweCZE Feb 5, 2026
4169625
feat(checkout): add Med checkout service and payment provider hooks
KaiUweCZE Feb 6, 2026
3111e41
feat(auth): add cross-domain query invalidation on auth changes and
KaiUweCZE Feb 8, 2026
78193c8
feat(cart): normalize query key and improve prefetch behavior
KaiUweCZE Feb 9, 2026
03333c2
feat(categories): add prefetch skip modes and signal-aware calls
KaiUweCZE Feb 9, 2026
de80457
feat(checkout): strengthen types and normalize query keys
KaiUweCZE Feb 9, 2026
05fad69
feat(collections): add Medusa collection service with abortable calls
KaiUweCZE Feb 9, 2026
1275651
feat(customers): improve auth handling and query key normalization
KaiUweCZE Feb 9, 2026
21dadd2
feat(products): normalize query keys and improve prefetch logic
KaiUweCZE Feb 9, 2026
ca3086e
feat(storefront-data): normalize order query key params
KaiUweCZE Feb 9, 2026
da471ca
feat(regions): normalize query keys and improve prefetch logic
KaiUweCZE Feb 9, 2026
44bb2ee
fix(query-client): remove unused getQueryClient export
KaiUweCZE Feb 9, 2026
4c0fc32
feat(prefetch,query-keys): add prefetch helpers and normalize query p…
KaiUweCZE Feb 9, 2026
102a87a
test: add Medusa service unit tests for checkout and categories
KaiUweCZE Feb 9, 2026
061e3f7
docs(storefront-data): expand hooks, docs, and query-key helpers
KaiUweCZE Feb 9, 2026
7265779
fix(cart-service): respect isNotFoundError config and recover stale c…
KaiUweCZE Feb 13, 2026
130f676
merge master into storefront-data
KaiUweCZE Feb 13, 2026
fbdd198
Merge branch 'feat/storefront-data' of https://github.com/NMIT-WR/new…
KaiUweCZE Feb 20, 2026
33a1759
fix(storefront-data): report auth logout errors and sanitize cart pay…
KaiUweCZE Feb 20, 2026
a1ce79b
refactor(storefront-data): remove barrel exports and normalize cart p…
KaiUweCZE Feb 21, 2026
35f32f3
docs(auth): add tanstack source link
KaiUweCZE Feb 21, 2026
cbe5324
merge master into storefront-data
KaiUweCZE Feb 21, 2026
5a33e01
feat(storefront-data): add catalog module with hooks, query keys, and…
KaiUweCZE Feb 21, 2026
73bca5b
remove BOM
KaiUweCZE Feb 21, 2026
e93b1c2
fix(storefront-data): stabilize ssr hydration, hooks safety and custo…
KaiUweCZE Feb 22, 2026
1ce2b37
fix(storefront-data): add abort-signal cancellation across medusa rea…
KaiUweCZE Feb 22, 2026
1c2c451
fix: contracts and abort-signal reads
KaiUweCZE Feb 22, 2026
562053f
fix(storefront-data): harden exports and normalize customer validatio…
KaiUweCZE Feb 22, 2026
bb02b83
fix(storefront-data): harden server query-client server-only boundary
KaiUweCZE Feb 22, 2026
51a3ec5
refactor(storefront-data): centralize medusa error handling helpers
KaiUweCZE Feb 22, 2026
c4b9ff8
refactor(storefront-data): unify query-key utilities and shared key t…
KaiUweCZE Feb 22, 2026
03d35c8
refactor(storefront-data): improve catalog/product query ergonomics a…
KaiUweCZE Feb 22, 2026
28a84e4
refactor(storefront-data): reduce hook and service duplication across…
KaiUweCZE Feb 22, 2026
4055285
docs(storefront-data): clarify provider singleton config behavior
KaiUweCZE Feb 22, 2026
5479a16
test(storefront-data): improve edge-case coverage and test setup hygiene
KaiUweCZE Feb 22, 2026
2f43e4a
refactor(products): cache ergonomics and docs clarify products
KaiUweCZE Feb 22, 2026
2130648
fix(storefront-data): harden hooks and services and add phase regress…
KaiUweCZE Feb 22, 2026
c44da93
fix(storefront-data): harden auth/customer flows and fix hook invariants
KaiUweCZE Feb 23, 2026
cb4ff05
refactor(storefront-data): consolidate hook error/prefetch utilities …
KaiUweCZE Feb 23, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions libs/storefront-data/AGENTS.md
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 |
Comment thread
KaiUweCZE marked this conversation as resolved.

## 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.
1 change: 1 addition & 0 deletions libs/storefront-data/CLAUDE.md
261 changes: 261 additions & 0 deletions libs/storefront-data/README.md
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.
Comment thread
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>
)
}
```
Comment thread
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`
Comment thread
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"
}
Comment thread
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)
Loading
Loading