/
nasya
/
SafeDrop
Обзор
Документация
Войти
/
nasya
/
SafeDrop
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
apps/web/src/lib/api/auth-api.ts
46 строк
2 KB
nasya
Initial commit
24 май 2026, 16:12
24 май 2026, 16:12
16bc032
Код
Авторство
О чём код?
export type CurrentIdentity = { authMode?: string; identityId?: string | null; }; export async function getCurrentIdentity( apiUrl: string, accessToken: string, fetchImpl: typeof fetch = fetch, ): Promise<CurrentIdentity> { const response = await fetchImpl(new URL("/api/me", apiUrl), { headers: { authorization: `Bearer ${accessToken}` }, }); if (!response.ok) { throw new Error(`Не удалось проверить вход: ${response.status}`); } return (await response.json()) as CurrentIdentity; } if (import.meta.rstest) { const { describe, expect, it } = import.meta.rstest; describe("auth API helpers", () => { it("calls /api/me with Bearer access token", async () => { const fetchImpl = (async (input: string | URL | Request, init?: RequestInit) => { expect(String(input)).toBe("http://api.local/api/me"); expect(init?.headers).toEqual({ authorization: "Bearer token-1" }); return new Response(JSON.stringify({ authMode: "hydra", identityId: "identity-1" }), { headers: { "content-type": "application/json" }, }); }) as typeof fetch; const identity = await getCurrentIdentity("http://api.local", "token-1", fetchImpl); expect(identity).toEqual({ authMode: "hydra", identityId: "identity-1" }); }); it("maps identity failures to a controlled error", async () => { const fetchImpl = (async () => new Response("unauthorized", { status: 401 })) as typeof fetch; await expect(getCurrentIdentity("http://api.local", "bad-token", fetchImpl)).rejects.toThrow( "Не удалось проверить вход: 401", ); }); }); }