diff --git a/composables/api.ts b/composables/api.ts index a6f6882..f7cc281 100644 --- a/composables/api.ts +++ b/composables/api.ts @@ -1,7 +1,13 @@ -import type { AsyncData } from 'nuxt/app' import type { FetchError } from 'ofetch' -export async function useApi( +const apiEndpoint = + 'https://' + + (process.dev + ? useRuntimeConfig().public.apiIp || window.location.hostname + : window.location.hostname) + + '/yunohost/portalapi' + +export function useApi( path: string, { method = 'GET', @@ -10,30 +16,34 @@ export async function useApi( method?: 'GET' | 'POST' | 'PUT' | 'DELETE' body?: Record } = {}, -): Promise> { - const host = window.location.hostname - const apiEndpoint = - 'https://' + - (process.dev ? useRuntimeConfig().public.apiIp || host : host) + - '/yunohost/portalapi' - - const result = await useFetch(apiEndpoint + path, { - headers: { - 'Content-Type': 'application/json', - 'X-Requested-With': '', - }, - method, - credentials: 'include', - body, - }) - - const error = result.error.value - if (error && error.statusCode === 401) { - useIsLoggedIn().value = false - navigateTo('/login') +) { + type Resp = { + data: Ref + error: Ref + } + const result: Resp = { + data: ref(null), + error: ref(null), } - return result as AsyncData + const query = () => { + return $fetch(apiEndpoint + path, { + method, + credentials: 'include', + body, + }) + .then((data) => { + result.data.value = data as T + }) + .catch((e: FetchError) => { + result.error.value = e + if (e.statusCode === 401) { + useIsLoggedIn().value = false + navigateTo('/login') + } + }) + } + return Promise.resolve(query()).then(() => result) } export interface UserData {