Expected response type (should match schema if provided)
API endpoint URL (relative or absolute)
Optionaloptions: FetchOptionsFetch options including optional Zod schema
Promise resolving to validated response data
// Without validation
const data = await fetchData<User>('/api/user/123');
// With validation
const data = await fetchData<User>('/api/user/123', {
schema: userSchema
});
// With custom headers
const data = await fetchData<User>('/api/user/123', {
schema: userSchema,
headers: { 'Authorization': 'Bearer token' }
});
Validated Fetch Utility
Makes an HTTP request and validates the response against a Zod schema. Automatically handles JSON serialization and error responses.