Skip to content

Commit 9440f3b

Browse files
feat(vibes-api): restructure API modules and implement social API functions
1 parent 8d43c4b commit 9440f3b

File tree

5 files changed

+100
-34
lines changed

5 files changed

+100
-34
lines changed

src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export default function App() {
1616
syncThemeWithLocal();
1717
updateAppLanguage(i18n);
1818

19-
// Auto-open last project on startup
20-
autoOpenLastProject();
19+
// Auto-open last project on startup (temporarily disabled)
20+
// autoOpenLastProject();
2121
}, [i18n, autoOpenLastProject]);
2222

2323
return <RouterProvider router={router} />;

src/lib/vibes-api/api/fetchApi.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const VIBES_API_URL = process.env.API_URL || "http://localhost:3000";
2+
3+
export const fetchApi = async (path: string, options: RequestInit = {}) => {
4+
// Remove leading slash if present to avoid double slashes
5+
const cleanPath = path.startsWith('/') ? path.slice(1) : path;
6+
const finalUrl = `${VIBES_API_URL}/${cleanPath}`;
7+
8+
return fetch(finalUrl, {
9+
...options,
10+
credentials: 'include', // Include cookies for session management
11+
headers: {
12+
'Content-Type': 'application/json',
13+
...options.headers,
14+
},
15+
});
16+
};

src/lib/vibes-api/api/social.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { fetchApi } from "./fetchApi";
2+
3+
export const socialApi = {
4+
follow: (followerId: number, followingId: number) =>
5+
fetchApi("api/v1/social/follow", {
6+
method: "POST",
7+
body: JSON.stringify({ followerId, followingId }),
8+
}),
9+
unfollow: (followerId: number, followingId: number) =>
10+
fetchApi("api/v1/social/unfollow", {
11+
method: "POST",
12+
body: JSON.stringify({ followerId, followingId }),
13+
}),
14+
createPost: (authorId: number, content: string) =>
15+
fetchApi("api/v1/social/post", {
16+
method: "POST",
17+
body: JSON.stringify({ authorId, content }),
18+
}),
19+
likePost: (userId: number, postId: number) =>
20+
fetchApi("api/v1/social/like", {
21+
method: "POST",
22+
body: JSON.stringify({ userId, postId }),
23+
}),
24+
unlikePost: (userId: number, postId: number) =>
25+
fetchApi("api/v1/social/unlike", {
26+
method: "POST",
27+
body: JSON.stringify({ userId, postId }),
28+
}),
29+
bookmarkPost: (userId: number, postId: number) =>
30+
fetchApi("api/v1/social/bookmark", {
31+
method: "POST",
32+
body: JSON.stringify({ userId, postId }),
33+
}),
34+
unbookmarkPost: (userId: number, postId: number) =>
35+
fetchApi("api/v1/social/unbookmark", {
36+
method: "POST",
37+
body: JSON.stringify({ userId, postId }),
38+
}),
39+
createComment: (authorId: number, postId: number, content: string, parentId?: number) =>
40+
fetchApi("api/v1/social/comment", {
41+
method: "POST",
42+
body: JSON.stringify({ authorId, postId, content, parentId }),
43+
}),
44+
likeComment: (userId: number, commentId: number) =>
45+
fetchApi("api/v1/social/like-comment", {
46+
method: "POST",
47+
body: JSON.stringify({ userId, commentId }),
48+
}),
49+
unlikeComment: (userId: number, commentId: number) =>
50+
fetchApi("api/v1/social/unlike-comment", {
51+
method: "POST",
52+
body: JSON.stringify({ userId, commentId }),
53+
}),
54+
bookmarkComment: (userId: number, commentId: number) =>
55+
fetchApi("api/v1/social/bookmark-comment", {
56+
method: "POST",
57+
body: JSON.stringify({ userId, commentId }),
58+
}),
59+
unbookmarkComment: (userId: number, commentId: number) =>
60+
fetchApi("api/v1/social/unbookmark-comment", {
61+
method: "POST",
62+
body: JSON.stringify({ userId, commentId }),
63+
}),
64+
};

src/lib/vibes-api/auth/client.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createAuthClient } from "better-auth/react";
2+
import { reactStartCookies } from "better-auth/react-start";
3+
import { VIBES_API_URL } from "../api/fetchApi";
4+
5+
export const authClient = createAuthClient({
6+
baseURL: VIBES_API_URL,
7+
plugins: [reactStartCookies()],
8+
});

src/lib/vibes-api/index.ts

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,10 @@
1-
import { createAuthClient } from "better-auth/react" // make sure to import from better-auth/react
2-
import { reactStartCookies } from "better-auth/react-start";
3-
4-
export const VIBES_API_URL = process.env.API_URL || "http://localhost:3000";
5-
6-
export const fetchApi = async (path: string, options: RequestInit = {}) => {
7-
const url = `${VIBES_API_URL}/${path}`;
8-
9-
// Remove leading slash if present to avoid double slashes
10-
const cleanPath = path.startsWith('/') ? path.slice(1) : path;
11-
const finalUrl = `${VIBES_API_URL}/${cleanPath}`;
12-
13-
return fetch(finalUrl, {
14-
...options,
15-
credentials: 'include', // Include cookies for session management
16-
headers: {
17-
'Content-Type': 'application/json',
18-
...options.headers,
19-
},
20-
});
21-
}
22-
23-
24-
export const authClient = createAuthClient({
25-
//you can pass client configuration here
26-
baseURL: VIBES_API_URL,
27-
plugins: [
28-
reactStartCookies(),
29-
]
30-
})
31-
32-
// All authentication logic is now handled by authClient from better-auth/react
1+
import { fetchApi, VIBES_API_URL } from "./api/fetchApi";
2+
import { authClient } from "./auth/client";
3+
import { socialApi } from "./api/social";
4+
5+
export const vibesApi = {
6+
fetchApi,
7+
VIBES_API_URL,
8+
authClient,
9+
social: socialApi,
10+
};

0 commit comments

Comments
 (0)