minigame-std
Version:
Cross-platform standard library for WeChat minigame and web browsers with unified APIs for crypto, fs, fetch, storage, and more.
124 lines (119 loc) • 4.23 kB
TypeScript
import { FetchInit, FetchTask } from '@happy-ts/fetch-t';
export { ABORT_ERROR, FetchError, FetchTask, TIMEOUT_ERROR } from '@happy-ts/fetch-t';
/**
* 联合网络请求初始化配置类型,结合了 FetchInit 和 MinaFetchInit,并统一了 body 和 headers。
*
* - 使用 `body` 传递请求体数据,支持字符串、对象和 BufferSource。
* - 使用 `headers` 传递请求头,小游戏平台自动映射为 `header`。
* @since 1.0.0
* @example
* ```ts
* 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 extends Omit<FetchInit & MinaFetchInit, 'body' | 'header' | 'headers' | 'data'> {
body?: string | WechatMinigame.IAnyObject | BufferSource;
headers?: Record<string, string>;
}
/**
* 微信小游戏网络请求初始化配置接口,继承自微信小游戏请求选项,除去'url'和'responseType'。
* @internal
*/
interface MinaFetchInit extends Omit<WechatMinigame.RequestOption, 'url' | 'dataType' | 'responseType' | 'success' | 'fail'> {
responseType?: 'arraybuffer' | 'text' | 'json';
onChunk?: FetchInit['onChunk'];
}
/**
* 网络请求模块,提供可中断的 fetch 请求功能,支持 text、JSON、ArrayBuffer 等响应类型。
* @module fetch
*/
/**
* 发起一个可中断的文本类型响应的网络请求。
* @param url - 请求的 URL 地址。
* @param init - 请求的初始化配置,指定响应类型为文本且请求可中断。
* @returns 返回一个文本类型的 FetchTask。
* @since 1.0.0
* @example
* ```ts
* const task = fetchT('https://api.example.com/data', { responseType: 'text' });
* const result = await task.result;
* if (result.isOk()) {
* console.log(result.unwrap()); // 文本内容
* }
* // 如需中断请求
* task.abort();
* ```
*/
declare function fetchT(url: string, init: UnionFetchInit & {
responseType: 'text';
}): FetchTask<string>;
/**
* 发起一个可中断的 ArrayBuffer 类型响应的网络请求。
* @param url - 请求的 URL 地址。
* @param init - 请求的初始化配置,指定响应类型为 ArrayBuffer 且请求可中断。
* @returns 返回一个 ArrayBuffer 类型的 FetchTask。
* @since 1.0.0
* @example
* ```ts
* const task = fetchT('https://api.example.com/file', { responseType: 'arraybuffer' });
* const result = await task.result;
* if (result.isOk()) {
* const buffer = result.unwrap();
* console.log('文件大小:', buffer.byteLength);
* }
* ```
*/
declare function fetchT(url: string, init: UnionFetchInit & {
responseType: 'arraybuffer';
}): FetchTask<ArrayBuffer>;
/**
* 发起一个可中断的 JSON 类型响应的网络请求。
* @typeParam T - 预期的 JSON 响应数据类型。
* @param url - 请求的 URL 地址。
* @param init - 请求的初始化配置,指定响应类型为 JSON 且请求可中断。
* @returns 返回一个 JSON 类型的 FetchTask。
* @since 1.0.0
* @example
* ```ts
* interface User {
* id: number;
* name: string;
* }
* const task = fetchT<User>('https://api.example.com/user/1', { responseType: 'json' });
* const result = await task.result;
* if (result.isOk()) {
* const user = result.unwrap();
* console.log(user.name);
* }
* ```
*/
declare function fetchT<T>(url: string, init: UnionFetchInit & {
responseType: 'json';
}): FetchTask<T>;
/**
* 发起一个可中断的网络请求,默认返回文本类型响应。
* @typeParam T - 预期的响应数据类型。
* @param url - 请求的 URL 地址。
* @param init - 请求的初始化配置,指定请求可中断。
* @returns FetchTask。
* @since 1.0.0
* @example
* ```ts
* const task = fetchT('https://api.example.com/data');
* const result = await task.result;
* if (result.isOk()) {
* console.log(result.unwrap());
* }
* ```
*/
declare function fetchT(url: string, init?: UnionFetchInit): FetchTask<string | Response>;
export { fetchT };
export type { UnionFetchInit };