| import { getAuthHeaders, getJsonHeaders } from './api-headers'; |
| import { base } from '$app/paths'; |
| import { ERROR_MESSAGES, HTTP_CODE_TO_STRING } from '$lib/constants/error'; |
| import { UrlProtocol } from '$lib/enums'; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| export class ApiError extends Error { |
| status: number; |
|
|
| constructor(message: string, status: number) { |
| super(message); |
| this.name = 'ApiError'; |
| this.status = status; |
| } |
| } |
|
|
| export interface ApiFetchOptions extends Omit<RequestInit, 'headers'> { |
| |
| |
| |
| |
| authOnly?: boolean; |
| |
| |
| |
| headers?: Record<string, string>; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function apiFetch<T>(path: string, options: ApiFetchOptions = {}): Promise<T> { |
| const { authOnly = false, headers: customHeaders, ...fetchOptions } = options; |
| const baseHeaders = authOnly ? getAuthHeaders() : getJsonHeaders(); |
| const headers = { ...baseHeaders, ...customHeaders }; |
| const url = |
| path.startsWith(UrlProtocol.HTTP) || path.startsWith(UrlProtocol.HTTPS) |
| ? path |
| : `${base}${path}`; |
|
|
| let response; |
|
|
| try { |
| response = await fetch(url, { |
| ...fetchOptions, |
| headers |
| }); |
| } catch (e) { |
| throw new Error(beautifyNetworkError(e)); |
| } |
|
|
| if (!response.ok) { |
| const errorMessage = await parseErrorMessage(response); |
|
|
| throw new ApiError(errorMessage, response.status); |
| } |
|
|
| return response.json() as Promise<T>; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function apiFetchWithParams<T>( |
| basePath: string, |
| params: Record<string, string>, |
| options: ApiFetchOptions = {} |
| ): Promise<T> { |
| const url = new URL(basePath, window.location.href); |
|
|
| for (const [key, value] of Object.entries(params)) { |
| if (value !== undefined && value !== null) { |
| url.searchParams.set(key, value); |
| } |
| } |
|
|
| const { authOnly = false, headers: customHeaders, ...fetchOptions } = options; |
| const baseHeaders = authOnly ? getAuthHeaders() : getJsonHeaders(); |
| const headers = { ...baseHeaders, ...customHeaders }; |
|
|
| let response; |
|
|
| try { |
| response = await fetch(url.toString(), { |
| ...fetchOptions, |
| headers |
| }); |
| } catch (e) { |
| throw new Error(beautifyNetworkError(e)); |
| } |
|
|
| if (!response.ok) { |
| const errorMessage = await parseErrorMessage(response); |
|
|
| throw new ApiError(errorMessage, response.status); |
| } |
|
|
| return response.json() as Promise<T>; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export async function apiPost<T, B = unknown>( |
| path: string, |
| body: B, |
| options: ApiFetchOptions = {} |
| ): Promise<T> { |
| return apiFetch<T>(path, { |
| body: JSON.stringify(body), |
| method: 'POST', |
| ...options |
| }); |
| } |
|
|
| |
| |
| |
| |
| async function parseErrorMessage(response: Response): Promise<string> { |
| try { |
| const errorData = await response.json(); |
|
|
| if (errorData?.error?.message) { |
| return errorData.error.message; |
| } |
|
|
| if (errorData?.error && typeof errorData.error === 'string') { |
| return errorData.error; |
| } |
|
|
| if (errorData?.message) { |
| return errorData.message; |
| } |
| } catch { |
| |
| } |
|
|
| const httpErrorStr = HTTP_CODE_TO_STRING[response.status]; |
|
|
| if (httpErrorStr) { |
| return httpErrorStr; |
| } |
|
|
| return `${ERROR_MESSAGES.HTTP.GENERIC}: ${response.status} ${response.statusText}`; |
| } |
|
|
| |
| |
| |
| |
| |
| function beautifyNetworkError(throwable: unknown): string { |
| let message; |
|
|
| if (throwable instanceof Error) { |
| message = throwable.message; |
|
|
| if (throwable.name === 'TypeError' && message.includes('fetch')) { |
| return ERROR_MESSAGES.NETWORK.UNREACHABLE; |
| } |
| } else { |
| message = String(throwable); |
| } |
|
|
| if (message.includes('ECONNREFUSED')) { |
| return ERROR_MESSAGES.NETWORK.REFUSED; |
| } else if (message.includes('ENOTFOUND')) { |
| return ERROR_MESSAGES.NETWORK.NXDOMAIN; |
| } else if (message.includes('ETIMEDOUT')) { |
| return ERROR_MESSAGES.NETWORK.TIMEOUT; |
| } |
|
|
| return `${ERROR_MESSAGES.NETWORK.GENERIC} (${message})`; |
| } |
|
|