ui: Linting & Formatting scripts (#26819)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { getAuthHeaders, getJsonHeaders } from './api-headers';
|
||||
import { base } from '$app/paths';
|
||||
import { getJsonHeaders, getAuthHeaders } from './api-headers';
|
||||
import { UrlProtocol } from '$lib/enums';
|
||||
import { ERROR_MESSAGES, HTTP_CODE_TO_STRING } from '$lib/constants/error';
|
||||
import { UrlProtocol } from '$lib/enums';
|
||||
|
||||
/**
|
||||
* API Fetch Utilities
|
||||
@@ -61,16 +61,15 @@ export interface ApiFetchOptions extends Omit<RequestInit, 'headers'> {
|
||||
*/
|
||||
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,
|
||||
@@ -82,6 +81,7 @@ export async function apiFetch<T>(path: string, options: ApiFetchOptions = {}):
|
||||
|
||||
if (!response.ok) {
|
||||
const errorMessage = await parseErrorMessage(response);
|
||||
|
||||
throw new ApiError(errorMessage, response.status);
|
||||
}
|
||||
|
||||
@@ -118,11 +118,11 @@ export async function apiFetchWithParams<T>(
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -134,6 +134,7 @@ export async function apiFetchWithParams<T>(
|
||||
|
||||
if (!response.ok) {
|
||||
const errorMessage = await parseErrorMessage(response);
|
||||
|
||||
throw new ApiError(errorMessage, response.status);
|
||||
}
|
||||
|
||||
@@ -154,8 +155,8 @@ export async function apiPost<T, B = unknown>(
|
||||
options: ApiFetchOptions = {}
|
||||
): Promise<T> {
|
||||
return apiFetch<T>(path, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
method: 'POST',
|
||||
...options
|
||||
});
|
||||
}
|
||||
@@ -167,12 +168,15 @@ export async function apiPost<T, B = unknown>(
|
||||
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;
|
||||
}
|
||||
@@ -181,6 +185,7 @@ async function parseErrorMessage(response: Response): Promise<string> {
|
||||
}
|
||||
|
||||
const httpErrorStr = HTTP_CODE_TO_STRING[response.status];
|
||||
|
||||
if (httpErrorStr) {
|
||||
return httpErrorStr;
|
||||
}
|
||||
@@ -195,8 +200,10 @@ async function parseErrorMessage(response: Response): Promise<string> {
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user