Default Types
By default, the response data will always be type of unknown
. If you want to customize the default type you can pass the defaultOutput
option to the createFetch
function.
This only serves as a type for the response data it's not used as a validation schema.
import { createFetch } from "@better-fetch/fetch" ;
import { z } from "zod" ;
const $fetch = createFetch ({
baseURL : "https://jsonplaceholder.typicode.com" ,
defaultOutput : z . any (),
})
const { data , error } = await $fetch ( "/todos/1" )
Hover over the data object to see the type
If you define output schema, the default output type will be ignored.
import { createFetch } from "@better-fetch/fetch" ;
import { z } from "zod" ;
const $fetch = createFetch ({
baseURL : "https://jsonplaceholder.typicode.com" ,
defaultOutput : z . any (),
});
const { data , error } = await $fetch ( "/todos/1" , {
output : z . object ({
userId : z . string (),
id : z . number (),
title : z . string (),
completed : z . boolean (),
}),
})
Hover over the data object to see the type
The default error type is:
{ status : number, statusText : string, message ?: string }.
if you want custom default error type, you can pass a defaultError
option to the createFetch
function.
The status
and statusText
properties are always defined. Your custom error definitions are only
inferred if the API returns a JSON error object.
import { createFetch } from "@better-fetch/fetch" ;
import { z } from "zod" ;
const $fetch = createFetch ({
baseURL : "https://jsonplaceholder.typicode.com" ,
defaultError : z . object ({
message : z . string (). optional (),
error : z . string (),
}),
})
const { data , error } = await $fetch ( "/todos/1" )
Hover over the error object to see the type