Skip to content

Move docs for new serialization module to docs sites #811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions serialization/src/jsMain/kotlin/dev/fritz2/remote/serialization.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,3 @@
/**
* # Module fritz2-serialization
*
* This module contains a few helper functions for easy interoperability
* with [`kotlinx.serialization`](https://kotlinlang.org/api/kotlinx.serialization/).
*
* The example in the [documentation](https://www.fritz2.dev/docs/http/)
* could be simplified as follows.
*
* ```kotlin
* @Serializable
* data class Planet(val name: String)
*
* val swapiStore = object : RootStore<String>("") {
*
* private val api = http("https://swapi.dev/api")
* .acceptJson()
* .contentType("application/json")
*
* val planetName = handle<Int> { _, num ->
* val resp = api.get("planets/$num")
* require(resp.ok)
* resp.decoded<Planet>().name
* }
* }
* ```
*
* **Note**:
* for performance reasons this module does **not** convert values
* to string using
* [`encodeToString`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/encode-to-string.html)
* and [`decodeFromString`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/decode-from-string.html).
* Values are instead turned into "native" JavaScript objects using
* [`encodeToDynamic`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/encode-to-dynamic.html)
* and [`decodeFromDynamic`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/decode-from-dynamic.html),
* which work natively with the Fetch API used by `dev.fritz2.remote`.
*/

package dev.fritz2.remote

import kotlinx.serialization.ExperimentalSerializationApi
Expand Down
40 changes: 27 additions & 13 deletions www/src/pages/docs/80_Http.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,46 @@ different results:
* `formData(): FormData`
* `json(): Any?`

Or you can use the fritz2-[serialization](https://central.sonatype.com/artifact/dev.fritz2/serialization)
module, which contains a few helper functions for easy interoperability
with [`kotlinx.serialization`](https://kotlinlang.org/api/kotlinx.serialization/).

If your request was not successful (`Response.ok` property returns `false` according to the
[fetch](https://developer.mozilla.org/en-US/docs/Web/API/Response/ok) API), a `FetchException` will be thrown.
[fetch](https://developer.mozilla.org/en-US/docs/Web/API/Response/ok) API).

The same works for `POST` and all other HTTP methods - just use different parameters for the body to send.

The remote service is primarily designed for use in your `Store`'s `Handler`s when
exchanging data with the backend.
Here is a short example which uses
[https://github.com/Kotlin/kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization)
to parse the returning JSON:
The remote service is primarily designed for use in your `Store`'s `Handler`s when exchanging data with the backend.

Here is a short example which uses fritz2-[serialization](https://central.sonatype.com/artifact/dev.fritz2/serialization)
module to handle the returning JSON:
```kotlin
val swapiStore = object : RootStore<String>("", job = Job()) {
@Serializable
data class Planet(val name: String)

val swapiStore = object : RootStore<String>("", job = Job()) {

private val api = http("https://swapi.dev/api")
.acceptJson()
.contentType("application/json")

val planetName = handle<Int> { _, num ->
val resp = api.get("planets/$num")
if (resp.ok)
Json.parseToJsonElement(resp.body())
.jsonObject["name"]?.jsonPrimitive?.content ?: throw NoSuchElementException()
else throw IllegalArgumentException()
require(resp.ok)
resp.decoded<Planet>().name
}
}
```
```
::: warning
For performance reasons the [serialization](https://central.sonatype.com/artifact/dev.fritz2/serialization) module
does **not** convert values to string using
[`encodeToString`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/encode-to-string.html)
and [`decodeFromString`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/decode-from-string.html).
Values are instead turned into "native" JavaScript objects using
[`encodeToDynamic`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/encode-to-dynamic.html)
and [`decodeFromDynamic`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/decode-from-dynamic.html),
which work natively with the Fetch API used by `dev.fritz2.remote`.
:::

You can use this `Handler` like any other to handle `Flow`s of actions:

Expand Down