Files
Aislo/A07_Register/A07_Register_Api_Fetch.ts
T

48 lines
1.5 KiB
TypeScript

import { API_BASE_URL } from "@config/config_frontend";
export interface RegisterPayload {
email: string;
password: string;
password_confirm: string;
name: string;
position: string | null;
phone: string | null;
terms_version: string;
terms_agreed: boolean;
privacy_agreed: boolean;
marketing_agreed: boolean;
}
async function request(path: string, body: object): Promise<Record<string, unknown>> {
const response = await fetch(`${API_BASE_URL}${path}`, {
method: "POST",
credentials: "include",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
const result = (await response.json()) as Record<string, unknown>;
if (!response.ok) throw new Error(String(result.detail ?? "Request failed"));
return result;
}
export function requestRegistration(payload: RegisterPayload): Promise<Record<string, unknown>> {
return request("/auth/register/request", payload);
}
export function verifyRegistration(
email: string,
otpCode: string,
): Promise<Record<string, unknown>> {
return request("/auth/register/verify", { email, otp_code: otpCode });
}
export async function searchCompanies(query: string): Promise<{ id: number; name: string }[]> {
const response = await fetch(`${API_BASE_URL}/auth/companies?q=${encodeURIComponent(query)}`);
const result = (await response.json()) as {
detail?: string;
companies?: { id: number; name: string }[];
};
if (!response.ok) throw new Error(result.detail ?? "Request failed");
return result.companies ?? [];
}