import type { NitroFetchOptions, NitroFetchRequest } from "nitropack"; import { FetchError } from "ofetch"; /** * 封装 HTTP 请求 * @param url 请求的路径 * @param options 请求选项 * @returns 返回请求结果 * * @throws {FetchError} 请求失败时抛出错误 */ export const http = async ( url: string, options?: NitroFetchOptions ) => { const loginState = useLoginState(); const runtimeConfig = useRuntimeConfig(); const baseURL = runtimeConfig.public.baseURL as string; try { const data = await $fetch(url, { baseURL, headers: { "Content-Type": "application/json", Authorization: `Bearer ${loginState.token}`, }, ...options, }); return data; } catch (err: unknown) { if (err instanceof FetchError) { throw err; } else { throw new FetchError("请求失败"); } } };