nidus-sync/ts/client.ts

97 lines
2.1 KiB
TypeScript
Raw Normal View History

// src/api/axios.ts
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
2026-03-23 14:39:57 -07:00
2026-04-23 15:24:06 +00:00
export interface AxiosErrorJSON {
status: number;
}
class ApiClient {
private client: AxiosInstance;
constructor() {
this.client = axios.create({
2026-04-23 15:24:06 +00:00
timeout: 10000,
withCredentials: true,
});
// Request interceptor for auth headers, content-type, etc.
this.client.interceptors.request.use((config) => {
// Content-type negotiation
config.headers["Accept"] = "application/json";
config.headers["X-Requested-With"] = "nidus-web 0.1";
// Add auth token if logged in
const token = localStorage.getItem("authToken");
if (token) {
config.headers["Authorization"] = `Bearer ${token}`;
}
return config;
});
// Response interceptor for handling auth errors
this.client.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
// Could emit event or redirect here
}
return Promise.reject(error);
},
);
}
async JSONGet<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
const response = await this.client.get<T>(url, {
...config,
headers: {
Accept: "application/json",
...config?.headers,
},
});
return response.data;
}
async JSONPost<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig,
): Promise<T> {
const response = await this.client.post<T>(url, data, {
...config,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
...config?.headers,
},
});
return response.data;
}
async JSONPut<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig,
): Promise<T> {
const response = await this.client.put<T>(url, data, {
...config,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
...config?.headers,
},
});
return response.data;
}
async JSONDelete<T = any>(
url: string,
config?: AxiosRequestConfig,
): Promise<T> {
const response = await this.client.delete<T>(url, config);
return response.data;
}
}
// Single instance export - this IS the singleton
export const apiClient = new ApiClient();