Automatically decode (Foreign) objects coming from javascript into PureScript
types.
The interesting functions are decodeForeign and decodeObject, both of which come
from Foreign.Object.Decode...
-- Example.pursmoduleExample (main) whereimportPreludeimportEffect (Effect)
importEffect.ExceptionasExceptionimportData.Foldable (intercalate)
importForeign (Foreign)
importEffect.ConsoleasConsoleimportData.Either (Either(..))
importForeign.Object.Decode (decodeForeign)
typeOptions={port::Int
, host::String
, keys::ArrayInt}-- | Function to be called from javascript land.main::Foreign->EffectUnit
main =
decodeForeign >>> case _ ofLeft errors ->Exception.throw (intercalate "\n" errors)
Right options -> run options
-- | _Actually_ run the program given the necessary `Options`.run::Options->EffectUnit
run _ = Console.log "nice"// index.jsimport{main}from"./output/Example/index.js";main({port: 4242,host: "localhost",keys: []});This is useful if:
- You're writing functions (entrypoints or otherwise) to be called from javascript with arguments, and
- You are not necessarily in control of that calling code (in which
case
unsafeCoerceor similar would do), or - You are in control of the calling code but would like to catch your own errors gracefully