import log from 'loglevel' import { getPlatform, Platform } from '../platform' import { ApisauceInstance } from '../services/api/apisauce' export enum PLATFORM_SERVICES { FILES = 'fileService', CONNECTIVITY = 'connectivityService', OFFLINE = 'offlineService', NOTIFICATIONS = 'notificationService', LOCATION = 'locationService', } export interface IPlatformService { api: ApisauceInstance } export const resolveService = async ( service: PLATFORM_SERVICES, api: ApisauceInstance, ): Promise => { const platform = getPlatform() try { let provider if (platform === Platform.BROWSER) { if (service === PLATFORM_SERVICES.FILES) { provider = (await import('./browser/fileService.browser')).default } if (service === PLATFORM_SERVICES.CONNECTIVITY) { provider = (await import('./browser/connectivityService.browser')) .default } } if (platform === Platform.NODE) { if (service === PLATFORM_SERVICES.FILES) { provider = (await import('./node/fileService.node')).default } if (service === PLATFORM_SERVICES.CONNECTIVITY) { provider = (await import('./node/connectivityService.node')).default } } // const provider = require(`./${platform}/${service}.${platform}`).default // log.debug(`ResolveService: ${service} loaded for ${platform} platform.`) // return new provider(api) as T if (provider) { return (new provider(api) as unknown) as T } return } catch (e) { log.debug( `ResolveService: ${service} is not available for ${platform} platform.`, ) return } }