Warry / elm-ffi / FFI

Do not forget to setup elm-ffi (README)

read : JsCode -> Json.Decode.Value

Quickly access global values

language : String
language =
    FFI.read "return window.navigator.language"
    |> Decode.decodeValue Decode.string
    |> Result.withDefault "en"

function : List ( String, params -> Json.Decode.Value ) -> JsCode -> params -> Task Error Json.Decode.Value

Create a JS aync function from Elm, with its parameters

fetchJson : { url: String } -> Task Error Value
fetchJson =
    FFI.function
        [ ( "url", .url >> Json.Encode.string )
        ]
        """
        let res = await fetch(url)
        if (!res.ok) throw res.statusText
        return await res.json()
        """

decode : Json.Decode.Decoder result -> Task Error Json.Decode.Value -> Task Error result

Decode values within a Task Error Value

fetchedUser : Task FFI.Error User
fetchedUser =
    fetchUser
        |> FFI.decode decodeUser

Manage errors


type alias Error =
Json.Decode.Value

Representing JS errors

usually a JS object with a name and a message

fail : String -> String -> Task Error a

Explain why a Task failed providing error's name and message

error : Task FFI.Error a
error =
    FFI.fail "Some Error" "Some cause or solution"

errorToString : Error -> String

Get a readable error