import type { H3Event } from "h3"; import { getHeaders } from "h3"; import type { FetchOptions } from "ofetch"; /** Серверный хелпер для запросов к внешнему API. Проксирует заголовки клиента и разворачивает `response.data`. */ export class ServerApiHelper { /** * @param url - полный URL внешнего API * @param event - H3Event для проброса заголовков клиента * @param requestOptions - дополнительные опции fetch (method, body и т.д.) */ public static async fetch(url: string, event: H3Event, requestOptions?: FetchOptions): Promise { try { const response = await $fetch<{ data: T }>(url, { headers: getHeaders(event), ...requestOptions, }); return response.data; } catch (error: any) { throw new Error(`Cannot fetch ${url}: ${error?.data ?? error?.message ?? error}`, { cause: error }); } } }