OptionalbaseOptionalbodybody to be passed to fetch()
Optionalextraany additional options to be passed to fetch()
Optionalheadersheaders to be passed to fetch()
Optionaljsonshorthand for sending json body.
mutually exclusive with body
Optionalmapoptional function to map the error
useful when your API has an app-level error format, and you want to throw a custom error
the error to be thrown instead
const api = createFfetch({
...,
mapError: (err) => {
if (err.response.headers.get('Content-Type') !== 'application/json') return err
try {
// note: .bodyText is only available if `readBodyOnError` is true
const json = JSON.parse(err.bodyText)
throw new ApiError(json.errorCode, json.message)
} catch {
return err
}
}
Optionalmethodhttp method
Optionalmiddlewaresmiddlewares for the requests
Optionalreadwhether to read the body of the response on HttpError (i.e. when validateResponse returns false).
useful for debugging, but may be undesirable in some cases.
Optionalvalidatewhether to throw HttpError on non-2xx responses
when a function is provided, it will be called with the response and should return whether the response is valid. if it returns false, the response will be thrown as an HttpError
base url to be prepended to the url
this base url is always treated as a "base path", i.e. the path passed to
ffetch()will always be appended to it (unlike thenew URL(), which has ambiguous slash semantics)