All projects
E-commerce Store
hardBuild a full storefront with a product catalog, variant selection, a typed cart, checkout with Stripe, and order history. This is the capstone: you will combine generics for API responses, discriminated unions for payment and order states, exact money handling with integer cents, and end-to-end types shared between server and client.
Next.jsTypeScriptTailwind CSSPrismaPostgreSQLStripeZustand
Architecture
- UI layer: catalog grid, product page with variant picker, cart drawer, and a multi-step checkout flow
- State layer: a persisted Zustand cart store keyed by variant id, with derived totals computed in selectors
- Domain layer: money as integer cents with typed helpers, and order status as a discriminated union state machine
- Service layer: a typed API client where every endpoint declares its request and response types via a shared ApiResult generic
- API layer: route handlers for catalog, cart validation, checkout session creation, and the Stripe webhook
- Persistence layer: Prisma repositories for products, variants, and orders, returning domain types only
Folder structure
src/
app/
page.tsx
products/
[slug]/
page.tsx
cart/
page.tsx
checkout/
page.tsx
orders/
page.tsx
api/
checkout/
route.ts
webhooks/
stripe/
route.ts
components/
ProductGrid.tsx
VariantPicker.tsx
CartDrawer.tsx
CheckoutSteps.tsx
OrderHistory.tsx
store/
useCartStore.ts
lib/
money.ts
apiClient.ts
repository.ts
stripe.ts
types/
shop.tsDomain type definitions
Start every project by defining its types — they become the contract for every component, service, and API call that follows.
export interface Product {
id: string;
slug: string;
name: string;
description: string;
variants: Variant[];
}
export interface Variant {
id: string;
productId: string;
label: string; // e.g. "Blue / Large"
priceCents: number;
stock: number;
}
export interface CartLine {
variantId: string;
quantity: number;
}
export type OrderStatus =
| { state: "pending" }
| { state: "paid"; paidAt: string; stripePaymentId: string }
| { state: "shipped"; shippedAt: string; trackingCode: string }
| { state: "cancelled"; cancelledAt: string; reason: string };
export interface Order {
id: string;
lines: CartLine[];
totalCents: number;
status: OrderStatus;
createdAt: string;
}
export type ApiResult<T> =
| { ok: true; data: T }
| { ok: false; error: string; status: number };Build steps
- 1Define the type layer — Product, Variant, CartLine, the OrderStatus union, Order, and the ApiResult generic.
- 2Write money.ts with formatCents and addCents helpers so no component ever does floating-point math on prices.
- 3Set up Prisma models and repository.ts for products, variants, and orders, mapping rows into domain types.
- 4Build the catalog and product pages as server components, with VariantPicker narrowing the selected Variant on the client.
- 5Build the cart store keyed by variantId with add, remove, and setQuantity actions, persisted to localStorage.
- 6Write apiClient.ts: a fetch wrapper returning ApiResult so every caller must handle the error branch before touching data.
- 7Implement the checkout route: re-validate stock and prices server-side, then create a Stripe checkout session.
- 8Implement the Stripe webhook: verify the signature, then transition the order status from pending to paid.
- 9Build CheckoutSteps and the orders page, rendering each OrderStatus state with an exhaustive switch.
- 10Add a low-stock guard at add-to-cart time and an end-to-end type check that client and server agree on every payload.