import { ActionType } from '../../_contract-src/action-payloads/action-type'; import { EventType } from '../../_contract-src/event-payloads/event-type'; import type { TokenProvidedPayload } from '../../_contract-src/event-payloads/token/token-payload'; import { GetTokenOptions } from '../options/get-token-options'; import { requestFromHost } from './request-from-host'; const DEFAULT_TIMEOUT_MS = 10000; /** * Requests a short-lived signed token (JWT) from Struct PIM for the current app. * * The token is minted server-side and signed with the app's client secret. It can be verified * with the same client secret to confirm it originates from a genuine Struct embedded session * for your tenant. * * The promise rejects with the host's reason if Struct PIM cannot mint a token, and rejects on * timeout if the host does not answer at all. * * @example * ```ts * const { token, expiresAt, issuedAt } = await struct.actions.getToken(); * ``` */ export function getToken(options?: GetTokenOptions): Promise { return requestFromHost({ label: 'getToken', actionType: ActionType.GetToken, resolvesWith: EventType.TOKEN_PROVIDED, timeoutMs: options?.timeoutMs ?? DEFAULT_TIMEOUT_MS, }); }