SDK API¶
Package: polarion-app-sdk, version 0.5.2-preview. Entry module HostBridge (ESM and CommonJS). No runtime dependencies. Browser only; it uses window.parent.postMessage.
import {
fetchContext,
events,
destroy,
EventTypes,
type PolarionContext,
type SelectionChangedData,
} from 'polarion-app-sdk';
fetchContext()¶
function fetchContext(): Promise<PolarionContext>
Requests the current context from the Polarion host page.
- Creates the internal gateway on first use (or after
destroy()). - Resolves with a
PolarionContext. - Rejects with
Error('Timeout waiting for response to get-context')after five seconds without an answer, and withError('Invalid context received')if the answer does not containapplicationIdandviewId. A call made whiledestroy()is tearing down a previous instance is rejected withError('ApplicationGateway is destroyed, pending request cancelled.'). - May be called any number of times; each call is a separate round trip and returns the state at that moment.
events.subscribe()¶
events.subscribe<T = unknown>(
eventName: EventType,
callback: (data: T) => void,
): () => void
Registers callback for an event emitted by the host and returns a function that removes the registration.
eventName: a value ofEventTypes. In 0.5.2-preview the only value isEventTypes.SELECTION_CHANGED("selection-changed").callbackreceives the event payload. ForSELECTION_CHANGEDthe payload isSelectionChangedData; pass it as the type parameter.- Throws
Error('Already subscribed to event: <name>')when a subscription for that event already exists. Unsubscribe first. - The returned function is idempotent; calling it twice is harmless.
const unsubscribe = events.subscribe<SelectionChangedData>(
EventTypes.SELECTION_CHANGED,
({ selectedWorkItemIds }) => update(selectedWorkItemIds),
);
destroy()¶
function destroy(): void
Tears down the gateway: removes the message listener, rejects pending fetchContext() promises with Error('ApplicationGateway is destroyed, pending request cancelled.'), sends an unsubscribe for every active subscription, and clears them. Safe to call when nothing was created. The next fetchContext() or events.subscribe() creates a new instance.
Call it from the unmount hook of your framework.
EventTypes¶
const EventTypes = {
SELECTION_CHANGED: 'selection-changed',
} as const;
type EventType = typeof EventTypes[keyof typeof EventTypes];
Types¶
PolarionContext¶
interface PolarionContext {
applicationId: string;
viewId: string;
projectId?: string;
spaceId?: string;
documentId?: string;
workItemId?: string;
selectedWorkItemIds: string[];
appEndpoint: string;
restBaseUrl: string;
restToken?: string;
}
Described field by field in PolarionContext.
SelectionChangedData¶
interface SelectionChangedData {
selectedWorkItemIds: string[];
}
Work item ids with the project ("test/TEST-514"), observed on Polarion 2612 in the Work Items table and in Documents. Empty array when nothing is selected.
Behavior notes¶
- Origin check: the SDK posts to and accepts messages only from
window.location.origin. Messages from other origins are ignored with a console warning. - Single instance per page: the module keeps one gateway. Several components in the same view share it; only one of them may subscribe to a given event.
- Console output: the SDK logs warnings for unexpected messages (
Received message from unknown origin…,No pending request found…). They are harmless in Polarion but worth investigating if they appear repeatedly. - Timeouts: fixed at 5 s and not configurable.
Internal modules (not public API)¶
ApplicationGateway, Constants (Actions, MessageTypes) and the protocol types in types/ApplicationGatewayProtocol describe the postMessage protocol (polarion-view-bridge, polarion-view-bridge-result, polarion-view-bridge-event). They are exported as type declarations for transparency but may change without notice. Do not post these messages yourself.