Communitygithub.com

YepAPI/技能

使用 Zustand 和持久化管理购物车状态、产品变体、Stripe 结账、库存管理和订单生命周期。

技能 是什么?

这个 E-Commerce 技能提供了一套完整的电子商务后端与前端状态管理指南。它使用 Zustand 状态管理库与 localStorage 持久化,确保购物车在页面刷新或标签页关闭后仍能保持。产品变体被建模为独立的 SKU,共享父级产品,每个变体拥有独立的价格、库存和图片。结账过程通过 Stripe Checkout Sessions 在服务端处理,绝不会从客户端发送价格数据,库存检查也在服务端进行,并通过数据库事务防止竞态条件。订单生命周期由 `pending` → `paid` → `shipped` → `delivered` 组成,更新依赖 webhook 事件而非客户端操作。该技能支持主流 AI 编码助手(如 Claude Code、Coda 等),并提供了 TypeScript 示例代码和最佳实践指南。

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

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

电子商务

规则

  • 购物车状态:使用 Zustand 存储和 localStorage 持久化——支持页面刷新和标签页关闭后状态保持
  • 产品变体:模型化为独立的 SKU,共享父级产品——每个变体拥有自己的价格、库存和图片
  • 结账:Stripe Checkout Sessions(服务端处理)——绝不从客户端发送价格
  • 库存:创建结账会话前在服务端检查库存——通过数据库事务防止竞态条件
  • 订单生命周期:pending → paid → shipped → delivered——通过 webhook 事件更新,而非客户端操作
  • 产品列表:服务端过滤/排序/分页——URL 搜索参数实现可分享的筛选视图
  • 价格:以分为单位(整数)存储——使用 Intl.NumberFormat 和货币代码显示
  • 购物车操作:添加、移除、更新数量、清空——每次购物车操作时验证库存

模式

// Zustand 购物车存储带持久化
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 webhooks 处理 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.

相关技能