|
| 1 | +# Datastar Kotlin SDK |
| 2 | + |
| 3 | +A Kotlin SDK for Datastar! |
| 4 | + |
| 5 | +- No dependencies, just the standard Kotlin library! |
| 6 | +- 100% Kotlin, no Java dependencies! |
| 7 | +- Multiplatform! |
| 8 | +- Framework-agnostic, adapt to your own context and framework! |
| 9 | + |
| 10 | +## Getting Started |
| 11 | + |
| 12 | +### Minimum Requirements |
| 13 | + |
| 14 | +The minimum JVM version compatible is **Java 21**. |
| 15 | + |
| 16 | +### Add the dependency |
| 17 | + |
| 18 | +#### Gradle |
| 19 | + |
| 20 | +```kotlin |
| 21 | +dependencies { |
| 22 | + implementation("dev.datastar.kotlin:kotlin-sdk:...") |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +#### Maven |
| 27 | + |
| 28 | +```xml |
| 29 | + |
| 30 | +<dependency> |
| 31 | + <groupId>dev.datastar.kotlin</groupId> |
| 32 | + <artifactId>kotlin-sdk</artifactId> |
| 33 | + <version>...</version> |
| 34 | +</dependency> |
| 35 | +``` |
| 36 | + |
| 37 | +### Usage |
| 38 | + |
| 39 | +The SDK offers APIs to abstract the Datastar protocol while allowing you to adapt it to your own context and framework. |
| 40 | + |
| 41 | +The following shows a simple implementation base of the Java `HttpServer`. |
| 42 | + |
| 43 | +```kotlin |
| 44 | + |
| 45 | +val server = HttpServer.create( |
| 46 | + InetSocketAddress(8080), // Port used |
| 47 | + 0, // Backlog, 0 means default |
| 48 | + "/", // Path |
| 49 | + { exchange -> // Exchange handler |
| 50 | + |
| 51 | + // The `readSignals` method extracts the signals from the request. |
| 52 | + // If you use a web framework, you likely don't need this since the framework probably already handles this in its own way. |
| 53 | + // However, this method in the SDK allows you to provide your own unmarshalling strategy so you can adapt it to your preferred technology! |
| 54 | + val request: Request = adaptRequest(exchange) |
| 55 | + val signals = readSignals<EventsWrapper>(request, jsonUnmarshaller) |
| 56 | + |
| 57 | + // Connect a Datastar SSE generator to the response. |
| 58 | + val response: Response = adaptResponse(exchange) |
| 59 | + val generator = ServerSentEventGenerator(response) |
| 60 | + |
| 61 | + |
| 62 | + // Below are some simple examples of how to use the generator. |
| 63 | + generator.patchElements( |
| 64 | + elements = "<div>Merge</div>", |
| 65 | + ) |
| 66 | + |
| 67 | + generator.patchSignals( |
| 68 | + signals = |
| 69 | + """ |
| 70 | + { |
| 71 | + "one":1, |
| 72 | + "two":2 |
| 73 | + } |
| 74 | + """.trimIndent(), |
| 75 | + ) |
| 76 | + |
| 77 | + generator.executeScript( |
| 78 | + script = "alert('Hello World!')", |
| 79 | + ) |
| 80 | + |
| 81 | + exchange.close() |
| 82 | + } |
| 83 | +) |
| 84 | + |
| 85 | +fun adaptRequest(exchange: HttpExchange): Request = object : Request { |
| 86 | + override fun bodyString() = exchange.requestBody.use { it.readAllBytes().decodeToString() } |
| 87 | + |
| 88 | + override fun isGet() = exchange.requestMethod == "GET" |
| 89 | + |
| 90 | + override fun readParam(string: String) = |
| 91 | + exchange.requestURI.query |
| 92 | + ?.let { URLDecoder.decode(it, Charsets.UTF_8) } |
| 93 | + ?.split("&") |
| 94 | + ?.find { it.startsWith("$string=") } |
| 95 | + ?.substringAfter("=")!! |
| 96 | +} |
| 97 | + |
| 98 | +fun adaptResponse(exchange: HttpExchange): Response = object : Response { |
| 99 | + |
| 100 | + override fun sendConnectionHeaders( |
| 101 | + status: Int, |
| 102 | + headers: Map<String, List<String>>, |
| 103 | + ) { |
| 104 | + exchange.responseHeaders.putAll(headers) |
| 105 | + exchange.sendResponseHeaders(status, 0) |
| 106 | + } |
| 107 | + |
| 108 | + override fun write(text: String) { |
| 109 | + exchange.responseBody.write(text.toByteArray()) |
| 110 | + } |
| 111 | + |
| 112 | + override fun flush() { |
| 113 | + exchange.responseBody.flush() |
| 114 | + } |
| 115 | + |
| 116 | +} |
| 117 | +``` |
0 commit comments