@gbg/go-bridge-web) and the React adapter (@gbg/go-bridge-web-react) are covered.
Packages: @gbg/go-bridge-web (core), @gbg/go-bridge-web-react (React 19 wrapper)
Browsers: ES2020 / modern evergreen
TypeScript: 5.x
Table of contents
- Primary entry points
- BridgeHost
- BridgeHostProvider — React
- React hooks
- Configuration types
- Capability types
- Message models
- Actions
- Errors
- Interfaces
- Classes
- Built-in handlers
- React context
Primary entry points
These are the types you interact with directly to wire the bridge into your app. Non-React hosts useBridgeHost directly; React hosts wrap it in BridgeHostProvider and consume via hooks.
BridgeHost
The central coordinator that manages message routing between an<iframe> and your host page.
BridgeHost decodes incoming messages from the journey iframe, validates origins, routes requests to registered handlers, tracks pending requests, and sends responses and events back to the iframe. It is the only object you must construct to use the SDK without React.
Threading: JavaScript is single-threaded — there is no thread enforcement, but all calls happen on the main thread by definition.
Constructor
CapabilityQueryHandler is automatically registered for the "capability.query" action.
Example:
Static properties
Constants exposed on theBridgeHost class itself, not on instances.
PROTOCOL_VERSION
Properties
Methods
Imperative operations on aBridgeHost instance — attaching and detaching the iframe, registering handlers, sending events, and responding to requests.
attach
pendingRequests and receivedMessages buffers across the re-attach.
detach
window message listener and tears down the transport channel. Idempotent. After detach, inbound messages are no longer received and outbound sends are silently dropped.
Call this when the iframe is removed from the page. BridgeHostProvider calls it automatically on unmount.
registerHandler
capabilities constructor option or registerCustomCapability() for declaration.
getHandler
undefined. Used internally by useBridgeCapability to verify ownership before unregistering on unmount.
unregister
pendingRequests and delegate.onUnhandledRequest will fire.
Unregistering "capability.query" removes the SDK’s auto-registered handler — the SDK warns when this happens. Capability queries will stop receiving responses unless you register a replacement.
registerCustomCapability
capability.query responses, and if handler is provided, requests for the matching action ID are routed to it.
This is the closest web equivalent to iOS/Android’s “set handler on typed slot” pattern — both declaration and registration in one step.
sendEvent
web-event-{uuid}.
respond
delegate.onUnhandledRequest.
If options.action is omitted, the SDK looks up the original request in pendingRequests and uses its action. On success, the request is removed from pendingRequests.
The host enforces at-most-once delivery on terminal statuses (
success, error, cancelled, unsupported). Subsequent calls with the same correlationId and a terminal status are dropped. acknowledged is interim and may precede a terminal status.
clearError
lastError to null.
BridgeHostProvider
React provider that owns theBridgeHost lifecycle. Wrap your iframe-rendering subtree in this provider and use the hooks to interact with the bridge.
BridgeHost API. Holds the host in component state, sets up the underlying transport, exposes context to descendant hooks, and tears down on unmount.
Lifecycle:
- Mounts with
iframe={null}— no host yet. - iframe element appears (via
ref={setIframe}callback). Provider instantiatesBridgeHostand snapshotsallowedOrigins,hostVersion, andcapabilitiesprops. - Re-renders that change props do not rebuild the host. Force a remount (key change) to apply runtime config updates.
- Unmount: cleanup calls
host.detach()and clears the host from state.
Props
SeeBridgeHostProviderProps.
Example:
React hooks
All hooks must be called inside aBridgeHostProvider. Outside the provider they no-op and log a one-time console.warn.
useBridgeHost
Returns the underlyingBridgeHost instance, or null while the iframe hasn’t attached.
sendEvent, registerCustomCapability, clearError, etc.) that don’t have dedicated hooks.
Example:
useBridgeCapability
Registers aBridgeCapabilityHandler for the lifetime of the calling component.
handler in a stable proxy: identity never changes across renders, but the proxy delegates to the latest handler via a ref. An inline { handle: ... } object literal does not re-register on every render — only action changes (or a host swap) trigger re-registration.
On unmount, the hook unregisters only if its own proxy is still the active handler. This prevents two components racing on the same action from accidentally removing each other’s installations.
This only registers the handler. To declare the capability as supported, pass an entry in the provider’s
capabilities prop or call host.registerCustomCapability(id, version, handler) directly. See Concepts → Declaring vs. Registering.
Example:
useBridgeHostEvent
Subscribes to events emitted by the journey iframe with a specific action ID. The listener is invoked with the event’sdata payload.
Listener identity affects the effect dependency array — wrap inline listeners in
useCallback if you want a stable subscription across renders.
Example:
useBridgeHostError
Subscribes to internal SDK errors — handler exceptions, send failures, delegate throws, and other errors routed viaBridgeHost.fireError.
This subscribes to errors raised by the host itself. Errors returned to the journey via
responder.error(...) are not surfaced here — they’re outbound responses, not internal failures.
Configuration types
The interfaces below configureBridgeHost and BridgeHostProvider at construction time. All are plain TypeScript interfaces — no classes or constructors.
BridgeHostOptions
Constructor argument forBridgeHost.
BridgeHostProviderProps
Props forBridgeHostProvider.
RespondOptions
Argument toBridgeHost.respond.
Capability types
Types and constants for declaring capabilities the host can fulfil.CapabilityInfo is per-capability metadata; CapabilityMap is the shape you pass to the constructor; CAPABILITY_IDS lists standard identifiers.
CapabilityInfo
Metadata about a single capability.CapabilityMap
A map of capability IDs toCapabilityInfo.
CapabilityId
A type alias for the values inCAPABILITY_IDS. Useful when you want compile-time enforcement that a string is a standard capability identifier.
Environment
The platform reported incapability.query responses. Always "web" from this SDK.
The union is shared with the iframe-side SDK for type compatibility; host SDKs only ever emit their own platform’s value.
CAPABILITY_IDS
Standard capability identifiers, standardized across iOS, Android, and Web — the same string values appear inCAPABILITY_IDS on all three platforms, and on the iframe-side SDK. Use the constants rather than magic strings to avoid typos and to track value changes through TypeScript’s reference graph.
Message models
Every value that crosses the bridge is wrapped in aBridgeMessage envelope.
BridgeMessage
A structured envelope for all bridge communication.BridgeMessageType
RequestPayload
RequestOptions
ResponsePayload
ResponseStatus
ResponseError
EventPayload
Actions
Standard action identifiers for events the journey emits and commands the host sends. Use the constants to avoid magic strings that drift.BridgeActions
Known action identifiers used in events. Imported asBridgeActions and typed as BridgeAction.
Errors
A typed error class and enum for use in your own handler code when emitting structured errors — plus a helper to normalise unknown codes.BridgeError
A typed error class for use in your own host code when emitting structured errors.BridgeError internally — it’s a helper for your handler code. Catch it in your own error-handling and forward to responder.error({ code, message, recoverable }).
Example:
BridgeErrorCode
The SDK additionally emits
HANDLER_FAILURE (string literal, not in this enum) when a registered handler throws an uncaught exception and hasn’t already responded.
toBridgeErrorCode
BridgeErrorCode, falling back to INTERNAL_ERROR otherwise. Useful when receiving error codes from external sources (e.g., parsing a response from the iframe).
Returns: A
BridgeErrorCode enum value.
Interfaces
Contracts your host code implements — one for capability handlers, one for observing bridge activity.BridgeCapabilityHandler
The contract for objects that handle bridge request actions.error response with code: "HANDLER_FAILURE" and recoverable: false so the journey is never left waiting.
Example:
BridgeHostDelegate
An observer interface for monitoring bridge activity. All methods are optional.
Delegate method exceptions are caught: a throw from
onMessage or onUnhandledRequest is routed to onError. A throw from onError is logged via console.warn and swallowed (preventing recursion).
Classes
Concrete classes exposed by the SDK. Integrators use them but don’t instantiate them directly — the host creates them and hands them to your handlers.BridgeResponder
Handed to handlers as the second argument. Used to send a response to the originating request.postMessage or correlation IDs — they just call a terminal method.
You don’t instantiate BridgeResponder directly. The host creates one per inbound request and passes it to the handler.
Properties
State exposed on the responder instance.hasResponded
true after a terminal method (success, error, cancelled, unsupported) has been called. acknowledge does not set this to true.
Methods
One interim method (acknowledge) and four terminal methods — call exactly one terminal per request.
acknowledge
acknowledged response to signal “I got it, working on it”. Chainable. At-most-once on the wire — no-op after a prior acknowledge or after a terminal status.
Use this for long-running operations to confirm receipt before the terminal response. The journey-side SDK uses acknowledgment to distinguish “host received but slow” from “host never received” timeout cases.
Returns: The responder itself, for chaining.
success
success response. Subsequent calls on this responder are no-ops.
error
error response with the given error details.
cancelled
cancelled response. When reason is provided, the response includes an error payload with code: "CANCELLED" and recoverable: true.
unsupported
unsupported response. When reason is provided, the response includes an error payload with code: "UNSUPPORTED" and recoverable: false.
Built-in handlers
Handlers the SDK auto-registers for you. Documented here so you know what to look for incapability.query responses and how to override them if needed.
CapabilityQueryHandler
The handler for"capability.query" requests. Automatically registered by BridgeHost — you do not register this yourself.
Response format
The handler is constructed automatically by
BridgeHost with a provider that returns the current buildEffectiveCapabilities() map (capabilities option merged with registerCustomCapability entries). Calling unregister('capability.query') removes it and stops automatic responses — the SDK warns when this happens.
React context
These are the low-level context types exported from@gbg/go-bridge-web-react. Most integrators do not interact with them directly — the hooks wrap them. Exposed for advanced cases (custom providers, testing).
BridgeHostContext
BridgeHostProvider populates. The default value (used outside any provider) is a marker that lets hooks detect missing providers and issue a one-time warning.
BridgeHostContextValue
BridgeHostEventListener
data payload — the action is implicit (it’s the action you subscribed with).
BridgeHostErrorListener
Next steps
- Concepts — Architecture and design rationale behind these types.
- Embedding — Integration patterns by framework.
- Messaging — Event catalogue and request/response semantics.
- Capability Handling — Declaring, registering, and dynamic capabilities.