46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
![]() |
import { makeRequest } from "@frontend/kitui";
|
||
|
import { isAxiosError } from "axios";
|
||
|
|
||
|
const apiUrl = process.env.REACT_APP_DOMAIN + "/codeword/promocode";
|
||
|
|
||
|
export async function activatePromocode(promocode: string) {
|
||
|
try {
|
||
|
const response = await makeRequest<
|
||
|
| {
|
||
|
codeword: string;
|
||
|
}
|
||
|
| {
|
||
|
fastLink: string;
|
||
|
},
|
||
|
{
|
||
|
greetings: string;
|
||
|
}
|
||
|
>({
|
||
|
url: apiUrl + "/activate",
|
||
|
method: "POST",
|
||
|
contentType: true,
|
||
|
body: {
|
||
|
codeword: promocode,
|
||
|
},
|
||
|
});
|
||
|
|
||
|
return response.greetings;
|
||
|
} catch (error) {
|
||
|
let message = "Неизвестная ошибка";
|
||
|
if (isAxiosError(error)) {
|
||
|
const backendErrorMessage = error.response?.data.error;
|
||
|
|
||
|
if (error.response?.status === 404) {
|
||
|
message = "Промокод не найден";
|
||
|
} else if (
|
||
|
typeof backendErrorMessage === "string" &&
|
||
|
backendErrorMessage.includes("promo code is expired")
|
||
|
) {
|
||
|
message = "Промокод истек";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
throw new Error(message);
|
||
|
}
|
||
|
}
|