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.
34 lines
823 B
34 lines
823 B
import { createUseFetch } from '#imports'
|
|
|
|
/** 与 `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 = $fetch.create({})
|
|
|
|
const httpFetchDefaults = {
|
|
retry: 0,
|
|
$fetch: request,
|
|
transform: unwrapApiBody,
|
|
}
|
|
|
|
export const _useHttpFetch = createUseFetch(httpFetchDefaults)
|
|
export const _useLazyHttpFetch = createUseFetch({
|
|
...httpFetchDefaults,
|
|
lazy: true,
|
|
})
|