Dari

DariInterceptor API

Interface and default implementation for capturing bridge messages

Interface

interface DariInterceptor {
    val tag: String? get() = null
 
    fun onWebToAppRequest(
        handlerName: String,
        requestId: String?,
        requestData: String?,
        fireAndForget: Boolean? = null,
    )
 
    fun onWebToAppResponse(
        handlerName: String,
        requestId: String?,
        responseData: String?,
        isSuccess: Boolean,
    )
 
    fun onAppToWebRequest(
        handlerName: String,
        requestId: String?,
        data: String?,
        fireAndForget: Boolean? = null,
    )
 
    fun onAppToWebResponse(
        requestId: String?,
        isSuccess: Boolean,
        responseData: String?,
    )
}

DefaultDariInterceptor

The built-in implementation. Stores messages in Dari's Room database and posts notifications.

val interceptor = DefaultDariInterceptor(tag = "PaymentBridge")

The optional tag labels messages by bridge source when multiple interceptors are active in the same app.

Methods

onWebToAppRequest

Called when JavaScript sends a message to the native side.

ParameterTypeDescription
handlerNameStringBridge handler name
requestIdString?Unique ID for pairing with a response. null = standalone
requestDataString?Request payload
fireAndForgetBoolean?null = use global config, true = resolve immediately, false = wait for response

onWebToAppResponse

Called when the native side sends a response back to JavaScript.

ParameterTypeDescription
handlerNameStringMust match the request
requestIdString?Must match the request's requestId
responseDataString?Response payload
isSuccessBooleantrueSUCCESS, falseERROR

onAppToWebRequest

Called when the native side pushes a message to JavaScript.

onAppToWebResponse

Called when JavaScript responds to a native-initiated message.

Fire-and-Forget

One-way calls that never receive a response can be marked so they immediately resolve to SUCCESS:

// Per-call
interceptor.onWebToAppRequest(
    handlerName = "logEvent",
    requestId = null,
    requestData = """{"event": "button_click"}""",
    fireAndForget = true,
)
 
// Global — all calls
DariConfig(fireAndForget = true)

On this page