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, '../../../.structured-output-snippets')
9const trustStrategy = createAlwaysTrustedStrategy()
10const snippetStorage = createFileSnippetStorage({
11 directory: snippetsDir,
12 trustStrategy,
13})
14
15export const Route = createFileRoute(
16 '/_structured-output/api/structured-output-snippets',
17)({
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(
44 '[API Structured Output Snippets] Error loading snippets:',
45 error,
46 )
47 return new Response(
48 JSON.stringify({ error: 'Failed to load snippets' }),
49 {
50 status: 500,
51 headers: { 'Content-Type': 'application/json' },
52 },
53 )
54 }
55 },
56
57 DELETE: async ({ request }) => {
58 try {
59 const url = new URL(request.url)
60 const deleteAll = url.searchParams.get('all') === 'true'
61
62 if (deleteAll) {
63 const snippetIndex = await snippetStorage.loadIndex()
64 await Promise.all(
65 snippetIndex.map((snippet) =>
66 snippetStorage.delete(snippet.name),
67 ),
68 )
69 return new Response(
70 JSON.stringify({ success: true, deleted: snippetIndex.length }),
71 {
72 headers: { 'Content-Type': 'application/json' },
73 },
74 )
75 }
76
77 const name = url.searchParams.get('name')
78
79 if (!name) {
80 return new Response(
81 JSON.stringify({ error: 'Missing snippet name' }),
82 {
83 status: 400,
84 headers: { 'Content-Type': 'application/json' },
85 },
86 )
87 }
88
89 const deleted = await snippetStorage.delete(name)
90
91 if (!deleted) {
92 return new Response(
93 JSON.stringify({ error: `Snippet '${name}' not found` }),
94 {
95 status: 404,
96 headers: { 'Content-Type': 'application/json' },
97 },
98 )
99 }
100
101 return new Response(
102 JSON.stringify({ success: true, deleted: name }),
103 {
104 headers: { 'Content-Type': 'application/json' },
105 },
106 )
107 } catch (error) {
108 console.error(
109 '[API Structured Output Snippets] Error deleting snippet:',
110 error,
111 )
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})