You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

38 lines
967 B

import { createUseFetch } from '#imports'
import type { NitroFetchRequest } from 'nitropack/types'
import type { $Fetch } from 'ofetch'
/** 与 `R.success` / `R.error` 返回结构对齐 */
export type ApiResponse<T = unknown> = {
code: number
message: string
data: T
}
/** 从 Nitro 推断的响应体上剥离一层 `ApiResponse`,得到 `data` 字段类型 */
export type UnwrapApiResponse<T> = T extends ApiResponse<infer D> ? D : T
export function unwrapApiBody<T>(payload: ApiResponse<T>): 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,
})