import { createUseFetch } from '#imports' import type { NitroFetchRequest } from 'nitropack/types' import type { $Fetch } from 'ofetch' /** 与 `R.success` / `R.error` 返回结构对齐 */ export type ApiResponse = { code: number message: string data: T } /** 从 Nitro 推断的响应体上剥离一层 `ApiResponse`,得到 `data` 字段类型 */ export type UnwrapApiResponse = T extends ApiResponse ? D : T export function unwrapApiBody(payload: ApiResponse): T { if (payload.code !== 0) { throw new Error(payload.message) } return payload.data } export const request = import.meta.server ? undefined : $fetch.create({ credentials: 'include', }) const httpFetchDefaults = { retry: 0, $fetch: request, transform: unwrapApiBody, } export const _useHttpFetch = createUseFetch(httpFetchDefaults) export const _useLazyHttpFetch = createUseFetch({ ...httpFetchDefaults, lazy: true, })