| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { getApiKey, getSessionToken, getServerUrlSync } from './http-api-client'; |
|
|
| |
| const getServerUrl = (): string => getServerUrlSync(); |
| const DEFAULT_CACHE_MODE: RequestCache = 'no-store'; |
|
|
| export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; |
|
|
| export interface ApiFetchOptions extends Omit<RequestInit, 'method' | 'headers' | 'body'> { |
| |
| headers?: Record<string, string>; |
| |
| body?: unknown; |
| |
| skipAuth?: boolean; |
| } |
|
|
| |
| |
| |
| export function getAuthHeaders(additionalHeaders?: Record<string, string>): Record<string, string> { |
| const headers: Record<string, string> = { |
| 'Content-Type': 'application/json', |
| ...additionalHeaders, |
| }; |
|
|
| |
| const apiKey = getApiKey(); |
| if (apiKey) { |
| headers['X-API-Key'] = apiKey; |
| return headers; |
| } |
|
|
| |
| const sessionToken = getSessionToken(); |
| if (sessionToken) { |
| headers['X-Session-Token'] = sessionToken; |
| } |
|
|
| return headers; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function apiFetch( |
| endpoint: string, |
| method: HttpMethod = 'GET', |
| options: ApiFetchOptions = {} |
| ): Promise<Response> { |
| const { headers: additionalHeaders, body, skipAuth, cache, ...restOptions } = options; |
|
|
| const headers = skipAuth |
| ? { 'Content-Type': 'application/json', ...additionalHeaders } |
| : getAuthHeaders(additionalHeaders); |
|
|
| const fetchOptions: RequestInit = { |
| method, |
| headers, |
| credentials: 'include', |
| cache: cache ?? DEFAULT_CACHE_MODE, |
| ...restOptions, |
| }; |
|
|
| if (body !== undefined) { |
| fetchOptions.body = typeof body === 'string' ? body : JSON.stringify(body); |
| } |
|
|
| const url = endpoint.startsWith('http') ? endpoint : `${getServerUrl()}${endpoint}`; |
| return fetch(url, fetchOptions); |
| } |
|
|
| |
| |
| |
| export async function apiGet<T>( |
| endpoint: string, |
| options: Omit<ApiFetchOptions, 'body'> = {} |
| ): Promise<T> { |
| const response = await apiFetch(endpoint, 'GET', options); |
| return response.json(); |
| } |
|
|
| |
| |
| |
| export async function apiPost<T>( |
| endpoint: string, |
| body?: unknown, |
| options: ApiFetchOptions = {} |
| ): Promise<T> { |
| const response = await apiFetch(endpoint, 'POST', { ...options, body }); |
| return response.json(); |
| } |
|
|
| |
| |
| |
| export async function apiPut<T>( |
| endpoint: string, |
| body?: unknown, |
| options: ApiFetchOptions = {} |
| ): Promise<T> { |
| const response = await apiFetch(endpoint, 'PUT', { ...options, body }); |
| return response.json(); |
| } |
|
|
| |
| |
| |
| export async function apiDelete<T>(endpoint: string, options: ApiFetchOptions = {}): Promise<T> { |
| const response = await apiFetch(endpoint, 'DELETE', options); |
| return response.json(); |
| } |
|
|
| |
| |
| |
| export async function apiDeleteRaw( |
| endpoint: string, |
| options: ApiFetchOptions = {} |
| ): Promise<Response> { |
| return apiFetch(endpoint, 'DELETE', options); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function getAuthenticatedImageUrl( |
| path: string, |
| projectPath: string, |
| version?: string | number |
| ): string { |
| const serverUrl = getServerUrl(); |
| const params = new URLSearchParams({ |
| path, |
| projectPath, |
| }); |
|
|
| if (version !== undefined) { |
| params.set('v', String(version)); |
| } |
|
|
| |
| const apiKey = getApiKey(); |
| if (apiKey) { |
| params.set('apiKey', apiKey); |
| } |
|
|
| |
| |
| const sessionToken = getSessionToken(); |
| if (sessionToken) { |
| params.set('token', sessionToken); |
| } |
|
|
| return `${serverUrl}/api/fs/image?${params.toString()}`; |
| } |
|
|