Refactor APIs

This commit is contained in:
2025-11-18 22:02:44 +03:00
Unverified
parent e23959fbd9
commit a15d3a08bf
35 changed files with 1075 additions and 313 deletions
+50
View File
@@ -0,0 +1,50 @@
import { API_BASE_URL } from "@/core/config";
import { getAuthHeaders } from "./account";
export interface PushSubscriptionRequest {
endpoint: string;
keys: {
p256dh: string;
auth: string;
};
}
export interface PushSubscriptionResponse {
status: string;
message: string;
}
/**
* Subscribes the current user to push notifications
*/
export async function subscribeToPush(
subscription: PushSubscriptionRequest,
token: string
): Promise<PushSubscriptionResponse> {
const res = await fetch(`${API_BASE_URL}/push/subscribe`, {
method: "POST",
headers: getAuthHeaders(token, true),
body: JSON.stringify(subscription)
});
if (!res.ok) {
const error = await res.json().catch(() => ({ detail: "Failed to subscribe to push notifications" }));
throw new Error(error.detail || "Failed to subscribe to push notifications");
}
return await res.json();
}
/**
* Unsubscribes the current user from push notifications
*/
export async function unsubscribeFromPush(token: string): Promise<PushSubscriptionResponse> {
const res = await fetch(`${API_BASE_URL}/push/unsubscribe`, {
method: "DELETE",
headers: getAuthHeaders(token, true)
});
if (!res.ok) {
const error = await res.json().catch(() => ({ detail: "Failed to unsubscribe from push notifications" }));
throw new Error(error.detail || "Failed to unsubscribe from push notifications");
}
return await res.json();
}