From 8212868b92efa542221dce478027af6e39217a59 Mon Sep 17 00:00:00 2001 From: dominicbachmann Date: Wed, 6 Apr 2022 21:09:15 +0200 Subject: [PATCH] Added types for api --- src/lib/api.ts | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/lib/api.ts b/src/lib/api.ts index 42fe10a1e..85d4ccf26 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,9 +1,15 @@ -async function send({ method, path, data = {}, headers, timeout = 30000 }) { +async function send({ + method, + path, + data = {}, + headers, + timeout = 30000 +}): Promise> { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), timeout); const opts = { method, headers: {}, body: null, signal: controller.signal }; if (Object.keys(data).length > 0) { - let parsedData = data; + const parsedData = data; for (const [key, value] of Object.entries(data)) { if (value === '') { parsedData[key] = null; @@ -43,18 +49,33 @@ async function send({ method, path, data = {}, headers, timeout = 30000 }) { return responseData; } -export function get(path, headers = {}): Promise { +export function get( + path: string, + headers: Record +): Promise> { return send({ method: 'GET', path, headers }); } -export function del(path, data = {}, headers = {}): Promise { +export function del( + path: string, + data: Record, + headers: Record +): Promise> { return send({ method: 'DELETE', path, data, headers }); } -export function post(path, data, headers = {}): Promise { +export function post( + path: string, + data: Record, + headers: Record +): Promise> { return send({ method: 'POST', path, data, headers }); } -export function put(path, data, headers = {}): Promise { +export function put( + path: string, + data: Record, + headers: Record +): Promise> { return send({ method: 'PUT', path, data, headers }); }