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
7const __dirname = fileURLToPath(new URL('.', import.meta.url))
8const snippetsDir = resolve(__dirname, '../../../.db-snippets')
9
10const trustStrategy = createAlwaysTrustedStrategy()
11
12const snippetStorage = createFileSnippetStorage({
13 directory: snippetsDir,
14 trustStrategy,
15})
16
17export const Route = createFileRoute('/_database-demo/api/db-snippets')({
18 server: {
19 handlers: {
20 GET: async () => {
21 try {
22 const snippetIndex = await snippetStorage.loadIndex()
23
24 const snippetsWithStats = await Promise.all(
25 snippetIndex.map(async (snippet) => {
26 const full = await snippetStorage.get(snippet.name)
27 return {
28 id: snippet.id,
29 name: snippet.name,
30 description: snippet.description,
31 usageHints: snippet.usageHints,
32 trustLevel: snippet.trustLevel,
33 code: full?.code ?? '',
34 stats: full?.stats ?? { executions: 0, successRate: 0 },
35 }
36 }),
37 )
38
39 return new Response(JSON.stringify(snippetsWithStats), {
40 headers: { 'Content-Type': 'application/json' },
41 })
42 } catch (error) {
43 console.error('[API DB Snippets] Error loading snippets:', error)
44 return new Response(
45 JSON.stringify({ error: 'Failed to load snippets' }),
46 {
47 status: 500,
48 headers: { 'Content-Type': 'application/json' },
49 },
50 )
51 }
52 },
53
54 DELETE: async ({ request }) => {
55 try {
56 const url = new URL(request.url)
57 const deleteAll = url.searchParams.get('all') === 'true'
58
59 if (deleteAll) {
60 const snippetIndex = await snippetStorage.loadIndex()
61 await Promise.all(
62 snippetIndex.map((snippet) =>
63 snippetStorage.delete(snippet.name),
64 ),
65 )
66 return new Response(
67 JSON.stringify({ success: true, deleted: snippetIndex.length }),
68 {
69 headers: { 'Content-Type': 'application/json' },
70 },
71 )
72 }
73
74 const name = url.searchParams.get('name')
75
76 if (!name) {
77 return new Response(
78 JSON.stringify({ error: 'Missing snippet name' }),
79 {
80 status: 400,
81 headers: { 'Content-Type': 'application/json' },
82 },
83 )
84 }
85
86 const deleted = await snippetStorage.delete(name)
87
88 if (!deleted) {
89 return new Response(
90 JSON.stringify({ error: `Snippet '${name}' not found` }),
91 {
92 status: 404,
93 headers: { 'Content-Type': 'application/json' },
94 },
95 )
96 }
97
98 return new Response(
99 JSON.stringify({ success: true, deleted: name }),
100 {
101 headers: { 'Content-Type': 'application/json' },
102 },
103 )
104 } catch (error) {
105 console.error('[API DB Snippets] Error deleting snippet:', error)
106 return new Response(
107 JSON.stringify({ error: 'Failed to delete snippet' }),
108 {
109 status: 500,
110 headers: { 'Content-Type': 'application/json' },
111 },
112 )
113 }
114 },
115 },
116 },
117})