125 lines
3.1 KiB
TypeScript
125 lines
3.1 KiB
TypeScript
declare global {
|
|
interface Window {
|
|
__APP_CONFIG__?: {
|
|
API_BASE_URL?: string;
|
|
THALOS_AUTH_BASE_URL?: string;
|
|
};
|
|
}
|
|
}
|
|
|
|
export type ApiErrorBody = {
|
|
code?: string;
|
|
message?: string;
|
|
correlationId?: string;
|
|
};
|
|
|
|
export class ApiError extends Error {
|
|
readonly status: number;
|
|
readonly code?: string;
|
|
readonly correlationId?: string;
|
|
|
|
constructor(status: number, message: string, code?: string, correlationId?: string) {
|
|
super(message);
|
|
this.name = 'ApiError';
|
|
this.status = status;
|
|
this.code = code;
|
|
this.correlationId = correlationId;
|
|
}
|
|
}
|
|
|
|
const CorrelationHeaderName = 'x-correlation-id';
|
|
|
|
export function getApiBaseUrl(): string {
|
|
const runtimeValue = window.__APP_CONFIG__?.API_BASE_URL;
|
|
if (runtimeValue && runtimeValue.length > 0) {
|
|
return runtimeValue;
|
|
}
|
|
|
|
return import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:8080';
|
|
}
|
|
|
|
export function getThalosAuthBaseUrl(): string {
|
|
const runtimeValue = window.__APP_CONFIG__?.THALOS_AUTH_BASE_URL;
|
|
if (runtimeValue && runtimeValue.length > 0) {
|
|
return runtimeValue;
|
|
}
|
|
|
|
return import.meta.env.VITE_THALOS_AUTH_BASE_URL ?? getApiBaseUrl();
|
|
}
|
|
|
|
export async function getJson<T>(path: string, baseUrl = getApiBaseUrl()): Promise<T> {
|
|
return requestJson<T>(baseUrl, path, { method: 'GET' });
|
|
}
|
|
|
|
export async function postJson<TResponse>(
|
|
path: string,
|
|
body: unknown,
|
|
baseUrl = getApiBaseUrl()
|
|
): Promise<TResponse> {
|
|
return requestJson<TResponse>(baseUrl, path, {
|
|
method: 'POST',
|
|
body: JSON.stringify(body)
|
|
});
|
|
}
|
|
|
|
export async function postNoContent(
|
|
path: string,
|
|
body: unknown,
|
|
baseUrl = getApiBaseUrl()
|
|
): Promise<void> {
|
|
await request(baseUrl, path, {
|
|
method: 'POST',
|
|
body: JSON.stringify(body)
|
|
});
|
|
}
|
|
|
|
async function requestJson<T>(baseUrl: string, path: string, init: RequestInit): Promise<T> {
|
|
const response = await request(baseUrl, path, init);
|
|
if (response.status === 204) {
|
|
return {} as T;
|
|
}
|
|
|
|
return (await response.json()) as T;
|
|
}
|
|
|
|
async function request(baseUrl: string, path: string, init: RequestInit): Promise<Response> {
|
|
const correlationId = createCorrelationId();
|
|
const headers = new Headers(init.headers ?? {});
|
|
headers.set('Accept', 'application/json');
|
|
if (init.body) {
|
|
headers.set('Content-Type', 'application/json');
|
|
}
|
|
|
|
headers.set(CorrelationHeaderName, correlationId);
|
|
|
|
const response = await fetch(`${baseUrl}${path}`, {
|
|
...init,
|
|
credentials: 'include',
|
|
headers
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const apiError = await parseApiError(response);
|
|
const message = apiError.message ?? `${init.method ?? 'GET'} ${path} failed with status ${response.status}`;
|
|
throw new ApiError(response.status, message, apiError.code, apiError.correlationId ?? correlationId);
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
async function parseApiError(response: Response): Promise<ApiErrorBody> {
|
|
try {
|
|
return (await response.json()) as ApiErrorBody;
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
function createCorrelationId(): string {
|
|
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
|
return crypto.randomUUID();
|
|
}
|
|
|
|
return `corr-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
}
|