wealth/apps/bear/src/bootstrap/bootstrap.client.ts

33 lines
1.4 KiB
TypeScript
Raw Normal View History

import { configureErrorHandler } from '@lumina/libs/error-handler';
import { eventEmitter } from '@lumina/libs/event-emitter';
import { http } from '@lumina/libs/http';
import { mapApiErrorBody } from '@lumina/market/app/http';
import { SESSION_REFRESH_ENDPOINT } from '@lumina/market/app/session';
import { setEnv as setMarketEnv } from '@lumina/market/env';
import { AUTH_EVENTS } from '@lumina/market/features/auth';
import { API_VERSION_PATH } from '../constants';
import { clientEnv } from '../env';
import { ERROR_HANDLER_CONFIG } from './error-reporting';
// Fills the client module graph with public values only, before client components render (during
// SSR and in the browser). Idempotent: safe to run again.
setMarketEnv({
apiOrigin: clientEnv.NEXT_PUBLIC_API_ORIGIN,
baleHandle: clientEnv.NEXT_PUBLIC_BALE_HANDLE,
telegramHandle: clientEnv.NEXT_PUBLIC_TELEGRAM_HANDLE,
supportHours: clientEnv.NEXT_PUBLIC_SUPPORT_HOURS,
});
// The session lives in HttpOnly cookies the browser sends itself. A 401 runs one refresh for the
// whole app; when that fails the session is over and the shell signs the user out.
http.configure({
baseUrl: `${clientEnv.NEXT_PUBLIC_API_ORIGIN}${API_VERSION_PATH}`,
refresh: {
endpoint: SESSION_REFRESH_ENDPOINT,
onFailure: () =>
eventEmitter.emit(AUTH_EVENTS.logout, { reason: 'session-ended' }),
},
mapErrorBody: mapApiErrorBody,
});
configureErrorHandler(ERROR_HANDLER_CONFIG);