Warry/elm-ffi/1.0.0

FFI

Do not forget to setup elm-ffi (README)

read : JsCode -> Value

Quickly access global values

language : String
language =
    FFI.read "return window.navigator.language"
    |> Decode.decodeValue Decode.string
    |> Result.withDefault "en"
List ( String, params -> Value )
-> JsCode
-> params
-> Task Error 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 : Decoder result -> Task Error 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 =
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