From 8aa9854fa9cf9ecc60f7e623d91e209c3134b97d Mon Sep 17 00:00:00 2001 From: _aandres Date: Mon, 26 Dec 2022 00:11:56 +0100 Subject: [PATCH] feat [front]: cleanup api helper --- front/composables/api.ts | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/front/composables/api.ts b/front/composables/api.ts index 8bdf259..ad2f263 100644 --- a/front/composables/api.ts +++ b/front/composables/api.ts @@ -1,6 +1,6 @@ import { useLoadingStore } from "~/stores/loadingStore" -type MyHeaders = { [key: string]: string } +type MyHeaders = Record const makeLoadingKey = (path: string) => { // camel-case the path : auth/login -> authLogin @@ -19,7 +19,7 @@ const makeLoadingKey = (path: string) => { } const getCsrfCookie = () => { - let cookie: string + let cookie: string | undefined if (process.server) { cookie = useRequestHeaders(["cookie"])["cookie"] } else { @@ -36,6 +36,7 @@ const getCsrfCookie = () => { } const getHeaders = (includeCsrf = false): MyHeaders => { + // @ts-expect-error TODO handle null values here const headers: MyHeaders = useRequestHeaders(["cookie"]) if (includeCsrf) { const csfrToken = getCsrfCookie() @@ -64,30 +65,24 @@ const baseUrl = `${host}/api/` export async function apiRequest( method: string, path: string, - payload, + payload: Record | undefined, params = {} ) { const loadingStore = useLoadingStore() const key = makeLoadingKey(path) loadingStore.markLoading(key) - const response = await useAsyncData( - key, - () => - $fetch(baseUrl + path, { - method: method, - body: payload, - credentials: "include", - headers: getHeaders(method != "GET"), - params: params, - }), - { initialCache: false } + const response = await useAsyncData(key, () => + $fetch(baseUrl + path, { + method: method, + body: payload, + credentials: "include", + headers: getHeaders(method != "GET"), + params: params, + }) ) - if (response.error.value) { - loadingStore.markError(key) - } else { - loadingStore.markDone(key) - } + if (response.error.value) loadingStore.markError(key) + else loadingStore.markDone(key) return response }