Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 30x 30x 30x 44x 333x 321x 317x 8x | import {
createApiClient,
type ApiClient,
type UnauthorizedHandler,
} from "@plain-journal/foundation";
export interface AuthenticatedSessionBinding {
accessToken: () => string | null;
requestAuthority: () => string | null;
onUnauthorized: UnauthorizedHandler;
}
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL?.trim() ?? "";
const allowFallbackAccessToken = import.meta.env.MODE === "test";
let sessionBinding: AuthenticatedSessionBinding | null = null;
export function configureAuthenticatedSession(
binding: AuthenticatedSessionBinding,
): void {
sessionBinding = binding;
}
export function createAuthenticatedApiClient(
fallbackAccessToken: string | null,
timeoutMs = 8000,
): ApiClient {
return createApiClient({
baseUrl: apiBaseUrl,
timeoutMs,
tokenProvider: () => sessionBinding
? sessionBinding.accessToken()
: allowFallbackAccessToken
? fallbackAccessToken
: null,
requestAuthorityProvider: () => sessionBinding?.requestAuthority() ?? null,
onUnauthorized: async (context) => sessionBinding
? sessionBinding.onUnauthorized(context)
: "reject",
});
}
|