Open History to see the last commit
1import { resolve } from 'node:path'
2import { fileURLToPath } from 'node:url'
3import { createFileRoute } from '@tanstack/react-router'
4import { createFileSnippetStorage } from '@tanstack/ai-code-mode-snippets/storage'
5import { createAlwaysTrustedStrategy } from '@tanstack/ai-code-mode-snippets'
6
7// Resolve snippets directory relative to project root
8const __dirname = fileURLToPath(new URL('.', import.meta.url))
9const snippetsDir = resolve(__dirname, '../../../.snippets')
10
11// Use the same trust strategy as the main snippets endpoint
12const trustStrategy = createAlwaysTrustedStrategy()
13
14// Use the same storage as the snippets endpoint
15const snippetStorage = createFileSnippetStorage({
16 directory: snippetsDir,
17 trustStrategy,
18})
19
20export const Route = createFileRoute('/_home/api/snippets')({
21 server: {
22 handlers: {
23 // GET - List all snippets with stats
24 GET: async () => {
25 try {
26 const snippetIndex = await snippetStorage.loadIndex()
27
28 // Load full stats and code for each snippet
29 const snippetsWithStats = await Promise.all(
30 snippetIndex.map(async (snippet) => {
31 const full = await snippetStorage.get(snippet.name)
32 return {
33 id: snippet.id,
34 name: snippet.name,
35 description: snippet.description,
36 usageHints: snippet.usageHints,
37 trustLevel: snippet.trustLevel,
38 code: full?.code ?? '',
39 stats: full?.stats ?? { executions: 0, successRate: 0 },
40 }
41 }),
42 )
43
44 return new Response(JSON.stringify(snippetsWithStats), {
45 headers: { 'Content-Type': 'application/json' },
46 })
47 } catch (error) {
48 console.error('[API Snippets] Error loading snippets:', error)
49 return new Response(
50 JSON.stringify({ error: 'Failed to load snippets' }),
51 {
52 status: 500,
53 headers: { 'Content-Type': 'application/json' },
54 },
55 )
56 }
57 },
58
59 // DELETE - Delete a snippet by name, or all snippets if ?all=true
60 DELETE: async ({ request }) => {
61 try {
62 const url = new URL(request.url)
63 const deleteAll = url.searchParams.get('all') === 'true'
64
65 if (deleteAll) {
66 const snippetIndex = await snippetStorage.loadIndex()
67 await Promise.all(
68 snippetIndex.map((snippet) =>
69 snippetStorage.delete(snippet.name),
70 ),
71 )
72 return new Response(
73 JSON.stringify({ success: true, deleted: snippetIndex.length }),
74 {
75 headers: { 'Content-Type': 'application/json' },
76 },
77 )
78 }
79
80 const name = url.searchParams.get('name')
81
82 if (!name) {
83 return new Response(
84 JSON.stringify({ error: 'Missing snippet name' }),
85 {
86 status: 400,
87 headers: { 'Content-Type': 'application/json' },
88 },
89 )
90 }
91
92 const deleted = await snippetStorage.delete(name)
93
94 if (!deleted) {
95 return new Response(
96 JSON.stringify({ error: `Snippet '${name}' not found` }),
97 {
98 status: 404,
99 headers: { 'Content-Type': 'application/json' },
100 },
101 )
102 }
103
104 return new Response(
105 JSON.stringify({ success: true, deleted: name }),
106 {
107 headers: { 'Content-Type': 'application/json' },
108 },
109 )
110 } catch (error) {
111 console.error('[API Snippets] Error deleting snippet:', error)
112 return new Response(
113 JSON.stringify({ error: 'Failed to delete snippet' }),
114 {
115 status: 500,
116 headers: { 'Content-Type': 'application/json' },
117 },
118 )
119 }
120 },
121 },
122 },
123})