Skip to content

Commit 704ee92

Browse files
authored
Read signals from Request via Schema into case class (#3791) (#3813)
1 parent b01e3ed commit 704ee92

File tree

4 files changed

+306
-71
lines changed

4 files changed

+306
-71
lines changed

docs/reference/datastar-sdk/index.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ To use the Datastar SDK with ZIO HTTP, add the following dependency to your `bui
4040
libraryDependencies += "dev.zio" %% "zio-http-datastar-sdk" % "@VERSION@"
4141
```
4242

43-
You also have to include the Datastar JavaScript client module in your HTML pages. You can do this by adding the following script tag to your HTML head:
43+
You also have to include the Datastar JavaScript client module in your HTML pages. You can do this by adding the following script tag to your HTML:
4444

4545
```scala mdoc:compile-only
46+
import zio.http.datastar._
4647
import zio.http.template2._
4748

48-
script(
49-
`type` := "module",
50-
src := "https://cdn.jsdelivr.net/gh/starfederation/datastar@<VERSION>/bundles/datastar.js"
51-
)
49+
// Uses the default version of datastar from the zio-http-datastar-sdk module
50+
head(datastarScript)
51+
52+
// Or specify a custom version of datastar
53+
head(datastarScript(version = "1.2.3"))
5254
```
5355

5456
Pick the proper version of the module according to the [installation instructions](https://data-star.dev/guide/getting_started#installation) in the Datastar's documentation.
@@ -189,30 +191,24 @@ The server can extract the signals from the request like this:
189191

190192
```scala mdoc:silent
191193
import zio._
192-
import zio.json._
194+
import zio.schema._
193195
import zio.http._
194196
import zio.http.datastar._
195197

196198
case class Delay(value: Int)
197199
object Delay {
198-
implicit val jsonCodec: JsonCodec[Delay] = DeriveJsonCodec.gen
200+
implicit val schema: Schema[Delay] = DeriveSchema.gen
199201
}
200202

201203
val route =
202204
Method.GET / "hello-world" -> events {
203205
handler { (request: Request) =>
204-
val delay = request.url.queryParams
205-
.getAll("datastar")
206-
.headOption
207-
.flatMap { s =>
208-
Delay.jsonCodec.decodeJson(s).toOption
209-
}
210-
.getOrElse(Delay(100))
211206

212207
// Use the extracted delay value in your logic
213208
val message = "Hello, world!"
214209
ZIO.foreachDiscard(message.indices) { i =>
215210
for {
211+
delay <- request.readSignals[Delay].orElse(ZIO.succeed(Delay(100)))
216212
_ <- ServerSentEventGenerator.executeScript(js"console.log('Sending substring(0, ${i + 1})')")
217213
_ <- ServerSentEventGenerator.patchElements(div(id("message"), message.substring(0, i + 1)))
218214
_ <- ZIO.sleep(delay.value.millis)
@@ -253,16 +249,15 @@ div(
253249
The server responds with a single-shot event that updates a greeting message with the provided username:
254250

255251
```scala mdoc:compile-only
256-
Method.GET / "greet" -> handler { (req: Request) =>
257-
Response(
258-
headers = Headers(Header.ContentType(MediaType.text.`html`)),
259-
body = Body.fromCharSequence(
252+
Method.GET / "greet" -> event {
253+
handler { (req: Request) =>
254+
DatastarEvent.patchElements(
260255
div(
261256
id("greeting"),
262257
p(s"Hello ${req.queryParam("name").getOrElse("Guest")}"),
263-
).render
264-
)
265-
)
258+
)
259+
)
260+
}
266261
}
267262
```
268263

0 commit comments

Comments
 (0)