File size: 2,152 Bytes
f91a684 3bda374 f91a684 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | /**
* 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 ?? '';
}
|