import SDK_VERSION from './version'; import type { RadarError } from './errors'; import type { RadarOptions } from './types'; /** global SDK configuration singleton */ class Config { /** current SDK options */ static options: RadarOptions; /** registered error callback, if any */ static errorCallback: ((error: RadarError) => void) | null = null; /** default option values applied during initialization */ static defaultOptions = { live: false, logLevel: 'debug', host: 'https://api-server-dev-binh.use1.radar-staging.com', version: 'v1', debug: true, }; /** store SDK options (called by Radar.initialize) */ public static setup(options: RadarOptions = {}) { Config.options = options; } /** get the current SDK options */ public static get(): RadarOptions { return Config.options || {}; } /** clear all SDK options and error callback */ public static clear() { Config.options = {}; Config.errorCallback = null; } /** register a callback invoked on SDK errors */ public static onError(callback: (error: RadarError) => void) { Config.errorCallback = callback; } /** dispatch an error to the registered callback */ public static sendError(error: any) { if (Config.errorCallback && error) { Config.errorCallback(error); } } /** build standard Radar request headers (Authorization, Device-Type, SDK-Version). * Callers must ensure credentials are set before calling (e.g. via Radar.initialize). * */ static getDefaultHeaders(): Record { const { publishableKey, authToken, getRequestHeaders: getHeaders } = Config.get(); const headers: Record = { 'X-Radar-Device-Type': 'Web', 'X-Radar-SDK-Version': SDK_VERSION, }; if (authToken) { headers.Authorization = `Bearer ${authToken}`; } else if (publishableKey) { headers.Authorization = publishableKey; } if (typeof getHeaders === 'function') { Object.assign(headers, getHeaders()); } return headers; } } export default Config;