RYP / src /lib /aptitudeClient.ts
Soumya79's picture
Upload 1361 files
3bda374 verified
/**
* aptitudeClient.ts
* ------------------
* Fetches Aptitude topic data from the Go backend which
* reads it from the MongoDB collections dynamically.
*/
const API_BASE = '';
export interface AptitudeTopic {
id: string;
chapter_no: number;
chapter_name: string;
}
export interface AptitudeTopicContent extends AptitudeTopic {
content: string;
}
/**
* Fetches all topics for a given aptitude category.
*/
export async function fetchAptitudeTopics(category: string): Promise<AptitudeTopic[]> {
const res = await fetch(`${API_BASE}/api/aptitude/topics?category=${category}`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});
if (!res.ok) {
throw new Error(`API error ${res.status}: ${res.statusText}`);
}
const data = await res.json() as { topics: AptitudeTopic[] };
return data.topics ?? [];
}
/**
* Fetches full content for a specific aptitude topic in a category.
*/
export async function fetchAptitudeTopicContent(category: string, topicNo: number): Promise<AptitudeTopicContent> {
const res = await fetch(`${API_BASE}/api/aptitude/topics/${topicNo}?category=${category}`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});
if (!res.ok) {
throw new Error(`API error ${res.status}: ${res.statusText}`);
}
return await res.json() as AptitudeTopicContent;
}
export interface ExplainRequest {
question: string;
options: string;
answer: string;
topic: string;
}
/**
* Calls the AI backend to generate a structured step-by-step explanation
* for an aptitude question whose explanation is missing from the database.
*/
export async function generateAptitudeExplanation(req: ExplainRequest): Promise<string> {
const res = await fetch(`${API_BASE}/api/ai/explain`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(req),
});
if (!res.ok) {
const err = await res.json().catch(() => ({})) as { error?: string };
throw new Error(err?.error ?? `API error ${res.status}`);
}
const data = await res.json() as { explanation: string };
return data.explanation ?? '';
}