learning_ai_invt_trdg/shared/runtime.ts
Saravana Achu Mac 4cfb446f57 feat(backend): WebSocket namespaces, audit persistence, tab flags, telemetry
- Add /trading and /admin named Socket.IO namespaces; root namespace kept for
  backward compat; admin namespace rejects non-admins at connect time
- Wire auditRepository.ts: persist TradeAuditEvent to Cosmos audit-events
  container (best-effort); expose GET /api/admin/audit for admin queries
- Add tradingTelemetry singleton (Node.js Map-based storage adapter); init
  and fatal-error tracking wired in index.ts main()
- Add TAB_MARKETPLACE_ENABLED / TAB_MEMBERSHIP_ENABLED config flags; expose
  tabs.* shape in GET /api/feature-flags response
- Fix SupabaseService URL validation (regex check before createClient)
- Wire check:api-contract and check:audit-repository into npm run test
- Switch @bytelyst/* deps to file:../vendor/* references

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-29 19:35:00 -04:00

48 lines
1.5 KiB
TypeScript

import { productConfig } from './product.js';
export type ProductSurface = 'web' | 'mobile' | 'backend';
export interface RuntimeEnvironment {
productId: string;
platformApiUrl: string;
tradingApiUrl: string;
}
function readEnv(key: string): string | undefined {
const value = typeof process !== 'undefined' ? process.env[key] : undefined;
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined;
}
export function getRuntimeEnvironment(surface: ProductSurface): RuntimeEnvironment {
const surfacePrefix =
surface === 'mobile' ? 'EXPO_PUBLIC_' : surface === 'web' ? 'NEXT_PUBLIC_' : '';
const productId =
readEnv(`${surfacePrefix}PRODUCT_ID`) ??
readEnv('VITE_PRODUCT_ID') ??
readEnv('PRODUCT_ID') ??
productConfig.productId;
const platformApiUrl =
readEnv(`${surfacePrefix}PLATFORM_URL`) ??
readEnv('VITE_PLATFORM_URL') ??
readEnv('PLATFORM_API_URL') ??
'http://localhost:4003/api';
// Web code appends /api/... itself — no /api suffix in the base URL.
// Mobile code expects /api included and strips it for socket (EXPO_PUBLIC_TRADING_API_URL=http://host:port/api).
// Always set the surface-specific env var explicitly; do not rely on this default for mobile.
const tradingApiUrl =
readEnv(`${surfacePrefix}TRADING_API_URL`) ??
readEnv('VITE_TRADING_API_URL') ??
readEnv('TRADING_API_URL') ??
`http://localhost:${productConfig.backendPort}`;
return {
productId,
platformApiUrl,
tradingApiUrl,
};
}