@happy-ts/fetch-t
    Preparing search index...

    Interface FetchInit

    Extended fetch options that add additional capabilities to the standard RequestInit.

    1.0.0

    import { fetchT, type FetchInit } from '@happy-ts/fetch-t';

    const options: FetchInit = {
    // Standard RequestInit options
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ key: 'value' }),

    // Extended options
    abortable: true, // Return FetchTask for manual abort control
    responseType: 'json', // Auto-parse response as JSON
    timeout: 10000, // Abort after 10 seconds
    onProgress: (result) => { // Track download progress
    result.inspect(({ completedByteLength, totalByteLength }) => {
    console.log(`${completedByteLength}/${totalByteLength}`);
    });
    },
    onChunk: (chunk) => { // Receive raw data chunks
    console.log('Received chunk:', chunk.byteLength, 'bytes');
    },
    };

    const task = fetchT('https://api.example.com/upload', options);
    interface FetchInit {
        abortable?: boolean;
        onChunk?: (chunk: Uint8Array<ArrayBuffer>) => void;
        onProgress?: (progressResult: IOResult<FetchProgress>) => void;
        responseType?: FetchResponseType;
        retry?: number | FetchRetryOptions;
        timeout?: number;
    }

    Hierarchy

    • RequestInit
      • FetchInit
    Index

    Properties

    abortable?: boolean

    When true, returns a FetchTask instead of FetchResult.

    The FetchTask provides abort() method and aborted status.

    false
    
    onChunk?: (chunk: Uint8Array<ArrayBuffer>) => void

    Callback invoked when a chunk of data is received.

    Useful for streaming or processing data as it arrives. Each chunk is a Uint8Array<ArrayBuffer> containing the raw bytes.

    Note: This feature uses response.clone() internally. The cloned stream shares the same underlying data source (via tee()), so it does NOT double memory usage. However, if the two streams consume data at different speeds, chunks may be buffered temporarily until both streams have read them.

    Type Declaration

      • (chunk: Uint8Array<ArrayBuffer>): void
      • Parameters

        • chunk: Uint8Array<ArrayBuffer>

          The raw data chunk received from the response stream.

        Returns void

    onProgress?: (progressResult: IOResult<FetchProgress>) => void

    Callback invoked during download to report progress.

    Receives an IOResult<FetchProgress>:

    • Ok(FetchProgress) - Progress update with byte counts
    • Err(Error) - If Content-Length header is missing (called once)

    Note: This feature uses response.clone() internally. The cloned stream shares the same underlying data source (via tee()), so it does NOT double memory usage. However, if the two streams consume data at different speeds, chunks may be buffered temporarily until both streams have read them.

    Type Declaration

      • (progressResult: IOResult<FetchProgress>): void
      • Parameters

        • progressResult: IOResult<FetchProgress>

          The progress result, either success with progress data or error.

        Returns void

    responseType?: FetchResponseType

    Specifies how the response body should be parsed.

    • 'text' - Returns string
    • 'json' - Returns parsed JSON (type T)
    • 'arraybuffer' - Returns ArrayBuffer
    • 'bytes' - Returns Uint8Array<ArrayBuffer> (with fallback for older environments)
    • 'blob' - Returns Blob
    • 'stream' - Returns ReadableStream<Uint8Array<ArrayBuffer>>
    • undefined - Returns raw Response object

    When using a dynamic string value (not a literal type), the return type will be FetchResponseData (union of all possible types).

    retry?: number | FetchRetryOptions

    Retry options.

    Can be a number (shorthand for retries count) or an options object.

    // Retry up to 3 times on network errors
    const result = await fetchT('https://api.example.com/data', {
    retry: 3,
    });

    // Detailed configuration
    const result = await fetchT('https://api.example.com/data', {
    retry: {
    retries: 3,
    delay: 1000,
    when: [500, 502],
    onRetry: (error, attempt) => console.log(error),
    },
    });
    timeout?: number

    Maximum time in milliseconds to wait for the request to complete.

    If exceeded, the request is automatically aborted with a TimeoutError. Must be a positive number.