import { isObject, isString, compareVersion, isNative } from '@jolibox/common'; import { getCanIUseConfig, get } from '../utils/can-i-use'; const __VERSION__ = '__JOLIBOX_LOCAL_SDK_VERSION__'; // mock /** * @public * Checks if a specific API, component, or capability is available in the current environment. * @param schema - The schema string to check (e.g., "component.button", "api.request"). * @returns True if available, false otherwise. Returns false if the underlying command execution fails. * * @example * ```typescript * // Check if a basic API is available * if (canIUse('api.request')) { * console.log('Request API is available.'); * } else { * console.log('Request API is not available.'); * } * * // Check for a component * if (canIUse('component.button')) { * console.log('Button component is supported.'); * } * * // Check for a specific method or property on an API * // The schema format "ads:showInterstitialAd" is a common pattern for such checks. * if (canIUse('ads:showInterstitialAd')) { * console.log('Showing interstitial ads is supported.'); * } * ``` */ export function canIUse(schema: string): boolean { const [name, ...rest] = schema.split(':'); const api = getCanIUseConfig(isNative() ? 'native' : 'h5', name); if (!api) return false; const apiVersion = api['version']; if (typeof apiVersion !== 'string') return false; if (compareVersion(__VERSION__, apiVersion, '<')) return false; if (rest.length === 0) return true; return checkRest(api, rest) || checkRest(api['properties'], rest); } function checkRest(info: unknown, paths: string[]) { if (!info) return false; const v = get(info as Record, paths); if (!v) return false; if (isObject(v)) return true; if (isString(v)) return compareVersion(__VERSION__, v, '>='); return false; }