From c0eebc9b5deb6521be256a02e94fb44512041b0f Mon Sep 17 00:00:00 2001 From: mrjvs Date: Wed, 12 Aug 2026 14:22:53 +0200 Subject: [PATCH] fix: fixed error handling on useHttpApi --- server/utils/httpApi.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/server/utils/httpApi.ts b/server/utils/httpApi.ts index 4e539ef..3e26c54 100644 --- a/server/utils/httpApi.ts +++ b/server/utils/httpApi.ts @@ -1,10 +1,15 @@ import { request } from 'undici'; import type { Dispatcher } from 'undici'; -export function useHttpApi(event: H3Event, token?: string) { +export type HttpApiOptions = { + headers?: HeadersInit; body?: string; method?: Dispatcher.HttpMethod; +}; +export type HttpApiFetch = (url: string, ops?: HttpApiOptions) => Promise; + +export function useHttpApi(event: H3Event, token?: string): HttpApiFetch { const config = useRuntimeConfig(event); - return (url: string, opts: { headers?: HeadersInit; body?: string; method?: Dispatcher.HttpMethod } = {}) => { + async function httpFetch(url: string, opts: HttpApiOptions = {}): Promise { const headers = new Headers(opts.headers); headers.set('Host', config.apiBaseHost); // Can't use fetch due to Host header overwriting if (token) { @@ -12,10 +17,24 @@ export function useHttpApi(event: H3Event, token?: string) { } const urlWithBase = new URL(url, config.apiBase); - return request(urlWithBase, { + const response = await request(urlWithBase, { method: opts.method, headers: Object.fromEntries(headers.entries()), body: opts?.body }); - }; + + if (response.statusCode >= 400) { + const err = new Error(`Request failed with ${response.statusCode}`); + try { + (err as any).data = await response.body.text(); + } catch { + // It's already errored, we don't need to know the body + } + throw err; + } + + return await response.body.json() as T; + } + + return httpFetch; }