1import fetchWithCache from "./cache.ts";
2
3export type APOD = {
18 const cacheMinutes = 60;
19
20 const data: APOD = await fetchWithCache(url, cacheKey, cacheMinutes).then((res) => res.json());
21
22 if (!data) {
30 return c.json({ files: files.data });
31 } catch (error) {
32 console.error("Error fetching project files:", error);
33 return Response.json({ error: "Failed to fetch project files" }, { status: 500 });
34 }
35});
21 return c.json({ branches: branches.data });
22 } catch (error) {
23 console.error("Error fetching branches:", error);
24 return Response.json({ error: "Failed to fetch branches" }, { status: 500 });
25 }
26});
1import { useState, useEffect } from "https://esm.sh/react@18.2.0?dev";
2import { fetchProjectFiles } from "../utils/api.ts";
3
4interface UseProjectFilesProps {
15
16/**
17 * Custom hook to fetch and manage project files
18 */
19export function useProjectFiles({
38
39 try {
40 const filesData = await fetchProjectFiles({
41 bearerToken,
42 projectId,
51 }
52 } catch (err) {
53 console.error("Error fetching project files:", err);
54 setProjectFiles([]);
55 setError(err instanceof Error ? err : new Error(String(err)));
71```
72
735. **fetchTranspiledJavaScript** - Fetch and transpile TypeScript to JavaScript:
74```ts
75const jsCode = await fetchTranspiledJavaScript("https://esm.town/v/username/project/path/to/file.ts");
76```
77
200
201 // Inject data to avoid extra round-trips
202 const initialData = await fetchInitialData();
203 const dataScript = `<script>
204 window.__INITIAL_DATA__ = ${JSON.stringify(initialData)};
258
2595. **API Design:**
260 - `fetch` handler is the entry point for HTTP vals
261 - Run the Hono app with `export default app.fetch // This is the entry point for HTTP vals`
262 - Properly handle CORS if needed for external access
10
11async function loader({ bearerToken }: { bearerToken: string }) {
12 const data = await (await fetch("/api/projects-loader", {
13 headers: {
14 "Authorization": "Bearer " + bearerToken,
28});
29
30export default app.fetch;
128 }
129
130 // If there are selected files, fetch their content and add them to the messages
131 if (selectedFiles && selectedFiles.length > 0) {
132 const vt = new ValTown({ bearerToken });
148 fileContents += `## File: ${filePath}\n\`\`\`\n${content}\n\`\`\`\n\n`;
149 } catch (error) {
150 console.error(`Error fetching file ${filePath}:`, error);
151 fileContents += `## File: ${filePath}\nError: Could not fetch file content\n\n`;
152 }
153 }
260 return c.json({ files: files.data });
261 } catch (error) {
262 console.error("Error fetching project files:", error);
263 return Response.json({ error: "Failed to fetch project files" }, { status: 500 });
264 }
265});
282 return c.json({ branches: branches.data });
283 } catch (error) {
284 console.error("Error fetching branches:", error);
285 return Response.json({ error: "Failed to fetch branches" }, { status: 500 });
286 }
287});
43
44 try {
45 const response = await fetch("/api/create-branch", {
46 method: "POST",
47 headers: {
28 const [isDragging, setIsDragging] = useState(false);
29
30 // Use custom hook to fetch project files
31 const {
32 projectFiles,