Files
edbridge-scholars/src/utils/api.ts
2026-01-28 15:22:19 +06:00

188 lines
4.1 KiB
TypeScript

import type {
SessionAnswerResponse,
SessionQuestionsResponse,
SessionRequest,
SessionResponse,
SubmitAnswer,
} from "../types/session";
import type { PracticeSheet } from "../types/sheet";
const API_URL = "https://ed-dev-api.omukk.dev";
export interface LoginRequest {
email: string;
password: string;
}
export interface User {
email: string;
name: string;
role: "STUDENT" | "TEACHER" | "ADMIN";
avatar_url: string;
id: string;
status: "ACTIVE" | "INACTIVE";
joined_at: string;
last_active: string;
}
export interface LoginResponse {
token: string;
token_type: string;
user: User;
}
export interface ApiError {
detail?: string;
message?: string;
}
class ApiClient {
private baseURL: string;
constructor(baseURL: string) {
this.baseURL = baseURL;
}
private async request<T>(
endpoint: string,
options: RequestInit = {},
): Promise<T> {
const url = `${this.baseURL}${endpoint}`;
const config: RequestInit = {
...options,
headers: {
"Content-Type": "application/json",
...options.headers,
},
};
try {
const response = await fetch(url, config);
if (!response.ok) {
const error: ApiError = await response.json().catch(() => ({
message: "An error occurred",
}));
throw new Error(error.detail || error.message || "Request failed");
}
return await response.json();
} catch (error) {
if (error instanceof Error) {
throw error;
}
throw new Error("Network error occurred");
}
}
// Auth endpoints
async login(credentials: LoginRequest): Promise<LoginResponse> {
return this.request<LoginResponse>("/auth/login/", {
method: "POST",
body: JSON.stringify(credentials),
});
}
// Authenticated request helper
async authenticatedRequest<T>(
endpoint: string,
token: string,
options: RequestInit = {},
): Promise<T> {
return this.request<T>(endpoint, {
...options,
headers: {
...options.headers,
Authorization: `Bearer ${token}`,
},
});
}
// Example: Get user profile (authenticated endpoint)
async getUserProfile(token: string): Promise<User> {
return this.authenticatedRequest<User>("/auth/me/", token);
}
async getPracticeSheets(
token: string,
page: number,
limit: number,
): Promise<any> {
const queryParams = new URLSearchParams({
page: page.toString(),
limit: limit.toString(),
}).toString();
return this.authenticatedRequest<any>(
`/practice-sheets/?${queryParams}`,
token,
);
}
async getPracticeSheetById(
token: string,
sheetId: string,
): Promise<PracticeSheet> {
return this.authenticatedRequest<PracticeSheet>(
`/practice-sheets/${sheetId}`,
token,
);
}
async startSession(
token: string,
sessionData: SessionRequest,
): Promise<SessionResponse> {
return this.authenticatedRequest<SessionResponse>(`/sessions/`, token, {
method: "POST",
body: JSON.stringify(sessionData),
});
}
async fetchSessionQuestions(
token: string,
sessionId: string,
): Promise<SessionQuestionsResponse> {
return this.authenticatedRequest<SessionQuestionsResponse>(
`/sessions/${sessionId}/questions/`,
token,
);
}
async submitAnswer(
token: string,
sessionId: string,
answerSubmissionData: SubmitAnswer,
): Promise<SessionAnswerResponse> {
return this.authenticatedRequest<SessionAnswerResponse>(
`/sessions/${sessionId}/answer/`,
token,
{
method: "POST",
body: JSON.stringify(answerSubmissionData),
},
);
}
async fetchNextModule(token: string, sessionId: string): Promise<any> {
return this.authenticatedRequest<any>(
`/sessions/${sessionId}/next-module/`,
token,
{
method: "POST",
},
);
}
async fetchSessionStateById(
token: string,
sessionId: string,
): Promise<SessionResponse> {
return this.authenticatedRequest<SessionResponse>(
`/sessions/${sessionId}`,
token,
);
}
}
export const api = new ApiClient(API_URL);