// HTTP Client for MMS API
import type { ApiResponse, ApiError } from "./types.js";
const API_URL = process.env.MESS_API_URL || "https://mess.iiit.ac.in/api";
const API_KEY = process.env.MESS_API_KEY || "";
export class MessApiClient {
private baseUrl: string;
private apiKey: string;
constructor(apiKey?: string, baseUrl?: string) {
this.baseUrl = baseUrl || API_URL;
this.apiKey = apiKey || API_KEY;
}
private async request<T>(
endpoint: string,
options: RequestInit = {}
): Promise<T> {
const url = `${this.baseUrl}${endpoint}`;
const headers: Record<string, string> = {
"Authorization": this.apiKey,
"Content-Type": "application/json",
...((options.headers as Record<string, string>) || {}),
};
const response = await fetch(url, {
...options,
headers,
});
if (!response.ok) {
const errorBody = await response.json().catch(() => ({})) as ApiError;
throw new Error(
errorBody.error?.message || `API Error: ${response.status} ${response.statusText}`
);
}
// Handle 204 No Content
if (response.status === 204) {
return {} as T;
}
return response.json() as Promise<T>;
}
async get<T>(endpoint: string, params?: Record<string, string>): Promise<T> {
let url = endpoint;
if (params) {
const searchParams = new URLSearchParams(
Object.entries(params).filter(([_, v]) => v !== undefined && v !== "")
);
if (searchParams.toString()) {
url += `?${searchParams.toString()}`;
}
}
return this.request<T>(url, { method: "GET" });
}
async post<T>(endpoint: string, body?: unknown): Promise<T> {
return this.request<T>(endpoint, {
method: "POST",
body: body ? JSON.stringify(body) : undefined,
});
}
async put<T>(endpoint: string, body?: unknown): Promise<T> {
return this.request<T>(endpoint, {
method: "PUT",
body: body ? JSON.stringify(body) : undefined,
});
}
async delete<T>(endpoint: string, params?: Record<string, string>): Promise<T> {
let url = endpoint;
if (params) {
const searchParams = new URLSearchParams(
Object.entries(params).filter(([_, v]) => v !== undefined && v !== "")
);
if (searchParams.toString()) {
url += `?${searchParams.toString()}`;
}
}
return this.request<T>(url, { method: "DELETE" });
}
}
// Singleton instance
export const client = new MessApiClient();