react-native-healthkit-bridge
Version:
A comprehensive React Native bridge for Apple HealthKit with TypeScript support, advanced authorization, and flexible data queries
36 lines (29 loc) • 1.36 kB
text/typescript
import { HealthKitRepository } from '../repositories/HealthKitRepository';
import { HealthKitIdentifier } from '../value-objects/HealthKitIdentifier';
export class RequestAuthorizationUseCase {
constructor(private readonly healthKitRepository: HealthKitRepository) {}
async execute(): Promise<boolean> {
try {
if (!this.healthKitRepository.isAvailable()) {
throw new Error('HealthKit is not available on this device');
}
return await this.healthKitRepository.requestAuthorization();
} catch (error) {
throw new Error(`Failed to request authorization: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
async executeSelective(identifiers: string[]): Promise<boolean> {
try {
if (!this.healthKitRepository.isAvailable()) {
throw new Error('HealthKit is not available on this device');
}
if (!identifiers || identifiers.length === 0) {
throw new Error('At least one identifier must be provided');
}
const healthKitIdentifiers = identifiers.map(id => HealthKitIdentifier.create(id));
return await this.healthKitRepository.requestSelectiveAuthorization(healthKitIdentifiers);
} catch (error) {
throw new Error(`Failed to request selective authorization: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
}