Communitygithub.com

YepAPI/skills

使用 Zustand 搭配持久化機制管理購物車狀態,支援產品多規格、Stripe 結帳、庫存管理與訂單生命週期。

skills 是什麼?

這是一個專為電子商務應用設計的 AI Agent 技能,提供完整的購物車、產品、結帳與訂單管理解決方案。它使用 Zustand 狀態管理搭配 localStorage 持久化,確保購物車在頁面重整或分頁關閉後仍能保留。產品規格被建模為獨立 SKU,每個 SKU 擁有自己的價格、庫存與圖片。結帳流程依賴 Stripe Checkout Sessions 在伺服器端處理,避免客戶端傳送價格資訊,並在結帳前以資料庫交易檢查庫存以防止競態條件。訂單生命週期為 pending → paid → shipped → delivered,狀態更新僅透過 Stripe Webhook 事件驅動,而非客戶端操作。此外,支援伺服器端產品列表過濾、排序與分頁,價格以整數(分)儲存,顯示時再轉換為當地貨幣格式。支援 Cursor 與 Claude Code 等開發工具。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/e-commerce

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

電子商務

規則

  • 購物車狀態:使用 Zustand store 搭配 localStorage 持久化 — 在頁面重整與分頁關閉後仍能保留
  • 產品規格:將規格建模為獨立的 SKU,並共用同一個上層產品 — 每個規格擁有自己的價格、庫存、圖片
  • 結帳:Stripe Checkout Sessions(伺服器端)— 絕不從客戶端傳送價格
  • 庫存:在建立結帳工作階段前,在伺服器端檢查庫存 — 使用資料庫交易防止競態條件
  • 訂單生命週期:pending → paid → shipped → delivered — 透過 Webhook 事件更新,而非客戶端操作
  • 產品列表:伺服器端過濾/排序/分頁 — 使用 URL 搜尋參數建立可分享的篩選檢視
  • 價格:以分(整數)儲存 — 使用 Intl.NumberFormat 與貨幣代碼顯示
  • 購物車操作:新增、移除、更新數量、清空 — 在每次購物車操作時驗證庫存

模式

// 具有持久化的 Zustand 購物車 store
import { create } from "zustand";
import { persist } from "zustand/middleware";

interface CartItem { productId: string; variantId: string; quantity: number; price: number; }
interface CartStore {
  items: CartItem[];
  addItem: (item: CartItem) => void;
  removeItem: (variantId: string) => void;
  updateQuantity: (variantId: string, quantity: number) => void;
  clear: () => void;
  total: () => number;
}

export const useCart = create<CartStore>()(
  persist(
    (set, get) => ({
      items: [],
      addItem: (item) => set((state) => {
        const existing = state.items.find((i) => i.variantId === item.variantId);
        if (existing) {
          return { items: state.items.map((i) =>
            i.variantId === item.variantId ? { ...i, quantity: i.quantity + item.quantity } : i
          )};
        }
        return { items: [...state.items, item] };
      }),
      removeItem: (variantId) => set((s) => ({ items: s.items.filter((i) => i.variantId !== variantId) })),
      updateQuantity: (variantId, quantity) => set((s) => ({
        items: quantity <= 0
          ? s.items.filter((i) => i.variantId !== variantId)
          : s.items.map((i) => (i.variantId === variantId ? { ...i, quantity } : i)),
      })),
      clear: () => set({ items: [] }),
      total: () => get().items.reduce((sum, i) => sum + i.price * i.quantity, 0),
    }),
    { name: "cart-storage" }
  )
);

避免

  • 從客戶端傳送價格以建立結帳 — 應始終從你的資料庫在伺服器端解析價格
  • 僅將購物車儲存在 React 狀態中 — 使用持久化的 Zustand 或 localStorage,讓購物車在重整後仍可保留
  • 在「加入購物車」時扣除庫存 — 僅在結帳時保留庫存,並在工作階段過期時釋放
  • 從客戶端更新訂單狀態 — 使用 Stripe Webhook 處理 checkout.session.completed
  • 使用浮點數處理金錢 — 以整數(分)儲存,僅在顯示時進行格式化

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相關技能