minigame-std
Version:
Mini Game Standard Development Library.
1,143 lines (1,096 loc) • 42.5 kB
TypeScript
import { AsyncVoidIOResult, AsyncIOResult, VoidIOResult, IOResult, AsyncResult } from 'happy-rusty';
import { FetchInit, FetchTask } from '@happy-ts/fetch-t';
export { ABORT_ERROR, FetchError, FetchTask, TIMEOUT_ERROR } from '@happy-ts/fetch-t';
import * as happyOpfs from 'happy-opfs';
import { WriteFileContent as WriteFileContent$1, FsRequestInit, UploadRequestInit, ZipOptions, ReadFileContent as ReadFileContent$1, WriteOptions, DownloadFileTempResponse } from 'happy-opfs';
/**
* 断言传入的是一个字符串。
* @param str - 需要断言的字符串。
*/
declare function assertString(str: string): void;
/**
* 断言传入的 URL 是否为 `https` 协议。
* @param url - 需要断言的 URL 字符串。
*/
declare function assertSafeUrl(url: string): void;
/**
* 断言传入的 WebSocket URL 是否为 `wss` 协议。
* @param url - 需要断言的 WebSocket URL 字符串。
*/
declare function assertSafeSocketUrl(url: string): void;
/**
* 播放音频的选项。
*/
interface PlayOptions {
/**
* 是否循环播放。
* @defaultValue `false`
*/
loop?: boolean;
/**
* 播放完后是否自动调用 `source.disconnect`。
* @defaultValue `true`
*/
autoDisconnect?: boolean;
}
/**
* 获取缓存的 AudioContext。
* @returns 返回缓存的 AudioContext。
*/
declare function getGlobalAudioContext(): AudioContext;
/**
* 关闭缓存的 AudioContext。
* @returns 返回一个 AsyncVoidIOResult。
*/
declare function closeGlobalAudioContext(): AsyncVoidIOResult;
/**
* 创建一个 AudioContext。
* 如果要获取缓存的实例,请使用 `getGlobalAudioContext`。
* @returns 返回一个 AudioContext实例。
*/
declare function createWebAudioContext(): AudioContext;
/**
* 播放一个 AudioBuffer。
* @param buffer - 解码后的 AudioBuffer。
* @param options - 播放选项。
* @returns 正在播放的 AudioBufferSourceNode。
*/
declare function playWebAudioFromAudioBuffer(buffer: AudioBuffer, options?: PlayOptions): AudioBufferSourceNode;
/**
* 使用 Buffer 进行解码播放。
* @param buffer - 需要解码的 Buffer。
* @param options - 播放选项。
* @returns 正在播放的 AudioBufferSourceNode。
*/
declare function playWebAudioFromArrayBuffer(buffer: BufferSource, options?: PlayOptions): Promise<AudioBufferSourceNode>;
/**
* 读取文件并播放。
* @param filePath - 文件路径。
* @param options - 播放选项。
* @returns 正在播放的 AudioBufferSourceNode。
*/
declare function playWebAudioFromFile(filePath: string, options?: PlayOptions): AsyncIOResult<AudioBufferSourceNode>;
type mod$8_PlayOptions = PlayOptions;
declare const mod$8_closeGlobalAudioContext: typeof closeGlobalAudioContext;
declare const mod$8_createWebAudioContext: typeof createWebAudioContext;
declare const mod$8_getGlobalAudioContext: typeof getGlobalAudioContext;
declare const mod$8_playWebAudioFromArrayBuffer: typeof playWebAudioFromArrayBuffer;
declare const mod$8_playWebAudioFromAudioBuffer: typeof playWebAudioFromAudioBuffer;
declare const mod$8_playWebAudioFromFile: typeof playWebAudioFromFile;
declare namespace mod$8 {
export { mod$8_closeGlobalAudioContext as closeGlobalAudioContext, mod$8_createWebAudioContext as createWebAudioContext, mod$8_getGlobalAudioContext as getGlobalAudioContext, mod$8_playWebAudioFromArrayBuffer as playWebAudioFromArrayBuffer, mod$8_playWebAudioFromAudioBuffer as playWebAudioFromAudioBuffer, mod$8_playWebAudioFromFile as playWebAudioFromFile };
export type { mod$8_PlayOptions as PlayOptions };
}
/**
* @fileoverview Encode/Decode between Uint8Array and base64 encoded string.
*
* Forked from @std/encoding/base64 and https://github.com/cross-org/base64
*/
/**
* Converts BufferSource into a base64 encoded string.
*
* @param data - The data to encode.
* @returns The base64 encoded string.
*/
declare function base64FromBuffer(data: BufferSource): string;
/**
* Converts a base64 encoded string to an Uint8Array
*
* @param data - Base64 encoded string
* @returns The decoded data as an Uint8Array.
*/
declare function base64ToBuffer(data: string): Uint8Array;
/**
* 将字符串数据编码为 Base64 格式。
* @param data - 需要编码的字符串数据。
* @returns 编码后的 Base64 字符串。
*/
declare function encodeBase64(data: string): string;
/**
* 将 Base64 格式的字符串数据解码。
* @param data - 需要解码的 Base64 字符串。
* @returns 解码后的字符串。
*/
declare function decodeBase64(data: string): string;
/**
* 异步写入文本数据到剪贴板。
* @param data - 需要写入的文本数据。
* @returns 写入操作的结果。
*/
declare function writeText(data: string): AsyncVoidIOResult;
/**
* 异步读取剪贴板文本数据。
* @returns 读取操作的结果。
*/
declare function readText(): AsyncIOResult<string>;
declare const mod$7_readText: typeof readText;
declare const mod$7_writeText: typeof writeText;
declare namespace mod$7 {
export {
mod$7_readText as readText,
mod$7_writeText as writeText,
};
}
/**
* The data source.
*/
type DataSource = string | BufferSource;
/**
* 将字符串数据编码为 `Uint8Array`
* @param data - 需要编码的字符串数据。
* @returns 编码后的 `Uint8Array`
*/
declare function textEncode(data: string): Uint8Array;
/**
* 将二进制数据解码为字符串。
* @param data - 需要解码的二进制数据。
* @returns 解码后的字符串。
*/
declare function textDecode(data: BufferSource): string;
/**
* 将 BufferSource 转换为十六进制字符串。
* @param buffer - 需要转换的 BufferSource。
* @returns 十六进制字符串。
*/
declare function hexFromBuffer(buffer: BufferSource): string;
/**
* 将字符串转换为 Uint8Array。
* @param str - 需要转换的字符串。
* @returns Uint8Array。
*/
declare function byteStringToBuffer(str: string): Uint8Array;
/**
* 将 Buffer 转换为 ByteString。
* @param buffer - 需要转换的 Buffer。
* @returns ByteString。
*/
declare function byteStringFromBuffer(buffer: BufferSource): string;
/**
* 将 UTF-8 字符串转换为 ByteString。
*
* @param data - 需要转换的字符串或 BufferSource。
* @returns 转换后的 ByteString。
*/
declare function toByteString(data: DataSource): string;
/**
* The RSA public key.
*/
interface RSAPublicKey {
/**
* Use the RSA-OAEP algorithm to encrypt the data.
* @param data - The data to encrypt.
* @returns Encrypted data.
*/
encrypt(data: string): Promise<ArrayBuffer>;
/**
* `encrypt` then convert to base64 string.
*/
encryptToString(data: string): Promise<string>;
}
/**
* Supported hash algorithms.
*/
type SHA = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512';
/**
* 计算 SHA-1 HMAC。
*/
declare function sha1HMAC(key: DataSource, data: DataSource): Promise<string>;
/**
* 计算 SHA-256 HMAC。
*/
declare function sha256HMAC(key: DataSource, data: DataSource): Promise<string>;
/**
* 计算 SHA-384 HMAC。
*/
declare function sha384HMAC(key: DataSource, data: DataSource): Promise<string>;
/**
* 计算 SHA-512 HMAC。
*/
declare function sha512HMAC(key: DataSource, data: DataSource): Promise<string>;
/**
* Md5 hash
*/
declare class Md5 {
private a;
private b;
private c;
private d;
private block;
private pos;
private n0;
private n1;
private addLength;
private hash;
/**
* Update internal state.
* @param data data to update, data cannot exceed 2^32 bytes.
*/
update(data: DataSource): this;
/**
* Returns final hash.
*/
digest(): ArrayBuffer;
/**
* Returns hash as a hex string.
*/
toString(): string;
}
/**
* 计算字符串或者 buffer 的 MD5 值,结果用16进制字符串表示。
* @param data - 需要计算 MD5 值的数据。
* @returns 计算得到的 MD5 十六进制字符串。
*/
declare function md5(data: DataSource): string;
/**
* UUID.
*/
type UUID = `${string}-${string}-${string}-${string}-${string}`;
/**
* 获取密码学安全随机数。
* @param length - 要生成的字节数。
* @returns 生成的随机数 Buffer。
*/
declare function getRandomValues(length: number): AsyncIOResult<Uint8Array>;
/**
* 生成 UUID。
* @returns UUID 字符串。
*/
declare function randomUUID(): AsyncIOResult<UUID>;
/**
* Import a public key from a PEM encoded string for encryption.
* @param pem - PEM encoded string.
* @param hash - Hash algorithm.
* @returns
*/
declare function importPublicKey(pem: string, hash: SHA): Promise<RSAPublicKey>;
declare const mod$6_importPublicKey: typeof importPublicKey;
declare namespace mod$6 {
export {
mod$6_importPublicKey as importPublicKey,
};
}
/**
* 计算 SHA-1。
*/
declare function sha1(data: DataSource): Promise<string>;
/**
* 计算 SHA-256。
*/
declare function sha256(data: DataSource): Promise<string>;
/**
* 计算 SHA-384。
*/
declare function sha384(data: DataSource): Promise<string>;
/**
* 计算 SHA-512。
*/
declare function sha512(data: DataSource): Promise<string>;
type mod$5_Md5 = Md5;
declare const mod$5_Md5: typeof Md5;
type mod$5_RSAPublicKey = RSAPublicKey;
type mod$5_SHA = SHA;
type mod$5_UUID = UUID;
declare const mod$5_getRandomValues: typeof getRandomValues;
declare const mod$5_md5: typeof md5;
declare const mod$5_randomUUID: typeof randomUUID;
declare const mod$5_sha1: typeof sha1;
declare const mod$5_sha1HMAC: typeof sha1HMAC;
declare const mod$5_sha256: typeof sha256;
declare const mod$5_sha256HMAC: typeof sha256HMAC;
declare const mod$5_sha384: typeof sha384;
declare const mod$5_sha384HMAC: typeof sha384HMAC;
declare const mod$5_sha512: typeof sha512;
declare const mod$5_sha512HMAC: typeof sha512HMAC;
declare namespace mod$5 {
export { mod$5_Md5 as Md5, mod$5_getRandomValues as getRandomValues, mod$5_md5 as md5, mod$5_randomUUID as randomUUID, mod$6 as rsa, mod$5_sha1 as sha1, mod$5_sha1HMAC as sha1HMAC, mod$5_sha256 as sha256, mod$5_sha256HMAC as sha256HMAC, mod$5_sha384 as sha384, mod$5_sha384HMAC as sha384HMAC, mod$5_sha512 as sha512, mod$5_sha512HMAC as sha512HMAC };
export type { mod$5_RSAPublicKey as RSAPublicKey, mod$5_SHA as SHA, mod$5_UUID as UUID };
}
/**
* 添加错误监听器,用于监听标准的错误事件。
* @param listener - 错误事件的回调函数。
* @returns 返回一个函数,调用该函数可以移除监听器。
*/
declare function addErrorListener(listener: (ev: WechatMinigame.Error) => void): () => void;
/**
* 添加未处理的 Promise 拒绝事件监听器。
* @param listener - 未处理的 Promise 拒绝事件的回调函数。
* @returns 返回一个函数,调用该函数可以移除监听器。
*/
declare function addUnhandledrejectionListener(listener: (ev: Pick<PromiseRejectionEvent, 'reason' | 'promise'>) => void): () => void;
/**
* 添加窗口大小变化监听器。
* @param listener - 窗口大小变化的回调函数。
* @returns 返回一个函数,调用该函数可以移除监听器。
*/
declare function addResizeListener(listener: WechatMinigame.OnWindowResizeCallback): () => void;
/**
* 微信小游戏网络请求初始化配置接口,继承自微信小游戏请求选项,除去'url'和'responseType'。
*/
interface MinaFetchInit extends Omit<WechatMinigame.RequestOption, 'url' | 'dataType' | 'responseType' | 'success' | 'fail'> {
responseType?: 'arraybuffer' | 'text' | 'json';
onChunk?: FetchInit['onChunk'];
}
/**
* 联合网络请求初始化配置类型,结合了 FetchInit 和 MinaFetchInit。
*/
type UnionFetchInit = FetchInit & MinaFetchInit;
/**
* 发起一个可中断的文本类型响应的网络请求。
* @param url - 请求的 URL 地址。
* @param init - 请求的初始化配置,指定响应类型为文本且请求可中断。
* @returns 返回一个文本类型的 FetchTask。
*/
declare function fetchT(url: string, init: UnionFetchInit & {
responseType: 'text';
}): FetchTask<string>;
/**
* 发起一个可中断的 ArrayBuffer 类型响应的网络请求。
* @param url - 请求的 URL 地址。
* @param init - 请求的初始化配置,指定响应类型为 ArrayBuffer 且请求可中断。
* @returns 返回一个 ArrayBuffer 类型的 FetchTask。
*/
declare function fetchT(url: string, init: UnionFetchInit & {
responseType: 'arraybuffer';
}): FetchTask<ArrayBuffer>;
/**
* 发起一个可中断的 JSON 类型响应的网络请求。
* @typeParam T - 预期的 JSON 响应数据类型。
* @param url - 请求的 URL 地址。
* @param init - 请求的初始化配置,指定响应类型为 JSON 且请求可中断。
* @returns 返回一个 JSON 类型的 FetchTask。
*/
declare function fetchT<T>(url: string, init: UnionFetchInit & {
responseType: 'json';
}): FetchTask<T>;
/**
* 发起一个可中断的网络请求,默认返回文本类型响应。
* @typeParam T - 预期的响应数据类型。
* @param url - 请求的 URL 地址。
* @param init - 请求的初始化配置,指定请求可中断。
* @returns FetchTask。
*/
declare function fetchT(url: string, init?: UnionFetchInit): FetchTask<string | Response>;
/**
* File content type for write, support `ArrayBuffer` `TypedArray` `string`.
*/
type WriteFileContent = Exclude<WriteFileContent$1, Blob>;
/**
* File content type for read result, support `ArrayBuffer` `string`.
*/
type ReadFileContent = Exclude<ReadFileContent$1, Blob>;
/**
* Options for reading files with specified encoding.
*/
interface ReadOptions {
/**
* Read file encoding type, support `binary(ArrayBuffer)` `utf8(string)` `blob(Blob)`
*
* @defaultValue `'binary'`
*/
encoding?: FileEncoding;
}
/**
* Supported file encodings for reading and writing files.
*/
type FileEncoding = 'binary' | 'utf8';
/**
* Options for downloading files.
*/
interface DownloadFileOptions extends Omit<WechatMinigame.DownloadFileOption, 'url' | 'filePath' | 'success' | 'fail'> {
onProgress?: FetchInit['onProgress'];
}
/**
* Options for uploading files.
*/
interface UploadFileOptions extends Omit<WechatMinigame.UploadFileOption, 'url' | 'filePath' | 'name' | 'success' | 'fail'> {
/**
* Optional file name.
*/
name?: string;
}
/**
* Options for union requests.
*/
type UnionDownloadFileOptions = FsRequestInit & DownloadFileOptions;
/**
* Options for union requests.
*/
type UnionUploadFileOptions = UploadRequestInit & UploadFileOptions;
/**
* Options for stat operations.
*/
interface StatOptions {
/**
* Whether to recursively read the contents of directories.
*/
recursive: boolean;
}
/**
* Union options for `unzipFromUrl`.
*/
type ZipFromUrlOptions = (DownloadFileOptions & ZipOptions) & FsRequestInit;
/**
* 递归创建文件夹,相当于`mkdir -p`。
* @param dirPath - 将要创建的目录的路径。
* @returns 创建成功返回 true 的异步操作结果。
*/
declare function mkdir(dirPath: string): AsyncVoidIOResult;
/**
* 重命名文件或目录。
* @param srcPath - 原始路径。
* @param destPath - 新路径。
* @returns 重命名成功返回 true 的异步操作结果。
*/
declare function move(srcPath: string, destPath: string): AsyncVoidIOResult;
/**
* 异步读取指定目录下的所有文件和子目录。
* @param dirPath - 需要读取的目录路径。
* @returns 包含目录内容的字符串数组的异步操作结果。
*/
declare function readDir(dirPath: string): AsyncIOResult<string[]>;
/**
* 读取文件内容。
* @param filePath - 文件的路径。
* @returns 包含文件内容的 ArrayBuffer 的异步操作结果。
*/
declare function readFile(filePath: string): AsyncIOResult<ArrayBuffer>;
/**
* 删除文件或目录。
* @param path - 要删除的文件或目录的路径。
* @returns 删除成功返回 true 的异步操作结果。
*/
declare function remove(path: string): AsyncVoidIOResult;
declare function stat(path: string): AsyncIOResult<WechatMinigame.Stats>;
declare function stat(path: string, options: StatOptions & {
recursive: true;
}): AsyncIOResult<WechatMinigame.FileStats[]>;
declare function stat(path: string, options?: StatOptions): AsyncIOResult<WechatMinigame.Stats | WechatMinigame.FileStats[]>;
/**
* 写入文件,不存在则创建,同时创建对应目录,contents只支持ArrayBuffer和string,并且需要确保string一定是utf8编码的。
* @param filePath - 文件路径。
* @param contents - 要写入的内容。
* @param options - 可选选项。
* @returns 写入成功返回 true 的异步操作结果。
*/
declare function writeFile(filePath: string, contents: WriteFileContent, options?: WriteOptions): AsyncVoidIOResult;
/**
* 向文件追加内容。
* @param filePath - 文件路径。
* @param contents - 要追加的内容。
* @returns 追加成功返回 true 的异步操作结果。
*/
declare function appendFile(filePath: string, contents: WriteFileContent): AsyncVoidIOResult;
/**
* 复制文件或文件夹。
*
* @param srcPath - 源文件或文件夹路径。
* @param destPath - 目标文件或文件夹路径。
* @returns 操作的异步结果。
*/
declare function copy(srcPath: string, destPath: string): AsyncVoidIOResult;
/**
* 检查指定路径的文件或目录是否存在。
* @param path - 文件或目录的路径。
* @returns 存在返回 true 的异步操作结果。
*/
declare function exists(path: string): AsyncIOResult<boolean>;
/**
* 清空指定目录下的所有文件和子目录。
* @param dirPath - 目录路径。
* @returns 清空成功返回 true 的异步操作结果。
*/
declare function emptyDir(dirPath: string): AsyncVoidIOResult;
/**
* 读取文件并解析为 JSON。
* @param filePath - 文件路径。
* @returns 读取结果。
*/
declare function readJsonFile<T>(filePath: string): AsyncIOResult<T>;
/**
* 读取文本文件的内容。
* @param filePath - 文件路径。
* @returns 包含文件文本内容的异步操作结果。
*/
declare function readTextFile(filePath: string): AsyncIOResult<string>;
/**
* 下载文件并保存到临时文件。
* @param fileUrl - 文件的网络 URL。
* @param options - 可选参数。
* @returns 下载操作的异步结果,成功时返回 true。
*/
declare function downloadFile(fileUrl: string, options?: UnionDownloadFileOptions): FetchTask<WechatMinigame.DownloadFileSuccessCallbackResult | DownloadFileTempResponse>;
/**
* 下载文件。
* @param fileUrl - 文件的网络 URL。
* @param filePath - 可选的下载后文件存储的路径,没传则存到临时文件。
* @param options - 可选的请求初始化参数。
* @returns 下载成功返回原始结果。
*/
declare function downloadFile(fileUrl: string, filePath: string, options?: UnionDownloadFileOptions): FetchTask<WechatMinigame.DownloadFileSuccessCallbackResult | Response>;
/**
* 上传本地文件。
* @param filePath - 需要上传的文件路径。
* @param fileUrl - 目标服务器的 URL。
* @param options - 可选的请求初始化参数。
* @returns 上传成功返回原始结果。
*/
declare function uploadFile(filePath: string, fileUrl: string, options?: UnionUploadFileOptions): FetchTask<WechatMinigame.UploadFileSuccessCallbackResult | Response>;
/**
* 解压 zip 文件。
* @param zipFilePath - 要解压的 zip 文件路径。
* @param targetPath - 要解压到的目标文件夹路径。
* @returns 解压操作的异步结果。
*/
declare function unzip(zipFilePath: string, targetPath: string): AsyncVoidIOResult;
/**
* 从网络下载 zip 文件并解压。
* @param zipFileUrl - Zip 文件的网络地址。
* @param targetPath - 要解压到的目标文件夹路径。
* @param options - 可选的下载参数。
* @returns 下载并解压操作的异步结果。
*/
declare function unzipFromUrl(zipFileUrl: string, targetPath: string, options?: UnionDownloadFileOptions): AsyncVoidIOResult;
/**
* 压缩文件到内存。
* @param sourcePath - 需要压缩的文件(夹)路径。
* @param options - 可选的压缩参数。
* @returns 压缩成功的异步结果。
*/
declare function zip(sourcePath: string, options?: ZipOptions): AsyncIOResult<Uint8Array>;
/**
* 压缩文件。
* @param sourcePath - 需要压缩的文件(夹)路径。
* @param zipFilePath - 压缩后的 zip 文件路径。
* @param options - 可选的压缩参数。
* @returns 压缩成功的异步结果。
*/
declare function zip(sourcePath: string, zipFilePath: string, options?: ZipOptions): AsyncVoidIOResult;
/**
* 下载文件并压缩到内存。
* @param sourceUrl - 要下载的文件 URL。
* @param options - 合并的下载和压缩选项。
*/
declare function zipFromUrl(sourceUrl: string, options?: ZipFromUrlOptions): AsyncIOResult<Uint8Array>;
/**
* 下载文件并压缩为 zip 文件。
* @param sourceUrl - 要下载的文件 URL。
* @param zipFilePath - 要输出的 zip 文件路径。
* @param options - 合并的下载和压缩选项。
*/
declare function zipFromUrl(sourceUrl: string, zipFilePath: string, options?: ZipFromUrlOptions): AsyncVoidIOResult;
/**
* `mkdir` 的同步版本。
*/
declare function mkdirSync(dirPath: string): VoidIOResult;
/**
* `move` 的同步版本。
*/
declare function moveSync(srcPath: string, destPath: string): VoidIOResult;
/**
* `readDir` 的同步版本。
*/
declare function readDirSync(dirPath: string): IOResult<string[]>;
/**
* `readFile` 的同步版本。
*/
declare function readFileSync(filePath: string): IOResult<ArrayBuffer>;
/**
* `remove` 的同步版本。
*/
declare function removeSync(path: string): VoidIOResult;
/**
* `stat` 的同步版本。
*/
declare function statSync(path: string): IOResult<WechatMinigame.Stats>;
declare function statSync(path: string, options: StatOptions & {
recursive: true;
}): IOResult<WechatMinigame.FileStats[]>;
declare function statSync(path: string, options?: StatOptions): IOResult<WechatMinigame.Stats | WechatMinigame.FileStats[]>;
/**
* `writeFile` 的同步版本。
*/
declare function writeFileSync(filePath: string, contents: WriteFileContent): VoidIOResult;
/**
* `copy` 的同步版本。
*/
declare function copySync(srcPath: string, destPath: string): VoidIOResult;
/**
* `appendFile` 的同步版本。
*/
declare function appendFileSync(filePath: string, contents: WriteFileContent): VoidIOResult;
/**
* `exists` 的同步版本。
*/
declare function existsSync(path: string): IOResult<boolean>;
/**
* `emptyDir` 的同步版本。
*/
declare function emptyDirSync(dirPath: string): VoidIOResult;
/**
* `readJsonFile` 的同步版本。
*/
declare function readJsonFileSync<T>(filePath: string): IOResult<T>;
/**
* `readTextFile` 的同步版本。
*/
declare function readTextFileSync(filePath: string): IOResult<string>;
/**
* `unzip` 的同步版本。
*/
declare function unzipSync(zipFilePath: string, targetPath: string): VoidIOResult;
/**
* `zip` 的同步版本。
*/
declare function zipSync(sourcePath: string, zipFilePath: string, options?: ZipOptions): VoidIOResult;
type mod$4_DownloadFileOptions = DownloadFileOptions;
type mod$4_FileEncoding = FileEncoding;
type mod$4_ReadFileContent = ReadFileContent;
type mod$4_ReadOptions = ReadOptions;
type mod$4_StatOptions = StatOptions;
type mod$4_UnionDownloadFileOptions = UnionDownloadFileOptions;
type mod$4_UnionUploadFileOptions = UnionUploadFileOptions;
type mod$4_UploadFileOptions = UploadFileOptions;
type mod$4_WriteFileContent = WriteFileContent;
type mod$4_ZipFromUrlOptions = ZipFromUrlOptions;
declare const mod$4_appendFile: typeof appendFile;
declare const mod$4_appendFileSync: typeof appendFileSync;
declare const mod$4_copy: typeof copy;
declare const mod$4_copySync: typeof copySync;
declare const mod$4_downloadFile: typeof downloadFile;
declare const mod$4_emptyDir: typeof emptyDir;
declare const mod$4_emptyDirSync: typeof emptyDirSync;
declare const mod$4_exists: typeof exists;
declare const mod$4_existsSync: typeof existsSync;
declare const mod$4_mkdir: typeof mkdir;
declare const mod$4_mkdirSync: typeof mkdirSync;
declare const mod$4_move: typeof move;
declare const mod$4_moveSync: typeof moveSync;
declare const mod$4_readDir: typeof readDir;
declare const mod$4_readDirSync: typeof readDirSync;
declare const mod$4_readFile: typeof readFile;
declare const mod$4_readFileSync: typeof readFileSync;
declare const mod$4_readJsonFile: typeof readJsonFile;
declare const mod$4_readJsonFileSync: typeof readJsonFileSync;
declare const mod$4_readTextFile: typeof readTextFile;
declare const mod$4_readTextFileSync: typeof readTextFileSync;
declare const mod$4_remove: typeof remove;
declare const mod$4_removeSync: typeof removeSync;
declare const mod$4_stat: typeof stat;
declare const mod$4_statSync: typeof statSync;
declare const mod$4_unzip: typeof unzip;
declare const mod$4_unzipFromUrl: typeof unzipFromUrl;
declare const mod$4_unzipSync: typeof unzipSync;
declare const mod$4_uploadFile: typeof uploadFile;
declare const mod$4_writeFile: typeof writeFile;
declare const mod$4_writeFileSync: typeof writeFileSync;
declare const mod$4_zip: typeof zip;
declare const mod$4_zipFromUrl: typeof zipFromUrl;
declare const mod$4_zipSync: typeof zipSync;
declare namespace mod$4 {
export { mod$4_appendFile as appendFile, mod$4_appendFileSync as appendFileSync, mod$4_copy as copy, mod$4_copySync as copySync, mod$4_downloadFile as downloadFile, mod$4_emptyDir as emptyDir, mod$4_emptyDirSync as emptyDirSync, mod$4_exists as exists, mod$4_existsSync as existsSync, mod$4_mkdir as mkdir, mod$4_mkdirSync as mkdirSync, mod$4_move as move, mod$4_moveSync as moveSync, happyOpfs as opfs, mod$4_readDir as readDir, mod$4_readDirSync as readDirSync, mod$4_readFile as readFile, mod$4_readFileSync as readFileSync, mod$4_readJsonFile as readJsonFile, mod$4_readJsonFileSync as readJsonFileSync, mod$4_readTextFile as readTextFile, mod$4_readTextFileSync as readTextFileSync, mod$4_remove as remove, mod$4_removeSync as removeSync, mod$4_stat as stat, mod$4_statSync as statSync, mod$4_unzip as unzip, mod$4_unzipFromUrl as unzipFromUrl, mod$4_unzipSync as unzipSync, mod$4_uploadFile as uploadFile, mod$4_writeFile as writeFile, mod$4_writeFileSync as writeFileSync, mod$4_zip as zip, mod$4_zipFromUrl as zipFromUrl, mod$4_zipSync as zipSync };
export type { mod$4_DownloadFileOptions as DownloadFileOptions, mod$4_FileEncoding as FileEncoding, mod$4_ReadFileContent as ReadFileContent, mod$4_ReadOptions as ReadOptions, mod$4_StatOptions as StatOptions, mod$4_UnionDownloadFileOptions as UnionDownloadFileOptions, mod$4_UnionUploadFileOptions as UnionUploadFileOptions, mod$4_UploadFileOptions as UploadFileOptions, mod$4_WriteFileContent as WriteFileContent, mod$4_ZipFromUrlOptions as ZipFromUrlOptions };
}
/**
* 从URL创建图片。
* @param url - 图片URL。
* @returns Image对象。
*/
declare function createImageFromUrl(url: string): HTMLImageElement | WechatMinigame.Image;
/**
* 从文件创建图片。
* @param filePath - 文件路径。
* @returns 异步的Image对象。
*/
declare function createImageFromFile(filePath: string): AsyncIOResult<HTMLImageElement | WechatMinigame.Image>;
declare const mod$3_createImageFromFile: typeof createImageFromFile;
declare const mod$3_createImageFromUrl: typeof createImageFromUrl;
declare namespace mod$3 {
export {
mod$3_createImageFromFile as createImageFromFile,
mod$3_createImageFromUrl as createImageFromUrl,
};
}
/**
* geo 坐标。
*/
interface GeoPosition {
/**
* 纬度。
*/
latitude: number;
/**
* 经度。
*/
longitude: number;
}
/**
* 获取当前 geo 坐标。
* @returns 当前经纬度。
*/
declare function getCurrentPosition(): AsyncIOResult<GeoPosition>;
type mod$2_GeoPosition = GeoPosition;
declare const mod$2_getCurrentPosition: typeof getCurrentPosition;
declare namespace mod$2 {
export { mod$2_getCurrentPosition as getCurrentPosition };
export type { mod$2_GeoPosition as GeoPosition };
}
/**
* 网络状态,混合了 web 和小游戏环境。
*/
type NetworkType = 'wifi' | 'slow-2g' | '2g' | '3g' | '4g' | '5g' | 'unknown' | 'none';
/**
* 获取网络状态。
* @returns 根据浏览器支持情况不同,返回值可能为 `wifi` | `none` | `unknown` | `slow-2g` | `2g` | `3g` | `4g`
*/
declare function getNetworkType(): Promise<NetworkType>;
/**
* 监听网络状态变化。
* @param listener - 网络状态变化的回调函数。
* @returns 返回一个函数,调用该函数可以移除监听器。
*/
declare function addNetworkChangeListener(listener: (type: NetworkType) => void): () => void;
/**
* 平台类型,Web 或者小游戏。
*/
type TargetType = 'minigame' | 'web';
/**
* 获取当前的平台类型。
* @returns 返回当前的运行环境类型,可能是 'minigame' 或 'web'。
*/
declare function getTargetType(): TargetType;
/**
* 判断当前是否在 Web 环境中。
* @returns 如果在 Web 现境中返回 true,否则返回 false。
*/
declare function isWeb(): boolean;
/**
* 判断当前是否在小游戏环境中。
* @returns 如果在小游戏环境中返回 true,否则返回 false。
*/
declare function isMiniGame(): boolean;
/**
* 获取设备信息。
* @returns 返回小游戏的设备信息对象。
*/
declare function getDeviceInfo(): WechatMinigame.DeviceInfo;
/**
* 获取设备性能等级, web 环境返回 -2。
* @returns 返回设备性能等级。
*/
declare function getDeviceBenchmarkLevel(): AsyncIOResult<number>;
/**
* 判断当前是否在小游戏的运行时环境中。
* @returns 如果在小游戏的运行时环境中返回 true,否则返回 false。
*/
declare function isMiniGameRuntime(): boolean;
/**
* 判断当前是否在小游戏的开发者工具中。
* @returns 如果在小游戏的开发者工具中返回 true,否则返回 false。
*/
declare function isMiniGameDevtools(): boolean;
/**
* 判断当前是否在小游戏的 iOS 环境中。
* @returns 如果在小游戏的 iOS 环境中返回 true,否则返回 false。
*/
declare function isMiniGameIOS(): boolean;
/**
* 判断当前是否在小游戏的 Android 环境中。
* @returns 如果在小游戏的 Android 环境中返回 true,否则返回 false。
*/
declare function isMiniGameAndroid(): boolean;
/**
* 判断当前是否在小游戏的 Windows 环境中。
* @returns 如果在小游戏的 Windows 环境中返回 true,否则返回 false。
*/
declare function isMiniGameWin(): boolean;
/**
* 判断当前是否在小游戏的 Mac 环境中。
* @returns 如果在小游戏的 Mac 环境中返回 true,否则返回 false。
*/
declare function isMiniGameMac(): boolean;
/**
* 判断当前是否在小游戏的 HarmonyOS 环境中。
* @returns 如果在小游戏的 HarmonyOS 环境中返回 true,否则返回 false。
*/
declare function isMiniGameHarmonyOS(): boolean;
/**
* 获取窗口信息。
* @returns
*/
declare function getWindowInfo(): Pick<WechatMinigame.WindowInfo, 'pixelRatio' | 'screenHeight' | 'screenTop' | 'screenWidth' | 'windowHeight' | 'windowWidth'>;
type mod$1_TargetType = TargetType;
declare const mod$1_getDeviceBenchmarkLevel: typeof getDeviceBenchmarkLevel;
declare const mod$1_getDeviceInfo: typeof getDeviceInfo;
declare const mod$1_getTargetType: typeof getTargetType;
declare const mod$1_getWindowInfo: typeof getWindowInfo;
declare const mod$1_isMiniGame: typeof isMiniGame;
declare const mod$1_isMiniGameAndroid: typeof isMiniGameAndroid;
declare const mod$1_isMiniGameDevtools: typeof isMiniGameDevtools;
declare const mod$1_isMiniGameHarmonyOS: typeof isMiniGameHarmonyOS;
declare const mod$1_isMiniGameIOS: typeof isMiniGameIOS;
declare const mod$1_isMiniGameMac: typeof isMiniGameMac;
declare const mod$1_isMiniGameRuntime: typeof isMiniGameRuntime;
declare const mod$1_isMiniGameWin: typeof isMiniGameWin;
declare const mod$1_isWeb: typeof isWeb;
declare namespace mod$1 {
export { mod$1_getDeviceBenchmarkLevel as getDeviceBenchmarkLevel, mod$1_getDeviceInfo as getDeviceInfo, mod$1_getTargetType as getTargetType, mod$1_getWindowInfo as getWindowInfo, mod$1_isMiniGame as isMiniGame, mod$1_isMiniGameAndroid as isMiniGameAndroid, mod$1_isMiniGameDevtools as isMiniGameDevtools, mod$1_isMiniGameHarmonyOS as isMiniGameHarmonyOS, mod$1_isMiniGameIOS as isMiniGameIOS, mod$1_isMiniGameMac as isMiniGameMac, mod$1_isMiniGameRuntime as isMiniGameRuntime, mod$1_isMiniGameWin as isMiniGameWin, mod$1_isWeb as isWeb };
export type { mod$1_TargetType as TargetType };
}
/**
* WebSocket 连接状态,小游戏环境可用。
*/
declare const SocketReadyState: {
/**
* WebSocket.CONNECTING
*/
readonly CONNECTING: 0;
/**
* WebSocket.OPEN
*/
readonly OPEN: 1;
/**
* WebSocket.CLOSING
*/
readonly CLOSING: 2;
/**
* WebSocket.CLOSED
*/
readonly CLOSED: 3;
};
/**
* WebSocket 事件监听器映射接口,定义了与 WebSocket 事件对应的回调函数类型。
*/
interface SocketListenerMap {
/**
* 当 WebSocket 连接成功打开时触发。
*/
open(): void;
/**
* 当 WebSocket 连接关闭时触发。
* @param code - 表示关闭连接的状态码。
* @param reason - 表示关闭连接的原因。
*/
close(code: number, reason: string): void;
/**
* 当 WebSocket 接收到消息时触发。
* @param data - 接收到的消息数据,可以是字符串或者 ArrayBuffer。
*/
message(data: string | ArrayBuffer): void;
/**
* 当 WebSocket 连接发生错误时触发。
* @param err - 发生的错误对象。
*/
error(err: Error): void;
}
/**
* WebSocket 接口定义,描述了 WebSocket 的基本操作方法。
*/
interface ISocket {
/**
* WebSocket 的连接状态。
*/
readonly readyState: number;
/**
* 添加事件监听器到 WebSocket 对象。
* @typeParam K - 限定为 WebSocketEventMap 的键类型。
* @param type - 事件类型,如 'open', 'close', 'message', 'error'。
* @param listener - 对应事件的监听器回调函数。
* @returns 返回对应的`removeEventListener代理函数`。
*/
addEventListener<K extends keyof WebSocketEventMap>(type: K, listener: SocketListenerMap[K]): () => void;
/**
* 发送数据到 WebSocket 服务器。
* @param data - 要发送的数据,可以是字符串、ArrayBuffer 或 ArrayBufferView。
* @returns 返回一个 Promise,其解析为发送结果,成功时返回 true,失败时返回 Error。
*/
send(data: DataSource): AsyncVoidIOResult;
/**
* 关闭 WebSocket 连接。
* @param code - 可选的状态码,表示关闭连接的原因。
* @param reason - 可选的字符串,解释为什么要关闭连接。
*/
close(code?: number, reason?: string): void;
}
/**
* 创建Socket的可选参数。
*/
type SocketOptions = Omit<WechatMinigame.ConnectSocketOption, 'url' | 'complete' | 'success' | 'fail'>;
/**
* 创建并返回一个 WebSocket 连接。
* @param url - WebSocket 服务器的 URL。
* @param options - 可选的参数。
* @returns 返回一个实现了 ISocket 接口的 WebSocket 对象。
*/
declare function connectSocket(url: string, options?: SocketOptions): ISocket;
/**
* 将数据存储在本地缓存中。
* @param key - 数据的键名。
* @param data - 要存储的数据。
* @returns 返回一个 Promise,表示操作完成。
*/
declare function setItem(key: string, data: string): AsyncVoidIOResult;
/**
* 从本地缓存中读取数据。
* @param key - 数据的键名。
* @returns 返回一个 Promise,表示操作完成。
*/
declare function getItem(key: string): AsyncIOResult<string>;
/**
* 从本地缓存中移除指定的数据。
* @param key - 数据的键名。
* @returns 返回一个 Promise,表示操作完成。
*/
declare function removeItem(key: string): AsyncVoidIOResult;
/**
* 清除所有的本地存储数据。
* @returns 返回一个 Promise,表示操作完成。
*/
declare function clear(): AsyncVoidIOResult;
/**
* 获取本地存储数据的长度。
* @returns 返回一个 Promise,表示操作完成。
*/
declare function getLength(): AsyncIOResult<number>;
/**
* 检查本地存储中是否存在指定的数据。
* @param key - 数据的键名。
* @returns 返回一个 Promise,表示操作完成。
*/
declare function hasItem(key: string): AsyncIOResult<boolean>;
/**
* `setItem` 的同步版本。
*/
declare function setItemSync(key: string, data: string): VoidIOResult;
/**
* `getItem` 的同步版本。
*/
declare function getItemSync(key: string): IOResult<string>;
/**
* `removeItem` 的同步版本。
*/
declare function removeItemSync(key: string): VoidIOResult;
/**
* `clear` 的同步版本。
*/
declare function clearSync(): VoidIOResult;
/**
* `getLength` 的同步版本。
*/
declare function getLengthSync(): IOResult<number>;
/**
* `hasItem` 的同步版本。
* @param key - 数据的键名。
*/
declare function hasItemSync(key: string): IOResult<boolean>;
declare const mod_clear: typeof clear;
declare const mod_clearSync: typeof clearSync;
declare const mod_getItem: typeof getItem;
declare const mod_getItemSync: typeof getItemSync;
declare const mod_getLength: typeof getLength;
declare const mod_getLengthSync: typeof getLengthSync;
declare const mod_hasItem: typeof hasItem;
declare const mod_hasItemSync: typeof hasItemSync;
declare const mod_removeItem: typeof removeItem;
declare const mod_removeItemSync: typeof removeItemSync;
declare const mod_setItem: typeof setItem;
declare const mod_setItemSync: typeof setItemSync;
declare namespace mod {
export {
mod_clear as clear,
mod_clearSync as clearSync,
mod_getItem as getItem,
mod_getItemSync as getItemSync,
mod_getLength as getLength,
mod_getLengthSync as getLengthSync,
mod_hasItem as hasItem,
mod_hasItemSync as hasItemSync,
mod_removeItem as removeItem,
mod_removeItemSync as removeItemSync,
mod_setItem as setItem,
mod_setItemSync as setItemSync,
};
}
/**
* 类型工具:判断 API 是否符合 promisify 条件。
*/
type ValidAPI<T> = T extends (params: infer P) => infer R ? R extends void | Promise<any> ? P extends {
success?: any;
} | undefined ? true : P extends {
fail?: any;
} | undefined ? true : false : false : false;
/**
* 类型工具:提取成功回调参数类型。
*/
type SuccessType<T> = T extends (params: infer P) => any ? P extends {
success?: (res: infer S) => any;
} ? S : never : never;
/**
* 类型工具:提取失败回调参数类型。
*/
type FailType<T> = T extends (params: infer P) => any ? P extends {
fail?: (err: infer E) => any;
} ? E : never : never;
/**
* 将小游戏异步 API 转换为返回 `AsyncResult<T, E>` 的新函数,需要转换的 API 必须是接受可选 `success` 和 `fail` 回调的函数,并且其返回值必须是 `void` 或 `Promise`。
*
* 其中 `T` 为 `success` 回调的参数类型,`E` 为 `fail` 回调的参数类型。
*
* @param api - 小游戏异步 API。
* @returns 返回一个新的函数,该函数返回 `AsyncResult<T, E>`。
*/
declare function promisifyWithResult<F extends (...args: any[]) => any, T = SuccessType<F>, E = FailType<F>>(api: F): ValidAPI<F> extends true ? (...args: Parameters<F>) => AsyncResult<T, E> : never;
/**
* 将小游戏失败回调的结果转换为 `Error` 类型。
*
* 如果是异步 API 的 `fail` 回调返回的结果通常是 `WechatMinigame.GeneralCallbackResult` 或者变体类型,
* 如果是同步 API throw 的异常通常是一个类似 `Error` 的类型。
* @param err - 小游戏错误对象。
* @returns 转换后的 `Error` 对象。
*/
declare function miniGameFailureToError(err: WechatMinigame.GeneralCallbackResult | Error): Error;
/**
* 将错误对象转换为 IOResult 类型。
* @typeParam T - Result 的 Ok 类型。
* @param err - 错误对象。
* @returns 转换后的 IOResult 对象。
*/
declare function miniGameFailureToResult<T>(err: WechatMinigame.GeneralCallbackResult): IOResult<T>;
/**
* 执行同步函数,预期异常都是 `WechatMinigame.GeneralCallbackResult`。
* @param op - 需要执行的同步函数。
* @returns IOResult。
*/
declare function tryGeneralSyncOp<T>(op: () => T): IOResult<T>;
/**
* 执行异步函数,预期异常都是 `WechatMinigame.GeneralCallbackResult`。
* @param op - 需要执行的异步函数。
* @returns AsyncIOResult。
*/
declare function tryGeneralAsyncOp<T>(op: () => Promise<T>): AsyncIOResult<T>;
/**
* 执行同步函数,预期异常都是 `DOMException`。
* @param op - 需要执行的同步函数。
* @returns IOResult。
*/
declare function tryDOMSyncOp<T>(op: () => T): IOResult<T>;
/**
* 执行异步函数,预期异常都是 `DOMException`。
* @param op - 需要执行的异步函数。
* @returns AsyncIOResult。
*/
declare function tryDOMAsyncOp<T>(op: () => Promise<T>): AsyncIOResult<T>;
/**
* 将 BufferSource 转换为 Uint8Array。
* @param data - 需要转换的 BufferSource。
* @returns Uint8Array。
*/
declare function bufferSource2U8a(data: BufferSource): Uint8Array;
/**
* 将 BufferSource 转换为 ArrayBuffer。
* @param data - 需要转换的 BufferSource。
* @returns ArrayBuffer。
*/
declare function bufferSource2Ab(data: BufferSource): ArrayBuffer;
export { SocketReadyState, addErrorListener, addNetworkChangeListener, addResizeListener, addUnhandledrejectionListener, assertSafeSocketUrl, assertSafeUrl, assertString, mod$8 as audio, base64FromBuffer, base64ToBuffer, bufferSource2Ab, bufferSource2U8a, byteStringFromBuffer, byteStringToBuffer, mod$7 as clipboard, connectSocket, mod$5 as cryptos, decodeBase64, encodeBase64, fetchT, mod$4 as fs, getNetworkType, hexFromBuffer, mod$3 as image, mod$2 as lbs, miniGameFailureToError, miniGameFailureToResult, mod$1 as platform, promisifyWithResult, mod as storage, textDecode, textEncode, toByteString, tryDOMAsyncOp, tryDOMSyncOp, tryGeneralAsyncOp, tryGeneralSyncOp };
export type { DataSource, FailType, ISocket, MinaFetchInit, NetworkType, SocketListenerMap, SocketOptions, SuccessType, UnionFetchInit, ValidAPI };
//# sourceMappingURL=types.d.ts.map