import RookSdk from '../NativeRookSdk'; import { isModuleReady } from '../utils/isModuleReady'; import { Platform } from 'react-native'; import { useRookSyncGateContext } from '../context/RookSyncGateContext'; import type { APIDataSource } from '../types/DataSource'; import type { AuthorizedSource, DataSourceAuthorizer, DataSourceProps, } from '../types/AuthorizedSources'; /** * The `useRookDataSources` hook provides functionality to interact with data sources in Rook, * including retrieving available data sources, presenting a connections page, and revoking user * authorization for a specific data source. * @returns An object is being returned from the `useRookDataSources` hook to allow the user to access to the sources API */ export const useRookAPISources = () => { const { state: { ready }, } = useRookSyncGateContext(); /** * The function `revokeDataSource` revokes user authorization for the specified data source. * @param {DataSourceType} type - The `type` parameter in the `revokeDataSource` function represents * the type of data source that you want to revoke access to. It is of type `DataSourceType`. * @returns A Promise is being returned from the `revokeDataSource` function. */ const revokeDataSource = ( userid: string, type: APIDataSource ): Promise => { isModuleReady(ready); return RookSdk.revokeDataSource(userid, type); }; /** * Retrieves the authorization status for a specific data source linked to a given user. * If the user is not authorized, an authorization URL is provided to start the process. * If the user is already authorized, the response will indicate "authorized": true and no authorization URL will be * returned. The redirect_url parameter can be used to specify where the user should be redirected * after completing the authorization. * @returns The function `getDataSourceAuthorizer` is returning a `DataSourceAuthorizer` object after * parsing the response from `rookSync.current.getDataSourceAuthorizer()` as JSON. */ const getDataSourceAuthorizer = async ( props: DataSourceProps ): Promise => { const response = await RookSdk.getDataSourceAuthorizer(props); if (Platform.OS === 'android') return JSON.parse(response as any) as DataSourceAuthorizer; return response; }; /** * The function `getAuthorizedDataSources` returns authorized data sources after checking ROOK services * in case of apple health, health connect and android this only determines if the user * is connected not if the user granted permissions. * @returns The function `getAuthorizedDataSources` is returning a Promise that will resolve to the * result of calling `rookSync.current.getAuthorizedDataSources()`. */ const getAuthorizedDataSourcesV2 = async ( userid: string ): Promise => { isModuleReady(ready); if (Platform.OS !== 'android') return RookSdk.getAuthorizedDataSourcesV2(userid); const response = await RookSdk.getAuthorizedDataSourcesV2(userid); return JSON.parse(response as any) as AuthorizedSource[]; }; return { ready, revokeDataSource, getDataSourceAuthorizer, getAuthorizedDataSourcesV2, }; };