@balazsbarta/mp-skills 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/.agents/skills/brainstorming/SKILL.md +201 -0
  2. package/.agents/skills/brainstorming/references/option-evaluation.md +64 -0
  3. package/.agents/skills/brainstorming/references/questioning-playbook.md +57 -0
  4. package/.agents/skills/brainstorming/references/repo-analysis.md +60 -0
  5. package/.agents/skills/conventional-commits/SKILL.md +124 -0
  6. package/.agents/skills/conventional-commits/references/commit-types-scopes.md +75 -0
  7. package/.agents/skills/conventional-commits/references/semantic-release.md +71 -0
  8. package/.agents/skills/jest/SKILL.md +219 -0
  9. package/.agents/skills/jest/references/common-errors.md +274 -0
  10. package/.agents/skills/jest/references/configuration.md +175 -0
  11. package/.agents/skills/jest/references/embedded-docs.md +44 -0
  12. package/.agents/skills/jest/references/mocking.md +206 -0
  13. package/.agents/skills/jest/references/remote-docs.md +19 -0
  14. package/.agents/skills/jest/references/snapshot-testing.md +181 -0
  15. package/.agents/skills/jest/references/transforms.md +216 -0
  16. package/.agents/skills/maestro/SKILL.md +230 -0
  17. package/.agents/skills/maestro/references/assertions-commands.md +259 -0
  18. package/.agents/skills/maestro/references/common-errors.md +273 -0
  19. package/.agents/skills/maestro/references/eas-ci-integration.md +219 -0
  20. package/.agents/skills/maestro/references/flow-authoring.md +224 -0
  21. package/.agents/skills/maestro/references/remote-docs.md +23 -0
  22. package/.agents/skills/mastra/SKILL.md +159 -0
  23. package/.agents/skills/mastra/references/common-errors.md +535 -0
  24. package/.agents/skills/mastra/references/create-mastra.md +220 -0
  25. package/.agents/skills/mastra/references/embedded-docs.md +123 -0
  26. package/.agents/skills/mastra/references/migration-guide.md +180 -0
  27. package/.agents/skills/mastra/references/remote-docs.md +193 -0
  28. package/.agents/skills/next-js/SKILL.md +209 -0
  29. package/.agents/skills/next-js/references/api-routes.md +213 -0
  30. package/.agents/skills/next-js/references/app-router.md +206 -0
  31. package/.agents/skills/next-js/references/caching-revalidation.md +211 -0
  32. package/.agents/skills/next-js/references/common-errors.md +251 -0
  33. package/.agents/skills/next-js/references/embedded-docs.md +43 -0
  34. package/.agents/skills/next-js/references/metadata-seo.md +257 -0
  35. package/.agents/skills/next-js/references/remote-docs.md +22 -0
  36. package/.agents/skills/playwright/SKILL.md +218 -0
  37. package/.agents/skills/playwright/references/ci-configuration.md +208 -0
  38. package/.agents/skills/playwright/references/common-errors.md +258 -0
  39. package/.agents/skills/playwright/references/embedded-docs.md +41 -0
  40. package/.agents/skills/playwright/references/fixtures-assertions.md +208 -0
  41. package/.agents/skills/playwright/references/page-objects.md +167 -0
  42. package/.agents/skills/playwright/references/remote-docs.md +23 -0
  43. package/.agents/skills/playwright/references/visual-regression.md +206 -0
  44. package/.agents/skills/pull-request-lifecycle/SKILL.md +116 -0
  45. package/.agents/skills/pull-request-lifecycle/references/changelog-versioning.md +72 -0
  46. package/.agents/skills/pull-request-lifecycle/references/merge-strategies.md +33 -0
  47. package/.agents/skills/pull-request-lifecycle/references/pr-description-template.md +72 -0
  48. package/.agents/skills/pull-request-lifecycle/references/review-process.md +54 -0
  49. package/.agents/skills/pull-request-lifecycle/scripts/code_review.py +220 -0
  50. package/.agents/skills/react-native-expo/SKILL.md +212 -0
  51. package/.agents/skills/react-native-expo/references/common-errors.md +251 -0
  52. package/.agents/skills/react-native-expo/references/eas-build-submit.md +238 -0
  53. package/.agents/skills/react-native-expo/references/embedded-docs.md +42 -0
  54. package/.agents/skills/react-native-expo/references/native-modules.md +181 -0
  55. package/.agents/skills/react-native-expo/references/navigation-setup.md +229 -0
  56. package/.agents/skills/react-native-expo/references/remote-docs.md +23 -0
  57. package/.agents/skills/supabase/SKILL.md +216 -0
  58. package/.agents/skills/supabase/references/auth-setup.md +206 -0
  59. package/.agents/skills/supabase/references/common-errors.md +285 -0
  60. package/.agents/skills/supabase/references/edge-functions.md +178 -0
  61. package/.agents/skills/supabase/references/embedded-docs.md +43 -0
  62. package/.agents/skills/supabase/references/migrations.md +193 -0
  63. package/.agents/skills/supabase/references/remote-docs.md +24 -0
  64. package/.agents/skills/supabase/references/rls-policies.md +187 -0
  65. package/.agents/skills/supabase/references/storage.md +182 -0
  66. package/.agents/skills/task-breakdown/SKILL.md +179 -0
  67. package/.agents/skills/task-breakdown/references/acceptance-criteria.md +165 -0
  68. package/.agents/skills/task-breakdown/references/epic-story-format.md +209 -0
  69. package/.agents/skills/task-breakdown/references/estimation-guide.md +140 -0
  70. package/.agents/skills/vitest/SKILL.md +219 -0
  71. package/.agents/skills/vitest/references/common-errors.md +271 -0
  72. package/.agents/skills/vitest/references/component-testing.md +182 -0
  73. package/.agents/skills/vitest/references/configuration.md +184 -0
  74. package/.agents/skills/vitest/references/coverage.md +179 -0
  75. package/.agents/skills/vitest/references/embedded-docs.md +43 -0
  76. package/.agents/skills/vitest/references/mocking.md +182 -0
  77. package/.agents/skills/vitest/references/remote-docs.md +22 -0
  78. package/README.md +235 -0
  79. package/package.json +20 -0
  80. package/scripts/skills.mjs +849 -0
@@ -0,0 +1,193 @@
1
+ # Remote Docs Reference
2
+
3
+ How to look up current documentation from https://mastra.ai when local packages aren't available or you need conceptual guidance.
4
+
5
+ **Use this when:**
6
+
7
+ - Mastra packages aren't installed locally
8
+ - You need conceptual explanations or guides
9
+ - You want the latest documentation (may be ahead of installed version)
10
+
11
+ ## Documentation site structure
12
+
13
+ Mastra docs are organized at **https://mastra.ai**:
14
+
15
+ - **Docs**: Core documentation covering concepts, features, and implementation details
16
+ - **Models**: Mastra provides a unified interface for working with LLMs across multiple providers
17
+ - **Guides**: Step-by-step tutorials for building specific applications
18
+ - **Reference**: API reference documentation
19
+
20
+ ## Finding relevant documentation
21
+
22
+ ### Method 1: Use llms.txt (Recommended)
23
+
24
+ The main llms.txt file provides an agent-friendly overview of all documentation: https://mastra.ai/llms.txt
25
+
26
+ This returns a structured markdown document with:
27
+
28
+ - Documentation organization and hierarchy
29
+ - All available topics and sections
30
+ - Direct links to relevant documentation
31
+ - Agent-optimized content structure
32
+
33
+ **Use this first** to understand what documentation is available and where to find specific topics.
34
+
35
+ ### Method 2: Direct URL patterns
36
+
37
+ Documentation follows predictable URL patterns:
38
+
39
+ - Overview pages: `https://mastra.ai/docs/{topic}/overview`
40
+ - API reference: `https://mastra.ai/reference/{topic}/`
41
+ - Guides: `https://mastra.ai/guides/{topic}/`
42
+
43
+ **Examples:**
44
+
45
+ - `https://mastra.ai/docs/agents/overview`
46
+ - `https://mastra.ai/docs/workflows/overview`
47
+ - `https://mastra.ai/reference/workflows/workflow-methods/`
48
+
49
+ ## Agent-friendly documentation
50
+
51
+ **Critical feature**: Send the `text-markdown` request header or add `.md` to any documentation URL to get clean, agent-friendly markdown.
52
+
53
+ ### Standard URL:
54
+
55
+ ```
56
+ https://mastra.ai/reference/workflows/workflow-methods/then
57
+ ```
58
+
59
+ ### Agent-friendly URL (Markdown):
60
+
61
+ ```
62
+ https://mastra.ai/reference/workflows/workflow-methods/then.md
63
+ ```
64
+
65
+ The `.md` version:
66
+
67
+ - Removes navigation, headers, footers
68
+ - Returns pure markdown content
69
+ - Optimized for LLM consumption
70
+ - Includes all code examples and explanations
71
+
72
+ ## Lookup Workflow
73
+
74
+ ### 1. Check the main documentation index
75
+
76
+ **Start here** to understand what's available:
77
+
78
+ ```
79
+ https://mastra.ai/llms.txt
80
+ ```
81
+
82
+ This provides:
83
+
84
+ - Complete documentation structure
85
+ - Available topics and sections
86
+ - Links to relevant documentation pages
87
+
88
+ ### 2. Find relevant documentation
89
+
90
+ **Option A: Use information from llms.txt**
91
+ The main llms.txt will guide you to the right section.
92
+
93
+ **Option B: Construct URL directly**
94
+
95
+ ```
96
+ https://mastra.ai/docs/{topic}/overview
97
+ https://mastra.ai/reference/{topic}/
98
+ ```
99
+
100
+ ### 3. Fetch agent-friendly version
101
+
102
+ Add `.md` to the end of any documentation URL:
103
+
104
+ ```
105
+ https://mastra.ai/reference/workflows/workflow-methods/then.md
106
+ ```
107
+
108
+ ### 4. Extract relevant information
109
+
110
+ The markdown will include:
111
+
112
+ - Function signatures
113
+ - Parameter descriptions
114
+ - Return types
115
+ - Usage examples
116
+ - Best practices
117
+
118
+ ## Common documentation paths
119
+
120
+ ### Agents
121
+
122
+ - Overview: `https://mastra.ai/docs/agents/overview`
123
+ - Creating agents: `https://mastra.ai/docs/agents/creating-agents`
124
+ - Agent tools: `https://mastra.ai/docs/agents/tools`
125
+ - Memory: `https://mastra.ai/docs/agents/memory`
126
+
127
+ ### Workflows
128
+
129
+ - Overview: `https://mastra.ai/docs/workflows/overview`
130
+ - Creating workflows: `https://mastra.ai/docs/workflows/creating-workflows`
131
+ - Workflow methods: `https://mastra.ai/reference/workflows/workflow-methods/`
132
+
133
+ ### Tools
134
+
135
+ - Overview: `https://mastra.ai/docs/tools/overview`
136
+ - Creating tools: `https://mastra.ai/docs/tools/creating-tools`
137
+
138
+ ### Memory
139
+
140
+ - Overview: `https://mastra.ai/docs/memory/overview`
141
+ - Configuration: `https://mastra.ai/docs/memory/configuration`
142
+
143
+ ### RAG
144
+
145
+ - Overview: `https://mastra.ai/docs/rag/overview`
146
+ - Vector stores: `https://mastra.ai/docs/rag/vector-stores`
147
+
148
+ ## Example: Looking up workflow .then() method
149
+
150
+ ### 1. Check main documentation index
151
+
152
+ ```
153
+ WebFetch({
154
+ url: "https://mastra.ai/llms.txt",
155
+ prompt: "Where can I find documentation about workflow methods like .then()?"
156
+ })
157
+ ```
158
+
159
+ This will point you to the workflows reference section.
160
+
161
+ ### 2. Fetch specific method documentation
162
+
163
+ ```
164
+ https://mastra.ai/reference/workflows/workflow-methods/then.md
165
+ ```
166
+
167
+ ### 3. Use WebFetch tool
168
+
169
+ ```
170
+ WebFetch({
171
+ url: "https://mastra.ai/reference/workflows/workflow-methods/then.md",
172
+ prompt: "What are the parameters for the .then() method and how do I use it?"
173
+ })
174
+ ```
175
+
176
+ ## When to use remote vs embedded docs
177
+
178
+ | Situation | Use |
179
+ | -------------------------- | --------------------------------------------------- |
180
+ | Packages installed locally | **Embedded docs** (guaranteed version match) |
181
+ | Packages not installed | **Remote docs** |
182
+ | Need conceptual guides | **Remote docs** |
183
+ | Need exact API signatures | **Embedded docs** (if available) |
184
+ | Exploring new features | **Remote docs** (may be ahead of installed version) |
185
+ | Need working examples | **Both** (embedded for types, remote for guides) |
186
+
187
+ ## Best practices
188
+
189
+ 1. **Always use .md** for fetching documentation
190
+ 2. **Check sitemap.xml** when unsure about URL structure
191
+ 3. **Prefer embedded docs** when packages are installed (version accuracy)
192
+ 4. **Use remote docs** for conceptual understanding and guides
193
+ 5. **Combine both** for comprehensive understanding
@@ -0,0 +1,209 @@
1
+ ---
2
+ name: next-js
3
+ description: "Next.js App Router development guide. Covers App Router conventions, API routes, caching and revalidation, metadata/SEO, and common errors. Use this skill when building or debugging Next.js applications using the App Router."
4
+ license: MIT
5
+ metadata:
6
+ author: Balazs Barta
7
+ version: "2.0.0"
8
+ repository: https://github.com/balazsbarta/mp-skills
9
+ tags:
10
+ - next-js
11
+ - react
12
+ - app-router
13
+ - server-components
14
+ - full-stack
15
+ ---
16
+
17
+ # Next.js Guide
18
+
19
+ Build full-stack web applications with Next.js App Router.
20
+
21
+ ## ⚠️ Critical: Do not trust internal knowledge
22
+
23
+ **Everything you know about Next.js may be outdated or wrong. Never rely on memory. Always verify against current documentation.**
24
+
25
+ Next.js evolves rapidly between major versions. App Router APIs, caching behavior, and conventions change frequently. Always check the references in this skill.
26
+
27
+ ## Prerequisites
28
+
29
+ **Before writing Next.js code**, validate installed version and local project config:
30
+
31
+ ```bash
32
+ npx next --version
33
+ cat node_modules/next/package.json | grep '"version"'
34
+ ls next.config.* 2>/dev/null
35
+ npx next info
36
+ ```
37
+
38
+ - If installed, follow repository behavior and config first.
39
+ - If configuration is unclear, use this skill's lookup flow before coding.
40
+
41
+ ## Documentation lookup guide
42
+
43
+ ### Quick reference
44
+
45
+ | User Question | First Check | How To |
46
+ | --- | --- | --- |
47
+ | "What works in this repo version?" | [`references/embedded-docs.md`](references/embedded-docs.md) | Check local version, config, and route structure |
48
+ | "How do I create pages/layouts?" | [`references/app-router.md`](references/app-router.md) | File conventions, layouts, loading/error states |
49
+ | "How do I create an API endpoint?" | [`references/api-routes.md`](references/api-routes.md) | Route Handlers with GET/POST/etc. |
50
+ | "How does caching work?" | [`references/caching-revalidation.md`](references/caching-revalidation.md) | Caching layers and revalidation strategies |
51
+ | "How do I set metadata/SEO?" | [`references/metadata-seo.md`](references/metadata-seo.md) | Static and dynamic metadata, Open Graph |
52
+ | "I'm getting an error..." | [`references/common-errors.md`](references/common-errors.md) | Common errors and solutions |
53
+ | "I need official latest behavior" | [`references/remote-docs.md`](references/remote-docs.md) | Use official docs and llms index |
54
+
55
+ ### Priority order for writing code
56
+
57
+ 1. **Embedded/local project docs first**
58
+
59
+ - Use `references/embedded-docs.md`.
60
+ - Confirm actual behavior with local config and `npx next build`.
61
+
62
+ 2. **Local skill references second**
63
+
64
+ - Use App Router/API/caching/metadata reference files in this skill.
65
+
66
+ 3. **Official docs third**
67
+
68
+ - Use `references/remote-docs.md` and `https://nextjs.org/docs/llms.txt`.
69
+
70
+ ## Core concepts
71
+
72
+ ### App Router file conventions
73
+
74
+ ```
75
+ app/
76
+ ├── layout.tsx # Root layout (required)
77
+ ├── page.tsx # Home page (/)
78
+ ├── loading.tsx # Loading UI
79
+ ├── error.tsx # Error boundary
80
+ ├── not-found.tsx # 404 page
81
+ ├── about/
82
+ │ └── page.tsx # /about
83
+ ├── blog/
84
+ │ ├── page.tsx # /blog
85
+ │ └── [slug]/
86
+ │ └── page.tsx # /blog/:slug
87
+ └── api/
88
+ └── users/
89
+ └── route.ts # API: /api/users
90
+ ```
91
+
92
+ ### Server vs Client Components
93
+
94
+ | Feature | Server Component (default) | Client Component (`'use client'`) |
95
+ | --- | --- | --- |
96
+ | Rendering | Server only | Server + Client |
97
+ | Interactivity | No (no useState, onClick) | Yes |
98
+ | Data fetching | Direct (async/await) | Via hooks or API |
99
+ | Bundle size | Not in JS bundle | In JS bundle |
100
+
101
+ ### When to use `'use client'`
102
+
103
+ - Interactive UI (forms, buttons with handlers)
104
+ - Browser APIs (localStorage, window)
105
+ - React hooks (useState, useEffect, useContext)
106
+ - Event handlers (onClick, onChange)
107
+
108
+ ## Critical requirements
109
+
110
+ ### Root layout is required
111
+
112
+ ```typescript
113
+ // app/layout.tsx — REQUIRED
114
+ export default function RootLayout({ children }: { children: React.ReactNode }) {
115
+ return (
116
+ <html lang="en">
117
+ <body>{children}</body>
118
+ </html>
119
+ );
120
+ }
121
+ ```
122
+
123
+ ### Pages must export a default component
124
+
125
+ ```typescript
126
+ // app/page.tsx
127
+ export default function Home() {
128
+ return <h1>Hello World</h1>;
129
+ }
130
+ ```
131
+
132
+ ## When you see errors
133
+
134
+ **Most Next.js issues are version/config mismatches before they are code logic bugs.**
135
+
136
+ 1. Re-check local version and config.
137
+ 2. Verify route conventions in local files.
138
+ 3. Confirm behavior in official docs (`references/remote-docs.md`).
139
+
140
+ ## Step-by-step workflow
141
+
142
+ ### 1. Create a Next.js project
143
+
144
+ ```bash
145
+ npx create-next-app@latest my-app --typescript --app --tailwind
146
+ cd my-app
147
+ ```
148
+
149
+ ### 2. Start development
150
+
151
+ ```bash
152
+ npm run dev
153
+ ```
154
+
155
+ ### 3. Build for production
156
+
157
+ ```bash
158
+ npm run build
159
+ npm start
160
+ ```
161
+
162
+ ### 4. Check build output
163
+
164
+ ```bash
165
+ npx next build
166
+ # Review the output for page types: Static, SSR, ISR
167
+ ```
168
+
169
+ ## Error handling
170
+
171
+ ### Common issues
172
+
173
+ | Symptom | Likely Cause | Fix |
174
+ | --- | --- | --- |
175
+ | "useState is not defined" | Using hooks in Server Component | Add `'use client'` directive |
176
+ | "Hydration mismatch" | Server/client HTML differs | Check for browser-only code in render |
177
+ | "Dynamic server usage" | Using dynamic features in static page | Add `export const dynamic = 'force-dynamic'` |
178
+ | Build takes too long | Large pages or data fetching | Check data fetching, use ISR |
179
+
180
+ ## Expected outputs
181
+
182
+ ```bash
183
+ # Successful build
184
+ Route (app) Size First Load JS
185
+ ┌ ○ / 5.2 kB 90 kB
186
+ ├ ○ /about 1.1 kB 86 kB
187
+ ├ λ /api/users 0 B 0 B
188
+ └ ● /blog/[slug] 2.3 kB 87 kB
189
+
190
+ ○ (Static) prerendered as static content
191
+ ● (SSG) prerendered as static HTML (uses getStaticProps)
192
+ λ (Dynamic) server-rendered on demand
193
+ ```
194
+
195
+ ## Cross-references
196
+
197
+ - **Vitest**: [../vitest/SKILL.md](../vitest/SKILL.md) — Unit testing Next.js components
198
+ - **Playwright**: [../playwright/SKILL.md](../playwright/SKILL.md) — E2E testing Next.js applications
199
+ - **Supabase**: [../supabase/SKILL.md](../supabase/SKILL.md) — Backend services for Next.js
200
+
201
+ ## Resources
202
+
203
+ - **App Router**: [`references/app-router.md`](references/app-router.md)
204
+ - **API routes**: [`references/api-routes.md`](references/api-routes.md)
205
+ - **Caching and revalidation**: [`references/caching-revalidation.md`](references/caching-revalidation.md)
206
+ - **Metadata and SEO**: [`references/metadata-seo.md`](references/metadata-seo.md)
207
+ - **Common errors**: [`references/common-errors.md`](references/common-errors.md)
208
+ - **Embedded docs lookup**: [`references/embedded-docs.md`](references/embedded-docs.md)
209
+ - **Official docs lookup**: [`references/remote-docs.md`](references/remote-docs.md)
@@ -0,0 +1,213 @@
1
+ # API Routes (Route Handlers)
2
+
3
+ Create API endpoints using Next.js App Router Route Handlers.
4
+
5
+ ## When to use this reference
6
+
7
+ - Creating REST API endpoints in Next.js
8
+ - Handling different HTTP methods (GET, POST, PUT, DELETE)
9
+ - Working with request/response objects
10
+ - Streaming responses
11
+
12
+ ## Basic Route Handler
13
+
14
+ ```typescript
15
+ // app/api/users/route.ts
16
+ import { NextRequest, NextResponse } from 'next/server';
17
+
18
+ export async function GET() {
19
+ const users = await getUsers();
20
+ return NextResponse.json(users);
21
+ }
22
+
23
+ export async function POST(request: NextRequest) {
24
+ const body = await request.json();
25
+ const user = await createUser(body);
26
+ return NextResponse.json(user, { status: 201 });
27
+ }
28
+ ```
29
+
30
+ ## HTTP methods
31
+
32
+ Export named functions matching HTTP methods:
33
+
34
+ | Export | Method | Use For |
35
+ | --- | --- | --- |
36
+ | `GET` | GET | Fetch data |
37
+ | `POST` | POST | Create data |
38
+ | `PUT` | PUT | Replace data |
39
+ | `PATCH` | PATCH | Partial update |
40
+ | `DELETE` | DELETE | Remove data |
41
+ | `HEAD` | HEAD | Headers only |
42
+ | `OPTIONS` | OPTIONS | CORS preflight |
43
+
44
+ ## Route parameters
45
+
46
+ ```typescript
47
+ // app/api/users/[id]/route.ts
48
+ export async function GET(
49
+ request: NextRequest,
50
+ { params }: { params: Promise<{ id: string }> }
51
+ ) {
52
+ const { id } = await params;
53
+ const user = await getUser(id);
54
+
55
+ if (!user) {
56
+ return NextResponse.json({ error: 'Not found' }, { status: 404 });
57
+ }
58
+
59
+ return NextResponse.json(user);
60
+ }
61
+
62
+ export async function DELETE(
63
+ request: NextRequest,
64
+ { params }: { params: Promise<{ id: string }> }
65
+ ) {
66
+ const { id } = await params;
67
+ await deleteUser(id);
68
+ return new NextResponse(null, { status: 204 });
69
+ }
70
+ ```
71
+
72
+ ## Request handling
73
+
74
+ ### Query parameters
75
+
76
+ ```typescript
77
+ export async function GET(request: NextRequest) {
78
+ const searchParams = request.nextUrl.searchParams;
79
+ const page = searchParams.get('page') || '1';
80
+ const limit = searchParams.get('limit') || '10';
81
+ // ...
82
+ }
83
+ ```
84
+
85
+ ### Request body
86
+
87
+ ```typescript
88
+ export async function POST(request: NextRequest) {
89
+ // JSON body
90
+ const body = await request.json();
91
+
92
+ // Form data
93
+ const formData = await request.formData();
94
+ const name = formData.get('name');
95
+ }
96
+ ```
97
+
98
+ ### Headers
99
+
100
+ ```typescript
101
+ export async function GET(request: NextRequest) {
102
+ const authHeader = request.headers.get('Authorization');
103
+
104
+ return NextResponse.json(data, {
105
+ headers: {
106
+ 'Cache-Control': 'no-store',
107
+ },
108
+ });
109
+ }
110
+ ```
111
+
112
+ ## Response patterns
113
+
114
+ ### JSON response
115
+
116
+ ```typescript
117
+ return NextResponse.json({ message: 'Success' });
118
+ return NextResponse.json({ error: 'Not found' }, { status: 404 });
119
+ ```
120
+
121
+ ### Redirect
122
+
123
+ ```typescript
124
+ import { redirect } from 'next/navigation';
125
+ redirect('/login');
126
+
127
+ // Or with NextResponse
128
+ return NextResponse.redirect(new URL('/login', request.url));
129
+ ```
130
+
131
+ ### Streaming
132
+
133
+ ```typescript
134
+ export async function GET() {
135
+ const stream = new ReadableStream({
136
+ async start(controller) {
137
+ for (let i = 0; i < 10; i++) {
138
+ controller.enqueue(`data: ${i}\n\n`);
139
+ await new Promise((r) => setTimeout(r, 1000));
140
+ }
141
+ controller.close();
142
+ },
143
+ });
144
+
145
+ return new Response(stream, {
146
+ headers: { 'Content-Type': 'text/event-stream' },
147
+ });
148
+ }
149
+ ```
150
+
151
+ ## Route configuration
152
+
153
+ ```typescript
154
+ // app/api/users/route.ts
155
+
156
+ // Force dynamic (no caching)
157
+ export const dynamic = 'force-dynamic';
158
+
159
+ // Set max duration (for long-running handlers)
160
+ export const maxDuration = 30;
161
+
162
+ // Set runtime
163
+ export const runtime = 'edge'; // or 'nodejs' (default)
164
+ ```
165
+
166
+ ## CORS handling
167
+
168
+ ```typescript
169
+ export async function OPTIONS() {
170
+ return new NextResponse(null, {
171
+ headers: {
172
+ 'Access-Control-Allow-Origin': '*',
173
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
174
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
175
+ },
176
+ });
177
+ }
178
+ ```
179
+
180
+ ## Middleware for API routes
181
+
182
+ ```typescript
183
+ // middleware.ts
184
+ import { NextResponse } from 'next/server';
185
+ import type { NextRequest } from 'next/server';
186
+
187
+ export function middleware(request: NextRequest) {
188
+ if (request.nextUrl.pathname.startsWith('/api/')) {
189
+ const authHeader = request.headers.get('Authorization');
190
+ if (!authHeader) {
191
+ return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
192
+ }
193
+ }
194
+ return NextResponse.next();
195
+ }
196
+
197
+ export const config = {
198
+ matcher: '/api/:path*',
199
+ };
200
+ ```
201
+
202
+ ## Troubleshooting
203
+
204
+ ### Route not found
205
+
206
+ - File must be named `route.ts` (not `api.ts` or `handler.ts`)
207
+ - Cannot have both `route.ts` and `page.tsx` in same directory
208
+ - Check export matches HTTP method name (uppercase)
209
+
210
+ ### Body already consumed
211
+
212
+ - `request.json()` and `request.text()` can only be called once
213
+ - Store the result in a variable