convert request utils to typescript

This commit is contained in:
SeinopSys 2022-03-26 00:17:37 +01:00 committed by Luna D
parent cccbcd041f
commit 2b4ed32233
No known key found for this signature in database
GPG key ID: 4B1C63448394F688

View file

@ -1,7 +1,9 @@
// Request Utils // Request Utils
export function fetchJson(verb, endpoint, body) { type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH';
const data = {
export function fetchJson(verb: HttpMethod, endpoint: string, body?: Record<string, unknown>): Promise<Response> {
const data: RequestInit = {
method: verb, method: verb,
credentials: 'same-origin', credentials: 'same-origin',
headers: { headers: {
@ -19,7 +21,7 @@ export function fetchJson(verb, endpoint, body) {
return fetch(endpoint, data); return fetch(endpoint, data);
} }
export function fetchHtml(endpoint) { export function fetchHtml(endpoint: string): Promise<Response> {
return fetch(endpoint, { return fetch(endpoint, {
credentials: 'same-origin', credentials: 'same-origin',
headers: { headers: {
@ -29,7 +31,7 @@ export function fetchHtml(endpoint) {
}); });
} }
export function handleError(response) { export function handleError(response: Response): Response {
if (!response.ok) { if (!response.ok) {
throw new Error('Received error from server'); throw new Error('Received error from server');
} }