From 2b4ed3223344c70ae1b9e5cde780da2d0e1334a6 Mon Sep 17 00:00:00 2001 From: SeinopSys Date: Sat, 26 Mar 2022 00:17:37 +0100 Subject: [PATCH] convert request utils to typescript --- assets/js/utils/{requests.js => requests.ts} | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) rename assets/js/utils/{requests.js => requests.ts} (65%) diff --git a/assets/js/utils/requests.js b/assets/js/utils/requests.ts similarity index 65% rename from assets/js/utils/requests.js rename to assets/js/utils/requests.ts index aa7b80b2..e71a7662 100644 --- a/assets/js/utils/requests.js +++ b/assets/js/utils/requests.ts @@ -1,7 +1,9 @@ // Request Utils -export function fetchJson(verb, endpoint, body) { - const data = { +type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH'; + +export function fetchJson(verb: HttpMethod, endpoint: string, body?: Record): Promise { + const data: RequestInit = { method: verb, credentials: 'same-origin', headers: { @@ -19,7 +21,7 @@ export function fetchJson(verb, endpoint, body) { return fetch(endpoint, data); } -export function fetchHtml(endpoint) { +export function fetchHtml(endpoint: string): Promise { return fetch(endpoint, { credentials: 'same-origin', headers: { @@ -29,7 +31,7 @@ export function fetchHtml(endpoint) { }); } -export function handleError(response) { +export function handleError(response: Response): Response { if (!response.ok) { throw new Error('Received error from server'); }