11app.get("*", async (c, next) => {
12 const path = c.req.path;
13 if (path.startsWith("/api/") || c.req.header("Accept")?.includes("application/json")) {
14 return next();
15 }
21});
22
23app.route("/api", backend);
24app.get("/frontend/*", c => {
25 return serveFile(c.req.path, import.meta.url);
63 refetch: () => void;
64}) {
65 const { token, anthropicApiKey } = useAuth();
66 const [images, setImages] = useState<(string|null)[]>([]);
67 const [selectedFiles, setSelectedFiles] = useState<string[]>([]);
81 project,
82 branchId,
83 anthropicApiKey,
84 bearerToken: token,
85 selectedFiles,
59});
60
61// API endpoint to get all blog posts
62app.get("/api/posts", async c => {
63 try {
64 const posts = await getBlogPosts();
70});
71
72// API endpoint to get a specific blog post
73app.get("/api/posts/:slug", async c => {
74 try {
75 const slug = c.req.param("slug");
2import { useAuth } from "./useAuth.tsx";
3
4const PROJECT_ENDPOINT = "/api/project";
5const FILES_ENDPOINT = "/api/project-files";
6
7export function useProject (projectId: string, branchId?: string) {
2import { useAuth } from "./useAuth.tsx";
3
4const ENDPOINT = "/api/projects-loader";
5
6export function useProjects () {
3import { useAuth } from "./useAuth.tsx";
4
5const ENDPOINT = "/api/create-project";
6
7export function useCreateProject () {
2import { useAuth } from "./useAuth.tsx";
3
4const ENDPOINT = "/api/create-branch";
5
6export function useCreateBranch (projectId: string) {
6 project: any;
7 branchId: string | undefined;
8 anthropicApiKey: string;
9 bearerToken: string;
10 selectedFiles: string[];
16 project,
17 branchId,
18 anthropicApiKey,
19 bearerToken,
20 selectedFiles,
37 status,
38 } = useChat({
39 api: "/api/send-message",
40 body: {
41 project,
42 branchId,
43 anthropicApiKey,
44 selectedFiles,
45 images: images
2import { useAuth } from "./useAuth.tsx";
3
4const ENDPOINT = "/api/project-branches";
5
6export function useBranches (projectId: string) {
3
4const TOKEN_KEY = "bearer";
5const ANTHROPIC_KEY = "anthropic_api_key";
6
7export function useAuth () {
8 const [token, setToken, removeToken] = useLocalStorage(TOKEN_KEY, "");
9 const [anthropicApiKey, setAnthropicApiKey, removeAnthropicApiKey] = useLocalStorage(ANTHROPIC_KEY, "");
10 const [error, setError] = useState(null);
11
12 const isAuthenticated = !!token && !!anthropicApiKey;
13
14 const authenticate = async (valTownAPIKey: string, anthropicKey: string) => {
15 // replace all this with oauth when it's ready
16 try {
17 const res = await fetch("/api/user", {
18 headers: {
19 "Authorization": "Bearer " + valTownAPIKey,
20 },
21 });
25 setError(data.error);
26 removeToken();
27 removeAnthropicApiKey();
28 return;
29 }
30 setError(null);
31 setToken(valTownAPIKey);
32 setAnthropicApiKey(anthropicKey);
33 } catch (e) {
34 console.error(e);
35 setError(e.error);
36 removeToken();
37 removeAnthropicApiKey();
38 }
39 }
41 const logOut = () => {
42 removeToken();
43 removeAnthropicApiKey();
44 }
45
50 logOut,
51 token,
52 anthropicApiKey,
53 };
54}