This document explains how the frontend is integrated with the NetWorkr backend API.
Create a .env file in the root directory with the following:
VITE_API_BASE_URL=http://localhost:8017/v1Note: The backend API uses /v1 not /api/v1. Make sure your base URL doesn't include /api.
If no environment variable is set, the API defaults to:
- Development:
http://localhost:8017/v1
src/
├── config/
│ └── api.config.js # API configuration (base URL, timeouts)
├── utils/
│ ├── apiClient.js # HTTP client utilities (apiGet, apiPost, etc.)
│ └── auth.js # Authentication utilities (token management)
└── apis/
└── api.js # API function definitions
The API client provides standardized HTTP request methods:
apiGet(endpoint, options)- GET requestsapiPost(endpoint, body, options)- POST requestsapiPatch(endpoint, body, options)- PATCH requestsapiDelete(endpoint, options)- DELETE requestsapiUpload(endpoint, formData, options)- File uploads (multipart/form-data)
Features:
- Automatic JWT token injection from
localStorage - Automatic 401 handling (redirects to login)
- Standardized error handling
- Request timeout (30 seconds default)
- JSON response parsing
The API client automatically:
- Retrieves the JWT token from
localStorage(viagetAuthToken()) - Adds
Authorization: Bearer <token>header to all requests - Handles 401 responses by clearing auth and redirecting to login
All API functions follow this pattern:
export const functionName = async (params) => {
try {
const response = await apiGet('/endpoint', options)
if (response.success && response.data) {
return {
success: true,
data: response.data,
message: response.message
}
}
return {
success: false,
message: response.message || 'Operation failed'
}
} catch (error) {
return {
success: false,
message: error.message || 'Network error'
}
}
}- Base URL: The backend uses
/v1not/api/v1. The base URL should behttp://localhost:8017/v1. - Error Handling: The API client now properly handles non-JSON responses (like HTML error pages) and provides better error messages.
- Response Format: The backend returns
{ success: true/false, message: "...", data: {...} }format.
The frontend is configured to work with the following backend endpoints (all under /v1):
POST /v1/registrations- Register new userPOST /v1/verifications- Verify account with OTPPOST /v1/verifications/resend- Resend OTPPOST /v1/sessions- LoginDELETE /v1/sessions- LogoutPATCH /v1/sessions/password- Change passwordPOST /v1/password-resets- Request password resetPATCH /v1/password-resets- Complete password reset
GET /v1/users/me- Get current userGET /v1/users/:id- Get user by IDPATCH /v1/users/me- Update current user profilePOST /v1/users/me/avatar- Upload avatarPOST /v1/users/me/background- Upload background imageGET /v1/users/me/completeness- Get profile completenessGET /v1/users- Search users (with query params)POST /v1/users/me/experiences- Add work experiencePOST /v1/users/me/skills- Add skill
GET /v1/feeds/main- Get personalized feedPOST /v1/posts- Create post (supports multipart for media)POST /v1/posts/:id/interactions- Interact with post (like/share)POST /v1/posts/:id/comments- Add commentGET /v1/posts/:id/comments- Get comments
(Endpoints may vary based on backend implementation)
(Endpoints may vary based on backend implementation)
(Endpoints may vary based on backend implementation)
(Endpoints may vary based on backend implementation)
(Endpoints may vary based on backend implementation)
Note: Refer to the API documentation at http://localhost:8017/api-docs/ for the complete list of available endpoints and their exact specifications.
The backend returns responses in this format:
{
success: true, // or false
data: { ... }, // Response data (optional)
message: "...", // Human-readable message (optional)
error: "..." // Error message (optional, if success is false)
}All API functions handle errors gracefully:
- Network Errors: Returns
{ success: false, message: 'Network error...' } - HTTP Errors: Parses error message from response
- 401 Unauthorized: Automatically clears auth and redirects to login
- Timeouts: Returns timeout error message after 30 seconds
import { login } from '~/apis/api'
import { saveAuth } from '~/utils/auth'
const handleLogin = async (email, password) => {
const response = await login(email, password)
if (response.success) {
saveAuth(response.token, response.user)
// Redirect to dashboard
} else {
// Show error: response.message
}
}import { createPost } from '~/apis/api'
const handleCreatePost = async (content, mediaFiles) => {
const response = await createPost({
content,
media: mediaFiles, // File objects or URLs
visibility: 'PUBLIC'
})
if (response.success) {
// Post created: response.post
}
}import { uploadAvatar } from '~/apis/api'
const handleAvatarUpload = async (file) => {
const response = await uploadAvatar(file)
if (response.success) {
// Avatar uploaded: response.avatarUrl
}
}This API integration replaces the previous mock API functions. Key changes:
- Removed: Mock data functions (
getUsersData,getPostsData, etc.) - Removed:
simulateApiDelayfunction - Removed: localStorage-based mock data storage
- Added: Real HTTP requests using
fetchAPI - Added: JWT token authentication
- Added: Proper error handling and 401 redirects
All existing API function signatures remain the same for backward compatibility, but they now make real HTTP requests instead of returning mock data.