import axios from 'axios'; import {NativeModules, Platform} from 'react-native'; const buildAndroidUserAgent = () => { const platformConstants = Platform.constants as { Brand?: string; Model?: string; }; const brand = platformConstants.Brand ?? 'UnknownBrand'; const model = platformConstants.Model ?? 'UnknownModel'; const androidVersion = String(Platform.Version ?? 'UnknownVersion'); return `FacekiReactNativeSDK (Android ${androidVersion}; ${brand} ${model})`; }; const buildIOSAgentContext = () => { const platformConstants = Platform.constants as { osVersion?: string; systemName?: string; interfaceIdiom?: string; }; const settings = (NativeModules.SettingsManager?.settings ?? {}) as { CFBundleIdentifier?: string; CFBundleName?: string; CFBundleDisplayName?: string; CFBundleShortVersionString?: string; CFBundleVersion?: string; }; const appName = settings.CFBundleDisplayName ?? settings.CFBundleName ?? 'UnknownApp'; const appVersion = settings.CFBundleShortVersionString ?? settings.CFBundleVersion ?? '1'; const bundleId = settings.CFBundleIdentifier ?? 'unknown.bundle'; const systemName = platformConstants.systemName ?? 'iOS'; const iosVersion = platformConstants.osVersion ?? String(Platform.Version ?? 'UnknownVersion'); const deviceType = platformConstants.interfaceIdiom ?? 'UnknownDevice'; return `${appName}/${appVersion} (${bundleId}; ${systemName} ${iosVersion}; ${deviceType})`; }; const api = axios.create({ baseURL: 'https://sdk.faceki.com', headers: { 'Content-Type': 'application/json', }, }); api.interceptors.request.use(config => { config.headers = config.headers ?? {}; if (Platform.OS === 'android') { config.headers['User-Agent'] = buildAndroidUserAgent(); return config; } if (Platform.OS === 'ios') { const iosContext = buildIOSAgentContext(); const existingUserAgent = config.headers['User-Agent'] ?? config.headers['user-agent']; if (typeof existingUserAgent === 'string' && existingUserAgent.trim().length > 0) { config.headers['User-Agent'] = `${existingUserAgent} ${iosContext}`; return config; } // Keep CFNetwork/Darwin User-Agent untouched when not directly available in JS. config.headers['X-Faceki-Client-Context'] = iosContext; } return config; }); export default api;