File size: 2,529 Bytes
151ec26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios from "axios";

const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000/api/v1";

export const api = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    "Content-Type": "application/json",
  },
});

// Attach JWT access token to all outgoing requests
api.interceptors.request.use((config) => {
  if (typeof window !== "undefined") {
    const token = localStorage.getItem("sanjeevani_token");
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
  }
  return config;
});

// Response interceptor for handling 401s
api.interceptors.response.use(
  (response) => response,
  (error) => {
    if (error.response && error.response.status === 401) {
      if (typeof window !== "undefined" && !window.location.pathname.includes("/login")) {
        // Optional session expired redirect
      }
    }
    return Promise.reject(error);
  }
);

// Typed API services
export const authApi = {
  login: (data: any) => api.post("/auth/login", data),
  register: (data: any) => api.post("/auth/register", data),
  getMe: () => api.get("/auth/me"),
  refresh: (refresh_token: string) => api.post("/auth/refresh", { refresh_token }),
};

export const nerApi = {
  analyze: (text: string) => api.post("/ner/analyze", { text }),
  getModelInfo: () => api.get("/ner/model-info"),
};

export const documentApi = {
  upload: (formData: FormData) =>
    api.post("/documents/upload", formData, {
      headers: { "Content-Type": "multipart/form-data" },
    }),
  list: () => api.get("/documents"),
  getById: (id: string) => api.get(`/documents/${id}`),
  delete: (id: string) => api.delete(`/documents/${id}`),
};

export const chatApi = {
  sendMessage: (message: string, conversation_id?: string) =>
    api.post("/chat/message", { message, conversation_id }),
  listConversations: () => api.get("/chat/conversations"),
  getConversation: (id: string) => api.get(`/chat/conversations/${id}`),
  deleteConversation: (id: string) => api.delete(`/chat/conversations/${id}`),
};

export const profileApi = {
  getProfile: () => api.get("/profile"),
  updateProfile: (data: any) => api.put("/profile", data),
};

export const historyApi = {
  getHistory: (limit: number = 50) => api.get(`/history?limit=${limit}`),
};

export const adminApi = {
  getStats: () => api.get("/admin/stats"),
  getAuditLogs: (limit: number = 100) => api.get(`/admin/audit-logs?limit=${limit}`),
};

export const healthApi = {
  getHealth: () => api.get("/health"),
  getReady: () => api.get("/ready"),
};