61 lines
2.5 KiB
TypeScript
61 lines
2.5 KiB
TypeScript
import { configureErrorHandler } from '@lumina/libs/error-handler';
|
|
import { eventEmitter } from '@lumina/libs/event-emitter';
|
|
import { http } from '@lumina/libs/http';
|
|
import { captureInstallPrompt, configureServiceWorker } from '@lumina/libs/pwa';
|
|
import { mapApiErrorBody } from '@lumina/market/app/http';
|
|
import { setEnv as setMarketEnv } from '@lumina/market/env';
|
|
import {
|
|
AUTH_ENDPOINTS,
|
|
AUTH_EVENTS,
|
|
endSuspendedSession,
|
|
isSessionSuspended,
|
|
noteAgreementRequired,
|
|
suspendedSessionOf,
|
|
} 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({
|
|
apiBaseUrl: clientEnv.NEXT_PUBLIC_CLIENT_END_POINT,
|
|
baleHandle: clientEnv.NEXT_PUBLIC_BALE_HANDLE,
|
|
telegramHandle: clientEnv.NEXT_PUBLIC_TELEGRAM_HANDLE,
|
|
supportHours: clientEnv.NEXT_PUBLIC_SUPPORT_HOURS,
|
|
});
|
|
// The browser calls the gateway directly at its public origin (0066). 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. A 401 `ACCOUNT_SUSPENDED`
|
|
// never refreshes: the account was suspended mid-session, so the session ends and the page halts
|
|
// (J01-3d), once, whichever request learns it first.
|
|
http.configure({
|
|
baseUrl: `${clientEnv.NEXT_PUBLIC_CLIENT_END_POINT}${API_VERSION_PATH}`,
|
|
refresh: {
|
|
endpoint: AUTH_ENDPOINTS.refresh,
|
|
shouldRefresh: (error) =>
|
|
error.status === 401 && suspendedSessionOf(error) === null,
|
|
onFailure: () => {
|
|
if (isSessionSuspended()) return;
|
|
eventEmitter.emit(AUTH_EVENTS.logout, { reason: 'session-ended' });
|
|
},
|
|
},
|
|
mapErrorBody: mapApiErrorBody,
|
|
});
|
|
http.interceptors.response.use(undefined, (error) => {
|
|
const suspension = suspendedSessionOf(error);
|
|
if (suspension) endSuspendedSession(suspension);
|
|
noteAgreementRequired(error);
|
|
return error;
|
|
});
|
|
configureErrorHandler(ERROR_HANDLER_CONFIG);
|
|
// The service worker stores only JS, CSS, fonts and the icon sprite (pwa lib); the AppShell
|
|
// registers it when this flag is on, and turning the flag off unregisters it for every visitor.
|
|
configureServiceWorker({
|
|
enabled: clientEnv.NEXT_PUBLIC_SERVICE_WORKER_ENABLED,
|
|
});
|
|
// The browser's install offer can arrive before React hydrates; keep it for the install page.
|
|
// Does nothing on the server.
|
|
captureInstallPrompt();
|