123 lines
3.8 KiB
TypeScript
123 lines
3.8 KiB
TypeScript
import {apiUrl, getTokensFromCookies} from "../utils/common.ts";
|
|
import {ApiResponse} from "../types/api.ts";
|
|
|
|
|
|
export async function getFetch(path: string): Promise<ApiResponse> {
|
|
const finalTokens = getTokensFromCookies();
|
|
|
|
try {
|
|
const response = await fetch(apiUrl(path), {
|
|
method: 'GET',
|
|
headers: {
|
|
...(finalTokens?.access && { Authorization: `Bearer ${finalTokens.access}` }),
|
|
},
|
|
credentials: "include"
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const body = await response.json();
|
|
console.error('Error fetching:', 'Network response was not ok', body);
|
|
return { body, success: false };
|
|
}
|
|
|
|
return { body: await response.json(), success: true };
|
|
} catch (error) {
|
|
console.error('Error fetching:', error);
|
|
return { success: false };
|
|
}
|
|
}
|
|
|
|
export async function postFetch(
|
|
path: string,
|
|
body: object | FormData
|
|
): Promise<ApiResponse> {
|
|
const finalTokens = getTokensFromCookies();
|
|
const isFormData = body instanceof FormData;
|
|
|
|
try {
|
|
|
|
const headers = new Headers();
|
|
|
|
if (finalTokens?.access){
|
|
headers.append("Authorization", `Bearer ${finalTokens.access}`)
|
|
}
|
|
if (!isFormData){
|
|
headers.append('Content-Type','application/json')
|
|
}
|
|
const response = await fetch(apiUrl(path), {
|
|
method: 'POST',
|
|
headers: headers,
|
|
body: isFormData ? body as FormData : JSON.stringify(body),
|
|
credentials: 'include'
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const responseBody = await response.json();
|
|
console.error('Error posting:', 'Network response was not ok', responseBody);
|
|
return { body: responseBody, success: false };
|
|
}
|
|
|
|
return { body: await response.json(), success: true };
|
|
} catch (error) {
|
|
console.error('Error posting:', error);
|
|
return { success: false };
|
|
}
|
|
}
|
|
|
|
export async function patchFetch(
|
|
path: string,
|
|
body: object | FormData
|
|
): Promise<ApiResponse> {
|
|
const finalTokens = getTokensFromCookies();
|
|
const isFormData = body instanceof FormData;
|
|
|
|
try {
|
|
const response = await fetch(apiUrl(path), {
|
|
method: 'PATCH',
|
|
headers: {
|
|
...(finalTokens?.access && { Authorization: `Bearer ${finalTokens.access}` }),
|
|
...(!isFormData && { 'Content-Type': 'application/json' }),
|
|
},
|
|
body: isFormData ? body as FormData : JSON.stringify(body),
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const responseBody = await response.json();
|
|
console.error('Error patching:', 'Network response was not ok', responseBody);
|
|
return { body: responseBody, success: false };
|
|
}
|
|
|
|
return { body: await response.json(), success: true };
|
|
} catch (error) {
|
|
console.error('Error patching:', error);
|
|
return { success: false };
|
|
}
|
|
}
|
|
|
|
|
|
export async function deleteFetch(path: string): Promise<ApiResponse> {
|
|
const finalTokens = getTokensFromCookies();
|
|
|
|
try {
|
|
const response = await fetch(apiUrl(path), {
|
|
method: 'DELETE',
|
|
headers: {
|
|
...(finalTokens?.access && { Authorization: `Bearer ${finalTokens.access}` }),
|
|
},
|
|
credentials: 'include',
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const responseBody = await response.json();
|
|
console.error('Error deleting:', 'Network response was not ok', responseBody);
|
|
return { body: responseBody, success: false };
|
|
}
|
|
|
|
return { body: await response.json(), success: true };
|
|
} catch (error) {
|
|
console.error('Error deleting:', error);
|
|
return { success: false };
|
|
}
|
|
}
|