Skip to content

Bridge API

The @unextension/bridge package provides a unified API for your web app to communicate with the host IDE, regardless of whether it runs inside VS Code or JetBrains.

Terminal window
npm install @unextension/bridge
# or
pnpm add @unextension/bridge
import { bridge } from '@unextension/bridge'
// Send a message to the extension host
bridge.postMessage('myCommand', { file: 'index.ts' })
// Listen for messages from the extension host
const unsubscribe = bridge.onMessage((message) => {
console.log('Received:', message)
})
// Stop listening
unsubscribe()

Sends a message to the IDE extension host.

ParameterTypeDescription
typestringThe message type / command name
payloadunknownOptional data to send

Internally routes to acquireVsCodeApi().postMessage on VS Code, or __unextension_jb_bridge on JetBrains.

Registers a handler for messages sent from the IDE host to the webview.

Returns an unsubscribe function.

ParameterTypeDescription
handler(message: unknown) => voidCalled whenever a message is received
interface Bridge {
postMessage(type: string, payload?: unknown): void
onMessage(handler: MessageHandler): () => void
}
type MessageHandler = (message: unknown) => void