import { MethodName, MethodNameWithVersionedParams, MethodVersionedParams } from '@telegram-apps/bridge'; import { Computed } from '@telegram-apps/signals'; import { If, IsNever } from '@telegram-apps/toolkit'; import { AnyFn } from '../../types.js'; export type CustomSupportValidatorFn = () => string | undefined; export type IsSupportedType = MethodName | CustomSupportValidatorFn | (MethodName | CustomSupportValidatorFn)[] | { any: (MethodName | CustomSupportValidatorFn)[]; }; /** * A map where the key is a method name with versioned parameters, and the value is a tuple * containing the method and parameter names. The third tuple value is a function accepting * the wrapped function arguments and returning true if support check must be applied. */ export type Supports = Record, shouldCheck: (...args: Parameters) => boolean ]; }[MethodNameWithVersionedParams]>; export type IfAvailableFnResult = [called: true, data: Data] | [called: false]; export type SafeWrapped> = Fn & { /** * The signal returning `true` if the function is available in the current environment and * conditions. * * To be more accurate, the method checks the following: * 1. The current environment is Telegram Mini Apps. * 2. The SDK package is initialized. * 3. If passed, the `isSupported` signal returns true. * 4. If passed, the `isMounted` signal returns true. * * *You should use this function when possible because it provides must-have code security * mechanisms and makes a developer sure that he is using the package properly.* * * @returns True if the function is available in the current environment. * @example * if (showBackButton.isAvailable()) { * showBackButton(); * } */ isAvailable: Computed; /** * Calls the function only in case it is available. * * It uses the `isAvailable` internally to check if the function is supported. * @example * showBackButton.ifAvailable(); */ ifAvailable(...args: Parameters): IfAvailableFnResult>; } & If; }, {}> & If, {}, { /** * A map where the key is the function-specific option name and value is a signal indicating * if it is supported by the current environment. * @example * if (setHeaderColor.isAvailable()) { * if (setHeaderColor.supports.rgb()) { * setHeaderColor('#ffaabb'); * } else { * setHeaderColor('bg_color'); * } * } */ supports: Record>; }>; export interface WrapSafeOptions { /** * The component name owning the wrapped function. */ component?: string; /** * Signal returning true if the owning component is mounted. */ isMounted?: () => boolean; /** * Signal returning true if the owning component is mounting. */ isMounting?: () => boolean; /** * Value determining if the function is supported by the current environment. */ isSupported?: IsSupportedType; /** * A map where the key is a method name with versioned parameters, and the value is a tuple * containing the method and parameter names. The third tuple value is a function accepting * the wrapped function arguments and returning true if support check must be applied. */ supports?: Supports; } /** * Wraps the function enhancing it with the useful utilities described in the SafeWrapped type. * @see SafeWrapped * @param method - method name * @param fn - wrapped function */ export declare function wrapSafe(method: string, fn: Fn): SafeWrapped; /** * Wraps the function enhancing it with the useful utilities described in the SafeWrapped type. * @see SafeWrapped * @param method - method name * @param fn - wrapped function * @param options - additional options */ export declare function wrapSafe>(method: string, fn: Fn, options: O): SafeWrapped;