import DeviceInfo from 'react-native-device-info' import { Session } from './session' const brand = DeviceInfo.getBrand() const model = DeviceInfo.getModel() const systemName = DeviceInfo.getSystemName() const systemVersion = DeviceInfo.getSystemVersion() const readableVersion = DeviceInfo.getReadableVersion() const appName = DeviceInfo.getApplicationName() type Graph = 'churchcenter' | 'planningcenter' export class Uri { session: Session graph: Graph = 'planningcenter' app?: string constructor({ session, app, graph }: { session: Session; app?: string; graph?: Graph }) { this.session = session this.app = app this.graph = graph || 'planningcenter' } get schema() { return 'https' } get host() { return `${this.subdomain}.${this.domain}.${this.tld}` } get subdomain() { switch (this.env) { case 'staging': return this.isChurchCenter ? 'api.staging' : 'api-staging' default: return 'api' } } get domain(): 'pco' | 'planningcenteronline' | 'churchcenter' { if (this.isChurchCenter) { return 'churchcenter' } return this.env === 'development' ? 'pco' : 'planningcenteronline' } get tld() { switch (this.env) { case 'development': return 'test' default: return 'com' } } get isChurchCenter() { return this.session.isChurchCenterToken } get env() { return this.session?.env || 'production' } get baseUrl() { return `${this.schema}://${this.host}` } get directory() { return this.app ? `/${this.app}/v2` : '' } get headers() { return { 'User-Agent': `${appName}/${readableVersion} (${brand}, ${model}, ${systemName}, ${systemVersion})`, Authorization: `Bearer ${this.session.token?.access_token}`, } } appUrl = (path: string) => { if (path.startsWith(`${this.schema}://`)) return path return `${this.baseUrl}${this.directory}${path || '/'}` } api = (path: string) => { if (path.startsWith(`${this.schema}://`)) return path return `${this.baseUrl}${path || '/'}` } }