diff --git a/7project/frontend/src/api.ts b/7project/frontend/src/api.ts index 2b7a2cb..450c342 100644 --- a/7project/frontend/src/api.ts +++ b/7project/frontend/src/api.ts @@ -23,9 +23,21 @@ function getBaseUrl() { return base || ''; } -function authHeaders() { +function getHeaders(contentType: 'json' | 'form' | 'none' = 'json'): Record { const token = localStorage.getItem('token'); - return token ? { Authorization: `Bearer ${token}` } : {}; + const headers: Record = {}; + + if (contentType === 'json') { + headers['Content-Type'] = 'application/json'; + } else if (contentType === 'form') { + headers['Content-Type'] = 'application/x-www-form-urlencoded'; + } + + if (token) { + headers['Authorization'] = `Bearer ${token}`; + } + + return headers; } export async function login(email: string, password: string): Promise { @@ -62,7 +74,7 @@ export async function register(email: string, password: string, first_name?: str export async function getCategories(): Promise { const res = await fetch(`${getBaseUrl()}/categories/`, { - headers: { 'Content-Type': 'application/json', ...authHeaders() }, + headers: getHeaders(), }); if (!res.ok) throw new Error('Failed to load categories'); return res.json(); @@ -77,7 +89,7 @@ export type CreateTransactionInput = { export async function createTransaction(input: CreateTransactionInput): Promise { const res = await fetch(`${getBaseUrl()}/transactions/create`, { method: 'POST', - headers: { 'Content-Type': 'application/json', ...authHeaders() }, + headers: getHeaders(), body: JSON.stringify(input), }); if (!res.ok) { @@ -89,7 +101,7 @@ export async function createTransaction(input: CreateTransactionInput): Promise< export async function getTransactions(): Promise { const res = await fetch(`${getBaseUrl()}/transactions/`, { - headers: { 'Content-Type': 'application/json', ...authHeaders() }, + headers: getHeaders(), }); if (!res.ok) throw new Error('Failed to load transactions'); return res.json(); @@ -107,7 +119,7 @@ export type User = { export async function getMe(): Promise { const res = await fetch(`${getBaseUrl()}/users/me`, { - headers: { 'Content-Type': 'application/json', ...authHeaders() }, + headers: getHeaders(), }); if (!res.ok) throw new Error('Failed to load user'); return res.json(); @@ -117,7 +129,7 @@ export type UpdateMeInput = Partial> & { export async function updateMe(input: UpdateMeInput): Promise { const res = await fetch(`${getBaseUrl()}/users/me`, { method: 'PATCH', - headers: { 'Content-Type': 'application/json', ...authHeaders() }, + headers: getHeaders(), body: JSON.stringify(input), }); if (!res.ok) { @@ -130,7 +142,7 @@ export async function updateMe(input: UpdateMeInput): Promise { export async function deleteMe(): Promise { const res = await fetch(`${getBaseUrl()}/users/me`, { method: 'DELETE', - headers: { ...authHeaders() }, + headers: getHeaders(), }); if (!res.ok) { const text = await res.text(); diff --git a/7project/frontend/src/appearance.ts b/7project/frontend/src/appearance.ts index 2b14b99..eaed1c4 100644 --- a/7project/frontend/src/appearance.ts +++ b/7project/frontend/src/appearance.ts @@ -5,7 +5,6 @@ const THEME_KEY = 'app_theme'; const FONT_KEY = 'app_font_size'; export function applyTheme(theme: Theme) { - const root = document.documentElement; const body = document.body; const effective = theme === 'system' ? (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme; body.setAttribute('data-theme', effective);