Prompt Patterns

Reference intermediate

Prompt Patterns for Cursor

Inline Edit Patterns (Cmd+K / Ctrl+K)

Transform Pattern

Select code, then:

Convert this to [TARGET PATTERN]

Examples:

  • "Convert this to async/await"
  • "Convert this to a custom hook"
  • "Convert this class component to a function component"
  • "Convert this to use Zod validation"

Enhance Pattern

Select code, then:

Add [FEATURE] to this code

Examples:

  • "Add error handling with specific error types"
  • "Add TypeScript types to all parameters"
  • "Add loading and error states"
  • "Add JSDoc comments"

Fix Pattern

Select code, then:

Fix: [SPECIFIC ISSUE]

Examples:

  • "Fix: this causes a memory leak because the interval is never cleared"
  • "Fix: race condition when multiple requests arrive simultaneously"
  • "Fix: off-by-one error in pagination logic"

Chat Patterns (Cmd+L / Ctrl+L)

The Contextual Generation Pattern

@file:src/types/user.ts
@file:src/lib/db.ts

Create a new API route at src/app/api/users/route.ts that:
- Handles GET (list with pagination) and POST (create)
- Uses the User type from the referenced file
- Uses the db helper from the referenced file
- Validates POST body with Zod
- Returns proper HTTP status codes

The Codebase-Aware Pattern

Agent mode

How is authentication handled in this project? 
Show me the auth flow from login to protected API route.

Then follow up:

Create a new protected route at /api/settings that follows 
the same auth pattern you just described.

The Debug Pattern

@file:src/components/DataTable.tsx

This component re-renders excessively when the parent state changes.
The table has 1000+ rows and becomes unresponsive.

Identify the performance issue and fix it while maintaining 
the same functionality.

The Documentation-Guided Pattern

@docs:next.js

Create a middleware that:
- Checks for an auth token in cookies
- Redirects to /login if missing
- Allows access to public routes without auth
- Uses Next.js 14 middleware patterns from the docs

Agent/Agent Patterns (Cmd+I / Ctrl+I)

The Feature Implementation Pattern

Implement a complete notification system:

1. Database: Add a notifications table with fields:
   id, userId, type, title, message, read, createdAt

2. API: Create CRUD routes at /api/notifications
   - GET: list for current user (paginated, unread first)
   - PATCH /:id: mark as read
   - DELETE /:id: soft delete

3. Component: Create NotificationBell component
   - Shows unread count badge
   - Dropdown with notification list
   - Mark as read on click
   - Poll every 30 seconds

4. Hook: Create useNotifications hook
   - Manages fetch, optimistic updates, polling

Follow existing patterns in @folder:src/components and @folder:src/app/api

The Refactor-at-Scale Pattern

@folder:src/lib/api

Refactor all API client functions in this folder to:
1. Use a shared fetchWithAuth base function
2. Add proper TypeScript return types (no `any`)
3. Add request/response Zod schemas
4. Handle errors consistently with a custom ApiError class
5. Add retry logic for 5xx errors (max 3 attempts, exponential backoff)

Keep the same function signatures so callers don't break.

The Test Suite Pattern

@file:src/lib/cart.ts
@file:src/lib/cart.test.ts (existing tests for reference)

Add comprehensive tests for the cart module covering:
- Adding items (new item, existing item quantity increment)
- Removing items (last item removes entry, multiple items decrements)
- Calculating totals (subtotal, tax, shipping threshold, discounts)
- Edge cases: empty cart, negative quantities, max quantity limits
- Concurrent modification scenarios

Match the testing style in the existing test file.
Use vitest and @testing-library where appropriate.

Advanced Patterns

The Multi-Step Conversation Pattern

Step 1: "Analyze @folder:src/app/api and list all endpoints 
         with their methods and auth requirements"

Step 2: "Generate an OpenAPI 3.0 spec for all these endpoints"

Step 3: "Create a typed API client from this spec using the 
         pattern in @file:src/lib/apiClient.ts"

The Migration Pattern

@file:package.json
@folder:src/pages (current Pages Router)

Migrate the pages in src/pages/ to Next.js App Router:
- Convert getServerSideProps to server components
- Convert getStaticProps to generateStaticParams
- Move API routes to route handlers
- Update layouts to use the new layout.tsx pattern
- Keep the same URL structure

Do one page at a time, starting with the simplest.