Modern React Template - v0.0.10
    Preparing search index...

    Function fetchData

    • Validated Fetch Utility

      Makes an HTTP request and validates the response against a Zod schema. Automatically handles JSON serialization and error responses.

      Type Parameters

      • T

        Expected response type (should match schema if provided)

      Parameters

      • url: string

        API endpoint URL (relative or absolute)

      • Optionaloptions: FetchOptions

        Fetch options including optional Zod schema

      Returns Promise<T>

      Promise resolving to validated response data

      If HTTP request fails (non-2xx status)

      If response validation fails (when schema provided)

      For network errors or other fetch failures

      // 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' }
      });