94 lines
2.8 KiB
TypeScript
94 lines
2.8 KiB
TypeScript
import { createTelemetryClient } from '@bytelyst/telemetry-client';
|
|
import { createFeatureFlagClient } from '@bytelyst/feature-flag-client';
|
|
import { createKillSwitchClient } from '@bytelyst/kill-switch-client';
|
|
import { createBlobClient } from '@bytelyst/blob-client';
|
|
import { DiagnosticsClient } from '@bytelyst/diagnostics-client';
|
|
import { API_CONFIG, PRODUCT_ID } from '../api/config';
|
|
import { mmkvStorage } from '../store/mmkv-storage';
|
|
import { APP_VERSION, BUILD_NUMBER, OS_VERSION } from './app-metadata';
|
|
|
|
function getAccessToken(): string | null {
|
|
return mmkvStorage.getItem(`${PRODUCT_ID}_access_token`);
|
|
}
|
|
|
|
export const telemetryClient = createTelemetryClient({
|
|
productId: PRODUCT_ID,
|
|
baseUrl: API_CONFIG.platformBaseUrl,
|
|
endpoint: '/telemetry/events',
|
|
platform: 'mobile',
|
|
channel: 'notelett_mobile',
|
|
transport: 'fetch',
|
|
appVersion: APP_VERSION,
|
|
buildNumber: BUILD_NUMBER,
|
|
releaseChannel: 'dev',
|
|
osFamily: OS_VERSION,
|
|
});
|
|
|
|
export const featureFlagClient = createFeatureFlagClient({
|
|
baseUrl: API_CONFIG.platformBaseUrl,
|
|
productId: PRODUCT_ID,
|
|
platform: 'mobile',
|
|
pollIntervalMs: 5 * 60 * 1000,
|
|
getAccessToken,
|
|
storage: {
|
|
getItem: (key: string) => mmkvStorage.getItem(key),
|
|
setItem: (key: string, value: string) => mmkvStorage.setItem(key, value),
|
|
},
|
|
});
|
|
|
|
export const killSwitchClient = createKillSwitchClient({
|
|
baseUrl: API_CONFIG.platformBaseUrl,
|
|
productId: PRODUCT_ID,
|
|
platform: 'mobile',
|
|
});
|
|
|
|
export const blobClient = createBlobClient({
|
|
baseUrl: API_CONFIG.platformBaseUrl,
|
|
productId: PRODUCT_ID,
|
|
getAccessToken,
|
|
});
|
|
|
|
export function getDiagnosticsClient() {
|
|
return DiagnosticsClient.getInstance({
|
|
productId: PRODUCT_ID,
|
|
serverUrl: API_CONFIG.platformBaseUrl,
|
|
platform: 'mobile',
|
|
channel: 'notelett_mobile',
|
|
anonymousInstallId: mmkvStorage.getItem(`${PRODUCT_ID}_install_id`) ?? 'unknown',
|
|
osFamily: OS_VERSION,
|
|
appVersion: APP_VERSION,
|
|
buildNumber: BUILD_NUMBER,
|
|
releaseChannel: 'dev',
|
|
getAuthToken: () => getAccessToken() ?? '',
|
|
});
|
|
}
|
|
|
|
let platformInitialized = false;
|
|
|
|
export async function initPlatform(): Promise<void> {
|
|
if (platformInitialized) return;
|
|
platformInitialized = true;
|
|
|
|
telemetryClient.init();
|
|
telemetryClient.trackEvent('info', 'app_shell', 'mobile_app_initialized');
|
|
|
|
await featureFlagClient.init().catch(() => {
|
|
// Feature flags are best-effort
|
|
});
|
|
}
|
|
|
|
export function isFeatureEnabled(key: string): boolean {
|
|
return featureFlagClient.isEnabled(key);
|
|
}
|
|
|
|
export function getFeatureValue<T = boolean | string | number | Record<string, unknown>>(
|
|
key: string,
|
|
defaultValue: T,
|
|
): T {
|
|
return featureFlagClient.getValue(key, defaultValue);
|
|
}
|
|
|
|
export async function checkKillSwitch(): Promise<{ disabled: boolean; message: string | null }> {
|
|
return killSwitchClient.check();
|
|
}
|