import { ActionType } from '../../_contract-src/action-payloads/action-type'; import { EventType } from '../../_contract-src/event-payloads/event-type'; import type { BaseContextPayload } from '../../_contract-src/event-payloads/context/context-payload'; import { GetContextOptions } from '../options/get-context-options'; import { requestFromHost } from './request-from-host'; const DEFAULT_TIMEOUT_MS = 10000; /** * Requests the extension context from Struct PIM. * The returned promise resolves with the context payload for the current extension point type * (e.g. {@link TabContextPayload}, {@link SectionContextPayload}, etc.), containing * the entity ID, entity type, current language, user, and segment information. * * The promise rejects with the host's reason if Struct PIM cannot provide a context, and rejects * on timeout if the host does not answer at all. * * @example * ```ts * const ctx = await struct.actions.getContext(); * console.log('Entity:', ctx.entityId, ctx.entityType); * ``` */ export function getContext(options?: GetContextOptions): Promise { return requestFromHost({ label: 'getContext', actionType: ActionType.GetContext, resolvesWith: EventType.CONTEXT_PROVIDED, timeoutMs: options?.timeoutMs ?? DEFAULT_TIMEOUT_MS, }); }