CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-ktor--ktor-client-js

Ktor HTTP client implementation for JavaScript platform providing asynchronous HTTP client functionality specifically tailored for JavaScript environments.

Pending
Overview
Eval results
Files

http-engine.mddocs/

HTTP Engine

The core JavaScript HTTP client engine that provides asynchronous HTTP client functionality using the Fetch API. This engine works seamlessly across browser and Node.js environments with automatic platform detection.

Capabilities

Js Engine Object

The main JavaScript client engine factory that creates HTTP clients.

/**
 * A JavaScript client engine that uses the fetch API to execute requests.
 * 
 * To create the client with this engine, pass it to the HttpClient constructor:
 * ```kotlin
 * val client = HttpClient(Js)
 * ```
 */
object Js : HttpClientEngineFactory<JsClientEngineConfig> {
    override fun create(block: JsClientEngineConfig.() -> Unit): HttpClientEngine
}

Usage Examples:

import io.ktor.client.*
import io.ktor.client.engine.js.*

// Basic client creation
val client = HttpClient(Js)

// With configuration
val client = HttpClient(Js) {
    engine {
        configureRequest {
            credentials = "include"
        }
    }
}

JsClient Factory Function

Convenience function to get the Js engine singleton.

/**
 * Creates a Js client engine.
 * @return HttpClientEngineFactory for the Js engine
 */
@JsName("JsClient")
fun JsClient(): HttpClientEngineFactory<JsClientEngineConfig>

Usage Examples:

import io.ktor.client.engine.js.JsClient

// Using convenience function
val client = JsClient()

// Equivalent to HttpClient(Js) but more concise
val engine = JsClient()
val client = HttpClient(engine)

HttpClient Constructor

Platform-specific HttpClient constructor for JavaScript environments.

/**
 * Constructs an asynchronous HttpClient for JavaScript platform.
 * @param block Configuration block for the client
 * @return Configured HttpClient instance
 */
fun HttpClient(block: HttpClientConfig<*>.() -> Unit = {}): HttpClient

Usage Examples:

import io.ktor.client.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*

// Default client for JS platform
val client = HttpClient()

// With plugins and configuration
val client = HttpClient {
    install(ContentNegotiation) {
        json()
    }
    expectSuccess = true
}

Engine Capabilities

The Js engine supports the following Ktor capabilities:

  • HttpTimeoutCapability: Request timeout handling with automatic cancellation
  • WebSocketCapability: WebSocket connections using native browser WebSocket API or 'ws' library in Node.js
  • SSECapability: Server-Sent Events support for real-time data streaming

Platform Detection

The engine automatically detects the runtime environment:

  • Browser Environment: Uses native fetch() API and WebSocket constructor
  • Node.js Environment: Dynamically imports node-fetch and ws libraries
  • WASM-JS Environment: Uses WASM-compatible fetch implementation

Error Handling

All JavaScript runtime errors are wrapped in JsError for consistent error handling across platforms:

try {
    val response = client.get("https://api.example.com")
} catch (error: JsError) {
    println("JavaScript error: ${error.origin}")
    // error.origin contains the original JavaScript error object
}

Limitations

  • Proxy Configuration: Not supported - will throw exceptions if proxy is configured
  • Custom SSL Certificates: Limited compared to JVM implementations
  • Connection Pooling: Relies on browser/Node.js connection management

Install with Tessl CLI

npx tessl i tessl/maven-io-ktor--ktor-client-js

docs

engine-configuration.md

error-handling.md

fetch-api-types.md

http-engine.md

index.md

request-configuration.md

tile.json