Files
nahodka-front/src/api/phoneApi.js
2026-10-27 22:52:10 +03:00

31 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// src/api/phoneApi.js
import { API_URL } from "../config/authConstants";
import { apiFetch } from "./apiClient";
import { ensureOk } from "./apiErrors";
import { solveChallenge } from "../utils/proofOfWork";
/**
* Номер автора объявления. В ленте номера нет: бэкенд отдаёт его только в
* обмен на решённую proof-of-work задачу (защита от массового сбора).
* Работает и без входа; с токеном лимиты считаются и на аккаунт.
*
* Бросает ApiError: 404 — номер скрыт или объявление закрыто,
* 429 — лимит (Retry-After), 400 — решение не принято.
*/
export async function revealPhone(postId, { signal } = {}) {
const base = `${API_URL}/api/v1/posts/${encodeURIComponent(postId)}/phone`;
const challengeRes = await ensureOk(await apiFetch(`${base}/challenge`, { signal }));
const task = await challengeRes.json();
const number = await solveChallenge(task, { signal });
const res = await ensureOk(await apiFetch(base, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ salt: task.salt, number, signature: task.signature }),
signal,
}));
const { phone } = await res.json();
return phone;
}