mongog setup
This commit is contained in:
173
src/lib/api.ts
Normal file
173
src/lib/api.ts
Normal file
@@ -0,0 +1,173 @@
|
||||
/**
|
||||
* API Service Layer
|
||||
* Handles all communication with the backend API
|
||||
*/
|
||||
|
||||
const API_BASE_URL = 'http://localhost:8001'
|
||||
|
||||
type ApiOptions = {
|
||||
method?: 'GET' | 'POST' | 'PUT' | 'DELETE'
|
||||
body?: unknown
|
||||
token?: string | null
|
||||
}
|
||||
|
||||
async function apiCall<T>(
|
||||
endpoint: string,
|
||||
options: ApiOptions = {}
|
||||
): Promise<T> {
|
||||
const { method = 'GET', body, token } = options
|
||||
|
||||
const headers: HeadersInit = {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`
|
||||
}
|
||||
|
||||
const config: RequestInit = {
|
||||
method,
|
||||
headers,
|
||||
credentials: 'include',
|
||||
}
|
||||
|
||||
if (body) {
|
||||
config.body = JSON.stringify(body)
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_BASE_URL}${endpoint}`, config)
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({}))
|
||||
throw new Error(error.detail || `API error: ${response.statusText}`)
|
||||
}
|
||||
|
||||
return response.json() as Promise<T>
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// USER ENDPOINTS
|
||||
// ============================================
|
||||
|
||||
export async function registerUser(
|
||||
userData: {
|
||||
email: string
|
||||
displayName?: string
|
||||
photoURL?: string
|
||||
},
|
||||
token: string
|
||||
) {
|
||||
return apiCall('/api/users/register', {
|
||||
method: 'POST',
|
||||
body: userData,
|
||||
token,
|
||||
})
|
||||
}
|
||||
|
||||
export async function getUserByEmail(email: string, token: string) {
|
||||
return apiCall(`/api/users/by-email/${email}`, { token })
|
||||
}
|
||||
|
||||
export async function updateUserProfile(
|
||||
userId: string,
|
||||
updates: { displayName?: string; photoURL?: string; theme?: string },
|
||||
token: string
|
||||
) {
|
||||
return apiCall(`/api/users/update/${userId}`, {
|
||||
method: 'PUT',
|
||||
body: updates,
|
||||
token,
|
||||
})
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// ENTRY ENDPOINTS
|
||||
// ============================================
|
||||
|
||||
export interface JournalEntryCreate {
|
||||
title: string
|
||||
content: string
|
||||
mood?: string
|
||||
tags?: string[]
|
||||
isPublic?: boolean
|
||||
}
|
||||
|
||||
export interface JournalEntry extends JournalEntryCreate {
|
||||
id: string
|
||||
userId: string
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
}
|
||||
|
||||
export async function createEntry(
|
||||
userId: string,
|
||||
entryData: JournalEntryCreate,
|
||||
token: string
|
||||
) {
|
||||
return apiCall<{ id: string; message: string }>(
|
||||
`/api/entries/${userId}`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: entryData,
|
||||
token,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export async function getUserEntries(
|
||||
userId: string,
|
||||
token: string,
|
||||
limit = 50,
|
||||
skip = 0
|
||||
) {
|
||||
return apiCall<{ entries: JournalEntry[]; total: number }>(
|
||||
`/api/entries/${userId}?limit=${limit}&skip=${skip}`,
|
||||
{ token }
|
||||
)
|
||||
}
|
||||
|
||||
export async function getEntry(
|
||||
userId: string,
|
||||
entryId: string,
|
||||
token: string
|
||||
) {
|
||||
return apiCall<JournalEntry>(`/api/entries/${userId}/${entryId}`, {
|
||||
token,
|
||||
})
|
||||
}
|
||||
|
||||
export async function updateEntry(
|
||||
userId: string,
|
||||
entryId: string,
|
||||
updates: Partial<JournalEntryCreate>,
|
||||
token: string
|
||||
) {
|
||||
return apiCall(`/api/entries/${userId}/${entryId}`, {
|
||||
method: 'PUT',
|
||||
body: updates,
|
||||
token,
|
||||
})
|
||||
}
|
||||
|
||||
export async function deleteEntry(
|
||||
userId: string,
|
||||
entryId: string,
|
||||
token: string
|
||||
) {
|
||||
return apiCall(`/api/entries/${userId}/${entryId}`, {
|
||||
method: 'DELETE',
|
||||
token,
|
||||
})
|
||||
}
|
||||
|
||||
export async function getEntriesByDate(
|
||||
userId: string,
|
||||
startDate: string,
|
||||
endDate: string,
|
||||
token: string
|
||||
) {
|
||||
return apiCall<JournalEntry[]>(
|
||||
`/api/entries/${userId}/date-range?startDate=${startDate}&endDate=${endDate}`,
|
||||
{ token }
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user