mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 20:32:20 +03:00
Move chat pipeline validation, circuit breaker execution, proxy resolution, logging, and session header handling into dedicated helpers to keep the SSE handler smaller and easier to verify. Also fix shared API option precedence, rebuild skill version caches after deletions, ignore api.trycloudflare.com false positives, and add rate-limit manager test flush/reset hooks for deterministic coverage. Expand integration and unit coverage across chat routing, auth, cloud sync, skills, executors, streaming, DB helpers, proxy handling, and provider/model utilities.
66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
/**
|
|
* API utility functions for making HTTP requests
|
|
*/
|
|
|
|
const DEFAULT_HEADERS: Record<string, string> = {
|
|
"Content-Type": "application/json",
|
|
};
|
|
|
|
interface ApiOptions extends RequestInit {
|
|
headers?: Record<string, string>;
|
|
}
|
|
|
|
export async function get(url: string, options: ApiOptions = {}) {
|
|
const response = await fetch(url, {
|
|
...options,
|
|
method: "GET",
|
|
headers: { ...DEFAULT_HEADERS, ...options.headers },
|
|
});
|
|
return handleResponse(response);
|
|
}
|
|
|
|
export async function post(url: string, data: unknown, options: ApiOptions = {}) {
|
|
const response = await fetch(url, {
|
|
...options,
|
|
method: "POST",
|
|
headers: { ...DEFAULT_HEADERS, ...options.headers },
|
|
body: JSON.stringify(data),
|
|
});
|
|
return handleResponse(response);
|
|
}
|
|
|
|
export async function put(url: string, data: unknown, options: ApiOptions = {}) {
|
|
const response = await fetch(url, {
|
|
...options,
|
|
method: "PUT",
|
|
headers: { ...DEFAULT_HEADERS, ...options.headers },
|
|
body: JSON.stringify(data),
|
|
});
|
|
return handleResponse(response);
|
|
}
|
|
|
|
export async function del(url: string, options: ApiOptions = {}) {
|
|
const response = await fetch(url, {
|
|
...options,
|
|
method: "DELETE",
|
|
headers: { ...DEFAULT_HEADERS, ...options.headers },
|
|
});
|
|
return handleResponse(response);
|
|
}
|
|
|
|
async function handleResponse(response: Response) {
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
const error: any = new Error(data.error || "An error occurred");
|
|
error.status = response.status;
|
|
error.data = data;
|
|
throw error;
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
const api = { get, post, put, del };
|
|
export default api;
|