minigame-std
Version:
Cross-platform standard library for WeChat minigame and web browsers with unified APIs for crypto, fs, fetch, storage, and more.
233 lines (228 loc) • 6.66 kB
TypeScript
import { AsyncIOResult } from 'happy-rusty';
/**
* 平台类型,Web 或者小游戏。
* @since 1.0.0
* @example
* ```ts
* import { platform, type TargetType } from 'minigame-std';
*
* const type: TargetType = platform.getTargetType();
* console.log(type); // 'minigame' 或 'web'
* ```
*/
type TargetType = 'minigame' | 'web';
/**
* 获取当前的平台类型。
* @returns 返回当前的运行环境类型,可能是 'minigame' 或 'web'。
* @since 1.0.0
* @example
* ```ts
* const type = getTargetType();
* console.log('当前平台:', type); // 'minigame' 或 'web'
* ```
*/
declare function getTargetType(): TargetType;
/**
* 判断当前是否在 Web 环境中。
* @returns 如果在 Web 环境中返回 true,否则返回 false。
* @since 1.0.0
* @example
* ```ts
* if (isWeb()) {
* console.log('当前在浏览器环境');
* }
* ```
*/
declare function isWeb(): boolean;
/**
* 判断当前是否在小游戏环境中。
* @returns 如果在小游戏环境中返回 true,否则返回 false。
* @since 1.0.0
* @example
* ```ts
* if (isMiniGame()) {
* console.log('当前在小游戏环境');
* }
* ```
*/
declare function isMiniGame(): boolean;
/**
* 获取设备信息。
* @returns 返回小游戏的设备信息对象。
* @since 1.0.0
* @example
* ```ts
* const info = getDeviceInfo();
* console.log('设备平台:', info.platform);
* console.log('设备品牌:', info.brand);
* console.log('设备型号:', info.model);
* ```
*/
declare function getDeviceInfo(): DeviceInfo;
/**
* 获取设备性能等级, web 环境返回 -2。
* @returns 返回设备性能等级。
* @since 1.10.0
* @example
* ```ts
* const result = await getDeviceBenchmarkLevel();
* if (result.isOk()) {
* const level = result.unwrap();
* if (level >= 30) {
* console.log('高性能设备');
* } else if (level >= 20) {
* console.log('中等性能设备');
* } else {
* console.log('低性能设备');
* }
* }
* ```
*/
declare function getDeviceBenchmarkLevel(): AsyncIOResult<number>;
/**
* 获取窗口信息。
* @returns 包含窗口和屏幕相关信息的对象。
* @since 1.7.0
* @example
* ```ts
* const info = getWindowInfo();
* console.log('窗口尺寸:', info.windowWidth, 'x', info.windowHeight);
* console.log('屏幕尺寸:', info.screenWidth, 'x', info.screenHeight);
* console.log('设备像素比:', info.pixelRatio);
* ```
*/
declare function getWindowInfo(): WechatMinigame.WindowInfo;
/**
* 平台类型。
* @since 1.0.0
* @example
* ```ts
* import { platform, type Platform } from 'minigame-std';
*
* const info = platform.getDeviceInfo();
* const devicePlatform: Platform = info.platform;
* console.log(devicePlatform); // 'ios' | 'android' | 'mac' | ...
* ```
*/
type Platform = 'ios' | 'android' | 'mac' | 'windows' | 'ohos' | 'ohos_pc' | 'devtools' | 'linux' | 'unknown';
/**
* 设备信息类型。
* 修正了 `memorySize` 的类型为 `number`(小游戏 API 实际返回数字,但官方类型定义错误地声明为 string)。
* @see https://github.com/wechat-miniprogram/minigame-api-typings/issues/27
* @since 1.0.0
* @example
* ```ts
* import { platform, type DeviceInfo } from 'minigame-std';
*
* const info: DeviceInfo = platform.getDeviceInfo();
* console.log('平台:', info.platform);
* console.log('内存:', info.memorySize, 'MB');
* ```
*/
type DeviceInfo = Omit<WechatMinigame.DeviceInfo, 'abi' | 'cpuType' | 'deviceAbi' | 'memorySize' | 'platform'> & {
abi?: string;
cpuType?: string;
deviceAbi?: string;
/** 设备内存大小,单位为 MB */
memorySize: number;
/** 设备平台 */
platform: Platform;
};
/**
* 判断当前是否在小游戏的运行时环境中。
* @returns 如果在小游戏的运行时环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameRuntime()) {
* console.log('在小游戏真机环境中');
* }
* ```
*/
declare function isMiniGameRuntime(): boolean;
/**
* 判断当前是否在小游戏的开发者工具中。
* @returns 如果在小游戏的开发者工具中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameDevtools()) {
* console.log('在开发者工具中');
* }
* ```
*/
declare function isMiniGameDevtools(): boolean;
/**
* 判断当前是否在小游戏的 iOS 环境中。
* @returns 如果在小游戏的 iOS 环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameIOS()) {
* console.log('在 iOS 设备上运行');
* }
* ```
*/
declare function isMiniGameIOS(): boolean;
/**
* 判断当前是否在小游戏的 Android 环境中。
* @returns 如果在小游戏的 Android 环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameAndroid()) {
* console.log('在 Android 设备上运行');
* }
* ```
*/
declare function isMiniGameAndroid(): boolean;
/**
* 判断当前是否在小游戏的 Windows 环境中。
* @returns 如果在小游戏的 Windows 环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameWin()) {
* console.log('在 Windows 设备上运行');
* }
* ```
*/
declare function isMiniGameWin(): boolean;
/**
* 判断当前是否在小游戏的 Mac 环境中。
* @returns 如果在小游戏的 Mac 环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameMac()) {
* console.log('在 Mac 设备上运行');
* }
* ```
*/
declare function isMiniGameMac(): boolean;
/**
* 判断当前是否在小游戏的 HarmonyOS 环境中。
* @returns 如果在小游戏的 HarmonyOS 环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameHarmonyOS()) {
* console.log('在 HarmonyOS 设备上运行');
* }
* ```
*/
declare function isMiniGameHarmonyOS(): boolean;
/**
* 判断当前是否在小游戏的 HarmonyOS PC 环境中。
* @returns 如果在小游戏的 HarmonyOS PC 环境中返回 true,否则返回 false。
* @since 2.4.0
* @example
* ```ts
* if (isMiniGameHarmonyPC()) {
* console.log('在 HarmonyOS PC 设备上运行');
* }
* ```
*/
declare function isMiniGameHarmonyPC(): boolean;
export { getDeviceBenchmarkLevel, getDeviceInfo, getTargetType, getWindowInfo, isMiniGame, isMiniGameAndroid, isMiniGameDevtools, isMiniGameHarmonyOS, isMiniGameHarmonyPC, isMiniGameIOS, isMiniGameMac, isMiniGameRuntime, isMiniGameWin, isWeb };
export type { DeviceInfo, Platform, TargetType };