UNPKG

react-native-healthkit-bridge

Version:

A comprehensive React Native bridge for Apple HealthKit with TypeScript support, advanced authorization, and flexible data queries

56 lines (55 loc) 2.59 kB
import { HealthKitBridge, QuantityTypeIdentifier, CategoryTypeIdentifier, HealthKitUnit, HEALTHKIT_IDENTIFIERS, HEALTHKIT_UNITS } from '../index'; // Example usage with specific TypeScript types export async function exampleUsage() { const bridge = new HealthKitBridge(); // Check availability const isAvailable = await bridge.checkAvailability(); if (!isAvailable) { console.log('HealthKit not available'); return; } // Request authorization with specific types const isAuthorized = await bridge.requestSelectiveAuthorization([ QuantityTypeIdentifier.StepCount, QuantityTypeIdentifier.HeartRate, CategoryTypeIdentifier.SleepAnalysis ], []); if (!isAuthorized) { console.log('Permission denied'); return; } // Fetch data with specific types - TypeScript will suggest the correct units! const steps = await bridge.getQuantitySamplesForDays(QuantityTypeIdentifier.StepCount, HealthKitUnit.count, // ✅ Autocomplete suggests 'count' 1); const heartRate = await bridge.getQuantitySamplesForDays(QuantityTypeIdentifier.HeartRate, HealthKitUnit.beatsPerMinute, // ✅ Autocomplete suggests 'bpm' 1); const sleep = await bridge.getCategorySamplesForDays(CategoryTypeIdentifier.SleepAnalysis, // ✅ Autocomplete suggests categorical types 1); console.log('Steps:', steps); console.log('Heart rate:', heartRate); console.log('Sleep:', sleep); } // Example using constants to make it even easier export async function exampleWithConstants() { const bridge = new HealthKitBridge(); const heartRateData = await bridge.getQuantitySamplesForDays(HEALTHKIT_IDENTIFIERS.HeartRate, // ✅ Easier than typing the enum HEALTHKIT_UNITS.beatsPerMinute, // ✅ Easier than typing the enum 7 // last 7 days ); console.log('Heart rate history:', heartRateData); } // Example range query with specific types export async function exampleRangeQuery() { const bridge = new HealthKitBridge(); const startDate = '2024-01-01T00:00:00.000Z'; const endDate = '2024-01-31T23:59:59.999Z'; // TypeScript ensures the unit is correct for the type const stepsData = await bridge.getQuantitySamplesForRange(QuantityTypeIdentifier.StepCount, HealthKitUnit.count, // ✅ TypeScript ensures it's the correct unit startDate, endDate); console.log('January steps:', stepsData); } // TypeScript type advantages: // • Autocomplete for identifiers and units // • Compile-time type checking // • Prevention of typos // • Inline documentation