File size: 2,813 Bytes
18b71c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:4000/api";

let authToken: string | null = null;

export const setApiAuthToken = (token: string | null) => {
  authToken = token;
};

type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";

interface RequestOptions {
  method?: HttpMethod;
  body?: unknown;
  token?: string | null;
}

export interface ApiEnvelope<T> {
  success: boolean;
  data: T;
  message?: string;
}

export const apiRequest = async <T>(path: string, options: RequestOptions = {}) => {
  const response = await fetch(`${API_BASE_URL}${path}`, {
    method: options.method ?? "GET",
    headers: {
      "Content-Type": "application/json",
      ...(options.token || authToken
        ? { Authorization: `Bearer ${options.token ?? authToken}` }
        : {}),
    },
    body: options.body ? JSON.stringify(options.body) : undefined,
  });

  const payload = (await response.json()) as ApiEnvelope<T> | { success: false; message: string };

  if (!response.ok || !("success" in payload) || !payload.success) {
    const message = "message" in payload ? payload.message : "Request failed.";
    throw new Error(message);
  }

  return payload.data;
};

export const api = {
  signup: (body: { name: string; email: string; password: string }) =>
    apiRequest<{ token: string; user: unknown }>("/auth/signup", {
      method: "POST",
      body,
    }),
  login: (body: { email: string; password: string }) =>
    apiRequest<{ token: string; user: unknown }>("/auth/login", {
      method: "POST",
      body,
    }),
  getMe: () => apiRequest<unknown>("/auth/me"),
  getProfile: () => apiRequest<unknown>("/profile/me"),
  saveProfile: (body: {
    age: number;
    educationLevel: string;
    stream: string;
    interests: string[];
    careerIntent: string;
  }) =>
    apiRequest<unknown>("/profile/me", {
      method: "PUT",
      body,
    }),
  getQuizQuestions: () => apiRequest<unknown[]>("/quiz/questions"),
  submitQuiz: (body: { answers: Array<{ questionId: string; optionId: string }> }) =>
    apiRequest<unknown>("/quiz/submit", {
      method: "POST",
      body,
    }),
  getCurrentChat: () => apiRequest<unknown>("/chat/session/current"),
  startChat: () =>
    apiRequest<unknown>("/chat/session", {
      method: "POST",
    }),
  sendChatMessage: (message: string) =>
    apiRequest<unknown>("/chat/session/current/messages", {
      method: "POST",
      body: { message },
    }),
  generateRecommendations: () =>
    apiRequest<unknown>("/recommendations/generate", {
      method: "POST",
    }),
  getLatestRecommendations: () => apiRequest<unknown>("/recommendations/latest"),
  getRoadmap: (careerId: string) => apiRequest<unknown>(`/roadmaps/${careerId}`),
  getDashboard: () => apiRequest<unknown>("/dashboard/overview"),
};