diff --git a/shared/platform-clients.ts b/shared/platform-clients.ts index c6d2c84..34ba58f 100644 --- a/shared/platform-clients.ts +++ b/shared/platform-clients.ts @@ -1,16 +1,45 @@ -import { createKillSwitchClient } from '@bytelyst/kill-switch-client'; import { createWebTelemetry } from '@bytelyst/telemetry-client'; import { createRNPlatformSDK } from '@bytelyst/react-native-platform-sdk'; import { getRuntimeEnvironment } from './runtime.js'; import { productConfig } from './product.js'; +import { createRequestId } from './request-id.js'; + +interface TradingKillSwitchResult { + disabled: boolean; + message: string | null; +} + +function createRepoKillSwitchClient(baseUrl: string, productId: string, platform: 'web' | 'mobile') { + return { + async check(): Promise { + try { + const response = await globalThis.fetch( + `${baseUrl}/settings/kill-switch?platform=${encodeURIComponent(platform)}`, + { + headers: { + 'x-product-id': productId, + 'x-request-id': createRequestId('kill-switch'), + }, + } + ); + + if (!response.ok) return { disabled: false, message: null }; + + const data = await response.json() as { disabled?: boolean; message?: string | null }; + return { + disabled: data.disabled ?? false, + message: data.message ?? null, + }; + } catch { + return { disabled: false, message: null }; + } + }, + }; +} export function createTradingKillSwitchClient(platform: 'web' | 'mobile') { const runtime = getRuntimeEnvironment(platform); - return createKillSwitchClient({ - baseUrl: runtime.platformApiUrl, - productId: runtime.productId, - platform, - }); + return createRepoKillSwitchClient(runtime.platformApiUrl, runtime.productId, platform); } export function createTradingWebTelemetry() { @@ -32,4 +61,3 @@ export function createTradingMobileSdk(getAccessToken: () => string | null) { getAccessToken, }); } - diff --git a/shared/platform-mobile.ts b/shared/platform-mobile.ts index c822261..950f7ea 100644 --- a/shared/platform-mobile.ts +++ b/shared/platform-mobile.ts @@ -1,14 +1,43 @@ -import { createKillSwitchClient } from '@bytelyst/kill-switch-client'; import { createRNPlatformSDK } from '@bytelyst/react-native-platform-sdk'; import { getRuntimeEnvironment } from './runtime.js'; +import { createRequestId } from './request-id.js'; + +interface TradingKillSwitchResult { + disabled: boolean; + message: string | null; +} + +function createRepoKillSwitchClient(baseUrl: string, productId: string, platform: 'web' | 'mobile') { + return { + async check(): Promise { + try { + const response = await globalThis.fetch( + `${baseUrl}/settings/kill-switch?platform=${encodeURIComponent(platform)}`, + { + headers: { + 'x-product-id': productId, + 'x-request-id': createRequestId('kill-switch'), + }, + } + ); + + if (!response.ok) return { disabled: false, message: null }; + + const data = await response.json() as { disabled?: boolean; message?: string | null }; + return { + disabled: data.disabled ?? false, + message: data.message ?? null, + }; + } catch { + return { disabled: false, message: null }; + } + }, + }; +} export function createTradingKillSwitchClient(platform: 'web' | 'mobile') { const runtime = getRuntimeEnvironment(platform); - return createKillSwitchClient({ - baseUrl: runtime.platformApiUrl, - productId: runtime.productId, - platform, - }); + return createRepoKillSwitchClient(runtime.platformApiUrl, runtime.productId, platform); } export function createTradingMobileSdk(getAccessToken: () => string | null) { diff --git a/shared/platform-web.ts b/shared/platform-web.ts index 7122835..d64d1a8 100644 --- a/shared/platform-web.ts +++ b/shared/platform-web.ts @@ -1,15 +1,44 @@ -import { createKillSwitchClient } from '@bytelyst/kill-switch-client'; import { createWebTelemetry } from '@bytelyst/telemetry-client'; import { getRuntimeEnvironment } from './runtime.js'; import { productConfig } from './product.js'; +import { createRequestId } from './request-id.js'; + +interface TradingKillSwitchResult { + disabled: boolean; + message: string | null; +} + +function createRepoKillSwitchClient(baseUrl: string, productId: string, platform: 'web' | 'mobile') { + return { + async check(): Promise { + try { + const response = await globalThis.fetch( + `${baseUrl}/settings/kill-switch?platform=${encodeURIComponent(platform)}`, + { + headers: { + 'x-product-id': productId, + 'x-request-id': createRequestId('kill-switch'), + }, + } + ); + + if (!response.ok) return { disabled: false, message: null }; + + const data = await response.json() as { disabled?: boolean; message?: string | null }; + return { + disabled: data.disabled ?? false, + message: data.message ?? null, + }; + } catch { + return { disabled: false, message: null }; + } + }, + }; +} export function createTradingKillSwitchClient(platform: 'web' | 'mobile') { const runtime = getRuntimeEnvironment(platform); - return createKillSwitchClient({ - baseUrl: runtime.platformApiUrl, - productId: runtime.productId, - platform, - }); + return createRepoKillSwitchClient(runtime.platformApiUrl, runtime.productId, platform); } export function createTradingWebTelemetry() {