From 20e59131f297250372ef123329c04e9ef1645562 Mon Sep 17 00:00:00 2001 From: host Date: Sun, 9 Aug 2026 14:23:28 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20frontend=20API-=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=20?= =?UTF-8?q?=D0=B0=D0=B2=D1=82=D0=BE=D1=80=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/api.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 frontend/src/api.js diff --git a/frontend/src/api.js b/frontend/src/api.js new file mode 100644 index 0000000..ced1396 --- /dev/null +++ b/frontend/src/api.js @@ -0,0 +1,41 @@ +const BASE = "/api"; + +async function request(path, options = {}) { + const response = await fetch(`${BASE}${path}`, { + ...options, + headers: { + "Content-Type": "application/json", + ...(options.headers || {}), + }, + }); + const data = await response.json().catch(() => ({})); + if (!response.ok) { + throw new Error(data.detail || "Ошибка запроса"); + } + return data; +} + +export async function register(email, password) { + return request("/auth/register", { + method: "POST", + body: JSON.stringify({ email, password }), + }); +} + +export async function login(email, password) { + const body = new URLSearchParams({ username: email, password }); + const response = await fetch(`${BASE}/auth/login`, { + method: "POST", + headers: { "Content-Type": "application/x-www-form-urlencoded" }, + body, + }); + const data = await response.json().catch(() => ({})); + if (!response.ok) throw new Error(data.detail || "Ошибка входа"); + return data; +} + +export function getMe(token) { + return request("/auth/me", { + headers: { Authorization: `Bearer ${token}` }, + }); +}