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

engine-configuration.mddocs/

Engine Configuration

Configuration system for customizing JavaScript client behavior, fetch options, and platform-specific settings. The configuration allows direct access to the underlying Fetch API options while maintaining Kotlin type safety.

Capabilities

JsClientEngineConfig

Configuration class for the JavaScript client engine that extends the base HTTP client engine configuration.

/**
 * Configuration for the Js client.
 */
open class JsClientEngineConfig : HttpClientEngineConfig() {
    /**
     * Provides access to the underlying fetch options of the engine.
     * It allows setting credentials, cache, mode, redirect, referrer, integrity, keepalive, signal, window.
     */
    fun configureRequest(block: RequestInit.() -> Unit)
    
    /**
     * An Object which can contain additional configuration options that should get passed to node-fetch.
     * @deprecated Use configureRequest instead
     */
    @Deprecated("Use configureRequest instead", level = DeprecationLevel.WARNING)
    var nodeOptions: dynamic
}

Usage Examples:

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

// Configure fetch options
val client = HttpClient(Js) {
    engine {
        configureRequest {
            // Set credentials for CORS requests
            credentials = "include"
            
            // Configure caching behavior
            cache = "no-cache"
            
            // Set request mode
            mode = "cors"
            
            // Configure redirect handling
            redirect = "follow"
            
            // Set referrer policy
            referrer = "no-referrer"
            
            // Enable keepalive for background requests
            keepalive = true
        }
    }
}

Configure Request Block

The configureRequest method provides access to all standard Fetch API options through a type-safe Kotlin DSL.

/**
 * Configuration block for RequestInit options
 * Provides access to all fetch API configuration options
 * @param block Lambda with RequestInit receiver for configuration
 */
fun configureRequest(block: RequestInit.() -> Unit)

Available RequestInit Properties:

interface RequestInit {
    var method: String?
    var headers: dynamic
    var body: dynamic
    var mode: String?           // "cors", "no-cors", "same-origin", "navigate"
    var credentials: String?    // "omit", "same-origin", "include"
    var cache: String?          // "default", "no-store", "reload", "no-cache", "force-cache", "only-if-cached"
    var redirect: String?       // "follow", "error", "manual"
    var referrer: String?       // URL string or "no-referrer", "client"
    var referrerPolicy: String? // "no-referrer", "no-referrer-when-downgrade", etc.
    var integrity: String?      // Subresource integrity hash
    var keepalive: Boolean?     // Keep connection alive for background requests
    var signal: AbortSignal?    // AbortController signal for cancellation
    var window: dynamic         // Window object (browser only)
}

Advanced Configuration Examples:

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

// CORS configuration
val corsClient = HttpClient(Js) {
    engine {
        configureRequest {
            mode = "cors"
            credentials = "include"
            headers = js("""{
                "Accept": "application/json",
                "Content-Type": "application/json"
            }""")
        }
    }
}

// Background request configuration
val backgroundClient = HttpClient(Js) {
    engine {
        configureRequest {
            keepalive = true
            cache = "no-cache"
        }
    }
}

// Request with abort signal
val controller = AbortController()
val abortableClient = HttpClient(Js) {
    engine {
        configureRequest {
            signal = controller.signal
        }
    }
}

// Cancel the request later
// controller.abort()

Legacy Node Options (Deprecated)

The nodeOptions property is deprecated but still available for backward compatibility.

/**
 * @deprecated Use configureRequest instead
 * An Object which can contain additional configuration options for node-fetch.
 */
@Deprecated("Use configureRequest instead", level = DeprecationLevel.WARNING)
var nodeOptions: dynamic

Migration Example:

// Old way (deprecated)
val client = HttpClient(Js) {
    engine {
        nodeOptions.agent = customAgent
    }
}

// New way (recommended)
val client = HttpClient(Js) {
    engine {
        configureRequest {
            // Use RequestInit properties instead
            // Custom agents should be configured through proper fetch options
        }
    }
}

Common Configuration Patterns

Authentication with Credentials

val authClient = HttpClient(Js) {
    engine {
        configureRequest {
            credentials = "include" // Send cookies and auth headers
        }
    }
}

Cache Control

val noCacheClient = HttpClient(Js) {
    engine {
        configureRequest {
            cache = "no-cache"      // Always validate with server
            // or cache = "no-store" // Never cache
            // or cache = "reload"   // Always fetch from server
        }
    }
}

CORS Configuration

val corsClient = HttpClient(Js) {
    engine {
        configureRequest {
            mode = "cors"
            credentials = "same-origin"
            referrerPolicy = "strict-origin-when-cross-origin"
        }
    }
}

Request Integrity

val secureClient = HttpClient(Js) {
    engine {
        configureRequest {
            integrity = "sha384-abc123..." // Subresource integrity hash
            referrer = "no-referrer"       // Don't send referrer header
        }
    }
}

Platform Differences

  • Browser: All RequestInit options are supported as per Fetch API specification
  • Node.js: Some options like window are not applicable; agent configuration should be done through other means
  • WASM-JS: Limited to basic fetch options supported by the WASM runtime

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