minigame-std
    Preparing search index...

    Interface UnionFetchInit

    联合网络请求初始化配置类型,结合了 FetchInit 和 MinaFetchInit,并统一了 body 和 headers。

    • 使用 body 传递请求体数据,支持字符串、对象和 BufferSource。
    • 使用 headers 传递请求头,小游戏平台自动映射为 header

    1.0.0

    import { fetchT, type UnionFetchInit } from 'minigame-std';

    const init: UnionFetchInit = {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: { key: 'value' },
    responseType: 'json',
    };
    const task = fetchT('https://api.example.com/data', init);
    interface UnionFetchInit {
        abortable?: boolean;
        body?: string | BufferSource | IAnyObject;
        cache?: RequestCache;
        complete?: RequestCompleteCallback;
        credentials?: RequestCredentials;
        enableCache?: boolean;
        enableChunked?: boolean;
        enableHttp2?: boolean;
        enableHttpDNS?: boolean;
        enableProfile?: boolean;
        enableQuic?: boolean;
        forceCellularNetwork?: boolean;
        headers?: Record<string, string>;
        httpDNSServiceId?: string;
        httpDNSTimeout?: number;
        integrity?: string;
        keepalive?: boolean;
        method?:
            | "OPTIONS"
            | "GET"
            | "HEAD"
            | "POST"
            | "PUT"
            | "DELETE"
            | "TRACE"
            | "CONNECT";
        mode?: RequestMode;
        onChunk?: (chunk: Uint8Array<ArrayBuffer>) => void;
        onProgress?: (progressResult: IOResult<FetchProgress>) => void;
        priority?: RequestPriority;
        redirect?: "follow" | "manual";
        referrer?: string;
        referrerPolicy?: ReferrerPolicy;
        responseType?: "json" | "text" | "arraybuffer";
        retry?: number | FetchRetryOptions;
        signal?: AbortSignal | null;
        timeout?: number;
        useHighPerformanceMode?: boolean;
        window?: null;
    }

    Hierarchy

    • Omit<FetchInit & MinaFetchInit, "body" | "header" | "headers" | "data">
      • UnionFetchInit
    Index

    Properties

    abortable?: boolean

    When true, returns a FetchTask instead of FetchResult.

    The FetchTask provides abort() method and aborted status.

    false
    
    body?: string | BufferSource | IAnyObject
    cache?: RequestCache

    A string indicating how the request will interact with the browser's cache to set request's cache.

    complete?: RequestCompleteCallback

    接口调用结束的回调函数(调用成功、失败都会执行)

    credentials?: RequestCredentials

    A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials.

    enableCache?: boolean

    需要基础库: 2.10.4

    开启 Http 缓存

    enableChunked?: boolean

    需要基础库: 2.20.2

    开启 transfer-encoding chunked。

    enableHttp2?: boolean

    需要基础库: 2.10.4

    开启 http2

    enableHttpDNS?: boolean

    需要基础库: 2.19.1

    是否开启 HttpDNS 服务。如开启,需要同时填入 httpDNSServiceId 。 HttpDNS 用法详见 移动解析HttpDNS

    enableProfile?: boolean

    是否开启 profile。iOS 和 Android 端默认开启,其他端暂不支持。开启后可在接口回调的 res.profile 中查看性能调试信息。

    enableQuic?: boolean

    需要基础库: 2.10.4

    是否开启 Quic/h3 协议(iOS 微信目前使用 gQUIC-Q43;Android 微信在 v8.0.54 前使用 gQUIC-Q43,v8.0.54 开始使用 IETF QUIC,即 h3 协议;PC微信使用 IETF QUIC,即 h3 协议)

    forceCellularNetwork?: boolean

    需要基础库: 2.21.0

    强制使用蜂窝网络发送请求

    headers?: Record<string, string>
    httpDNSServiceId?: string

    需要基础库: 2.19.1

    HttpDNS 服务商 Id。 HttpDNS 用法详见 移动解析HttpDNS

    httpDNSTimeout?: number

    需要基础库: 3.8.9

    HttpDNS 超时时间。HttpDNS解析时间超过该值时不再走HttpDNS,本次请求将回退到localDNS。默认为 60000 毫秒。 HttpDNS 用法详见 移动解析HttpDNS

    integrity?: string

    A cryptographic hash of the resource to be fetched by request. Sets request's integrity.

    keepalive?: boolean

    A boolean to set request's keepalive.

    method?:
        | "OPTIONS"
        | "GET"
        | "HEAD"
        | "POST"
        | "PUT"
        | "DELETE"
        | "TRACE"
        | "CONNECT"

    A string to set request's method.

    mode?: RequestMode

    A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode.

    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

    priority?: RequestPriority
    redirect?: "follow" | "manual"

    A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect.

    referrer?: string

    A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer.

    referrerPolicy?: ReferrerPolicy

    A referrer policy to set request's referrerPolicy.

    responseType?: "json" | "text" | "arraybuffer"

    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),
    },
    });
    signal?: AbortSignal | null

    An AbortSignal to set request's signal.

    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.

    useHighPerformanceMode?: boolean

    需要基础库: 3.3.3

    使用高性能模式。从基础库 v3.5.0 开始在 Android 端默认开启,其他端暂不生效。该模式下有更优的网络性能表现,更多信息请查看下方说明。

    window?: null

    Can only be null. Used to disassociate request from any Window.