Files
accounted/.claude/skills/create-extension/references/api-routes.md
T
Jakob WennbergandClaude Sonnet 5 ec27228a8e style: remove em/en dashes repo-wide, add CLAUDE.md rule against them (#890)
Em dashes (—) and en dashes (–) had spread across comments, docs, tests,
and a few UI strings, reading as AI-generated boilerplate rather than
house style. Replaced each with punctuation matching its context: colon
for explanatory clauses, comma for asides, plain hyphen for numeric/legal
ranges (e.g. "21-23§"), "to"/"till" for date ranges, parentheses for
paired-dash asides. messages/en.json and messages/sv.json were fixed by
hand together to keep sv/en in sync.

Left untouched where the dash is the functional subject rather than
decorative punctuation: date-range-parser.ts's separator regex,
charset-repair.ts's CP1252 byte-mapping table (and its test), the SIE
encoding mojibake docs, generic-csv.ts's minus-sign normalizer, the
agent system-prompt files that already instruct against em dashes, and
a golden iXBRL test fixture compared byte-for-byte.

Also fixes two bugs surfaced along the way: an off-by-one in
ApiKeysPanel's scope-label split (a leftover from an earlier partial
pass), and a charset-repair test that had lost the literal en-dash it
exists to verify.

Regenerated the agent atom seed migration (skills:generate) since 27
SKILL.md files changed. Added a CLAUDE.md rule against em/en dashes,
with an explicit carve-out for the functional-dash cases above.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-04 15:58:06 +02:00

91 lines
2.8 KiB
Markdown

# API Routes Reference
All extension APIs dispatch through `app/api/extensions/ext/[...path]/route.ts`.
**URL scheme:** `/api/extensions/ext/{extensionId}/{routePath}`
## Route Definition
```typescript
import type { ApiRouteDefinition, ExtensionContext } from '@/lib/extensions/types'
import { NextResponse } from 'next/server'
export const myExtApiRoutes: ApiRouteDefinition[] = [
{
method: 'GET',
path: '/',
handler: async (_req, ctx) => {
const { data, error } = await ctx!.supabase
.from('my_items').select('*')
.eq('user_id', ctx!.userId).order('created_at', { ascending: false })
if (error) return NextResponse.json({ error: error.message }, { status: 500 })
return NextResponse.json({ data })
},
},
]
```
## Path Parameters
Use `:paramName`; dispatcher extracts as `_paramName` search params:
```typescript
{
method: 'GET',
path: '/:id',
handler: async (req, ctx) => {
const id = new URL(req.url).searchParams.get('_id')
const { data } = await ctx!.supabase
.from('my_items').select('*').eq('id', id).eq('user_id', ctx!.userId).single()
if (!data) return NextResponse.json({ error: 'Not found' }, { status: 404 })
return NextResponse.json({ data })
},
}
// Multiple params: path: '/:id/items/:itemId' → _id, _itemId
```
## Dispatcher Flow
1. Extract `extensionId` and `routePath` from URL segments
2. Auth check (401) → match method+path (404) → AI consent check (403 for AI extensions)
3. Extract path params → create `ExtensionContext` → call handler
## Settings Route Pattern
```typescript
{ method: 'GET', path: '/settings',
handler: async (_req, ctx) => {
const settings = await ctx!.settings.get<MySettings>()
return NextResponse.json({ data: settings ?? DEFAULT_SETTINGS })
},
},
{ method: 'PUT', path: '/settings',
handler: async (req, ctx) => {
const body = await req.json()
const merged = { ...(await ctx!.settings.get<MySettings>() ?? DEFAULT_SETTINGS), ...body }
await ctx!.settings.set('settings', merged)
return NextResponse.json({ data: merged })
},
},
```
## Response Conventions
```typescript
NextResponse.json({ data: result }) // Success
NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) // Auth error
NextResponse.json({ error: 'Not found' }, { status: 404 }) // Not found
NextResponse.json({ error: error.message }, { status: 500 }) // Server error
```
## Frontend Calls
```typescript
const res = await fetch('/api/extensions/ext/my-extension/')
const res = await fetch(`/api/extensions/ext/my-extension/${id}`)
const res = await fetch('/api/extensions/ext/my-extension/settings', {
method: 'PUT', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ featureEnabled: true }),
})
```