JSPM

  • Created
  • Published
  • Downloads 59
  • Score
    100M100P100Q70233F
  • License MIT

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

Package Exports

  • react-native-healthkit-bridge
  • react-native-healthkit-bridge/src/index.ts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-native-healthkit-bridge) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

React Native HealthKit Bridge

A complete React Native library for Apple HealthKit integration, providing access to health and fitness data on iOS devices.

🌍 Multi-Language Documentation

Documentation is available in three languages to serve a global community of developers:

πŸ‡ΊπŸ‡Έ English (en)

πŸ‡§πŸ‡· PortuguΓͺs (pt-br)

πŸ‡ͺπŸ‡Έ EspaΓ±ol (es)

πŸš€ Key Features

  • βœ… Complete Type Safety - Full TypeScript support with mapped types
  • βœ… 8 Authorization Methods - From basic to advanced smart authorization
  • βœ… 120+ Data Types - Comprehensive HealthKit coverage
  • βœ… React Hooks - 6 specialized hooks for easy integration
  • βœ… Advanced Queries - Hours, days, ranges, and smart data detection
  • βœ… Optimized Performance - Efficient queries and low memory usage
  • βœ… Robust Error Handling - Clear messages and automatic recovery
  • βœ… Autolinking - Automatic installation without manual configuration

πŸ“¦ Installation

npm install react-native-healthkit-bridge
# or
yarn add react-native-healthkit-bridge

πŸ”§ iOS Configuration

Info.plist

<key>NSHealthShareUsageDescription</key>
<string>This app needs to access health data to provide personalized insights and track your fitness progress.</string>
<key>NSHealthUpdateUsageDescription</key>
<string>This app needs permission to save health data to help you track your fitness activities.</string>

πŸ’» Basic Usage

Traditional Bridge Usage

import { HealthKitBridge, QuantityTypeIdentifier, getQuantityUnit } from 'react-native-healthkit-bridge';

const bridge = new HealthKitBridge();

// Request authorization
const isAuthorized = await bridge.requestFullAuthorization();

// Get heart rate data
const heartRate = await bridge.getQuantitySamplesForDays(
  QuantityTypeIdentifier.HeartRate,
  getQuantityUnit(QuantityTypeIdentifier.HeartRate),
  1
);

React Hooks Usage

import { useHealthKitQuantity, useHealthKitAuthorization } from 'react-native-healthkit-bridge';

function HeartRateMonitor() {
  const { requestAuthorization, isAuthorized } = useHealthKitAuthorization();
  const { data: heartRate, loading, error } = useHealthKitQuantity(
    'HKQuantityTypeIdentifierHeartRate',
    'bpm'
  );

  useEffect(() => {
    requestAuthorization();
  }, []);

  if (loading) return <Text>Loading...</Text>;
  if (error) return <Text>Error: {error}</Text>;
  
  return <Text>Heart Rate: {heartRate?.[0]?.value} bpm</Text>;
}

🎯 Use Cases

πŸƒβ€β™‚οΈ Fitness App

// Get steps for the last 7 days
const steps = await bridge.getQuantitySamplesForDays(
  QuantityTypeIdentifier.StepCount,
  getQuantityUnit(QuantityTypeIdentifier.StepCount),
  7
);

// Get data for specific hours
const hourlySteps = await bridge.getQuantitySamplesForHours(
  QuantityTypeIdentifier.StepCount,
  getQuantityUnit(QuantityTypeIdentifier.StepCount),
  24
);

πŸ₯ Medical App

// Smart authorization - only request what's needed
const result = await bridge.requestSmartAuthorization([
  QuantityTypeIdentifier.HeartRate,
  CategoryTypeIdentifier.HighHeartRateEvent
]);

// Ensure specific data type authorization
const authResult = await bridge.ensureDataTypeAuthorization(
  QuantityTypeIdentifier.BloodGlucose
);

πŸ“Š Health Dashboard

// Get the last day with data
const lastDayData = await bridge.getQuantitySamplesForLastDayWithData(
  QuantityTypeIdentifier.HeartRate,
  getQuantityUnit(QuantityTypeIdentifier.HeartRate)
);

// Query by date range
const rangeData = await bridge.getQuantitySamplesForRange(
  QuantityTypeIdentifier.HeartRate,
  getQuantityUnit(QuantityTypeIdentifier.HeartRate),
  '2024-01-01T00:00:00Z',
  '2024-01-31T23:59:59Z'
);

πŸ”§ Available React Hooks

Core Hooks

  • useHealthKitQuantity() - For quantitative data (steps, heart rate, etc.)
  • useHealthKitCategory() - For categorical data (sleep, symptoms, etc.)
  • useHealthKitWorkouts() - For workout data
  • useHealthKitTypes() - For available data types
  • useHealthKitProvider() - For provider information
  • useHealthKitAuthorization() - For authorization management

Authorization Hook Example

import { useHealthKitAuthorization } from 'react-native-healthkit-bridge';

function HealthKitManager() {
  const {
    requestAuthorization,
    requestSmartAuthorization,
    ensureDataTypeAuthorization,
    isAuthorized,
    authorizationStatus,
    loading,
    error
  } = useHealthKitAuthorization();

  const handleSmartAuth = async () => {
    const result = await requestSmartAuthorization([
      QuantityTypeIdentifier.StepCount,
      QuantityTypeIdentifier.HeartRate
    ]);
  
    console.log('Authorized:', result.authorizedTypes);
    console.log('Denied:', result.deniedTypes);
  };

  return (
    <View>
      <Button title="Request Authorization" onPress={requestAuthorization} />
      <Button title="Smart Authorization" onPress={handleSmartAuth} />
      <Text>Status: {isAuthorized ? 'Authorized' : 'Not Authorized'}</Text>
    </View>
  );
}

πŸ“Š Performance Metrics

Before Improvements

  • Type Safety: 0%
  • Error Handling: Basic
  • Code Reusability: Low
  • Developer Experience: Medium

After Improvements (v1.9.0)

  • Type Safety: 95%
  • Error Handling: Robust
  • Code Reusability: High
  • Developer Experience: Easy
  • React Integration: Native hooks support
  • Query Flexibility: Multiple time-based queries

πŸ”§ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    React Native App                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”‚
β”‚  β”‚   TypeScript    β”‚    β”‚   React Hooks   β”‚               β”‚
β”‚  β”‚   Interface     β”‚    β”‚   (6 hooks)     β”‚               β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                    JavaScript Bridge                       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”‚
β”‚  β”‚   TurboModule   β”‚    β”‚   NativeModule  β”‚               β”‚
β”‚  β”‚   (New Arch)    β”‚    β”‚   (Legacy)      β”‚               β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                    iOS Native Layer                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”‚
β”‚  β”‚ HealthKitBridge β”‚    β”‚   HKHealthStore β”‚               β”‚
β”‚  β”‚   (Objective-C) β”‚    β”‚   (Apple SDK)   β”‚               β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                    Apple HealthKit                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“š Additional Resources

πŸ”— External Documentation

πŸ“– Code Examples

🀝 Contributing

  1. Fork the repository
  2. Create a branch for your feature
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

πŸ“ Code Guidelines

  • Follow TypeScript best practices
  • Use meaningful names for variables and functions
  • Add JSDoc comments for public APIs
  • Ensure proper error handling
  • Write unit tests for all new features

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Apple for providing the HealthKit framework
  • React Native community for excellent documentation and tools
  • Contributors and maintainers of related open-source projects

This library is maintained by the community. If you find issues or have suggestions, please contribute!