import { NativeModules, Platform } from 'react-native'; import type { AgeDeclarationModuleType, AgeRangeResult, SetThresholdsResult, } from './types'; const LINKING_ERROR = `The package 'react-native-age-declaration' doesn't seem to be linked. Make sure: \n\n` + Platform.select({ ios: "- Run 'pod install' in the ios directory\n", default: '' }) + Platform.select({ android: "- You have added the package to your app's build.gradle dependencies\n" + "- You have added AgeDeclarationPackage to your MainApplication.java\n", default: '', }) + '- You rebuilt the app after installing the package\n'; const AgeDeclarationModule: AgeDeclarationModuleType = NativeModules.AgeDeclarationModule ? NativeModules.AgeDeclarationModule : new Proxy( {}, { get() { throw new Error(LINKING_ERROR); }, } ) as AgeDeclarationModuleType; /** * Sets the age range thresholds for the application * * **Requirements:** * - First threshold is mandatory * - Values must be 1-18 (inclusive) * - Must be in ascending order * - Minimum 2-year separation between values * * @param thresholds - Array of age thresholds * @returns Promise that resolves when thresholds are set * * @example * ```typescript * import AgeDeclaration from 'react-native-age-declaration'; * * // Set age thresholds at app startup * await AgeDeclaration.setAgeThresholds([13, 15, 18]); * ``` */ export function setAgeThresholds(thresholds: number[]): Promise { if (!Array.isArray(thresholds)) { return Promise.reject(new Error('Thresholds must be an array of numbers')); } if (thresholds.length === 0) { return Promise.reject(new Error('At least one threshold is required')); } // Validate that all values are numbers if (!thresholds.every((t) => typeof t === 'number' && !isNaN(t))) { return Promise.reject(new Error('All thresholds must be valid numbers')); } return AgeDeclarationModule.setAgeThresholds(thresholds); } /** * Requests age range information from the platform * * **Important:** * - Must call `setAgeThresholds()` first * - iOS: Requires iOS 26.0+ with DeclaredAgeRange framework * - Android: Requires Android 15+ with Google Play Services * * @returns Promise that resolves with age verification result * * @example * ```typescript * import AgeDeclaration from 'react-native-age-declaration'; * * const result = await AgeDeclaration.requestAgeRange(); * * switch (result.status) { * case 'verified': * console.log('User meets age requirements'); * break; * case 'not_verified': * console.log('User does not meet age requirements'); * break; * case 'unknown': * console.log('Age verification status unknown'); * break; * case 'not_available_yet': * console.log('API not available on this device/OS version'); * break; * } * ``` */ export function requestAgeRange(): Promise { return AgeDeclarationModule.requestAgeRange(); } /** * Checks if age range API is supported on this device * * @returns Promise that resolves to true if supported * * @example * ```typescript * import AgeDeclaration from 'react-native-age-declaration'; * * const supported = await AgeDeclaration.isSupported(); * * if (supported) { * await AgeDeclaration.setAgeThresholds([13, 18]); * const result = await AgeDeclaration.requestAgeRange(); * } else { * console.log('Age verification not supported on this device'); * } * ``` */ export function isSupported(): Promise { return AgeDeclarationModule.isSupported(); } // Default export export default { setAgeThresholds, requestAgeRange, isSupported, }; // Export types export type { AgeRangeResult, SetThresholdsResult } from './types';