1+ {-# LANGUAGE CPP #-}
12{-# LANGUAGE DataKinds #-}
23{-# LANGUAGE DeriveGeneric #-}
34{-# LANGUAGE RecordWildCards #-}
@@ -9,13 +10,19 @@ module XHR (start) where
910
1011-- slightly adapted from https://github.com/dmjio/miso/blob/master/examples/xhr/Main.hs
1112
13+ #ifdef wasi_HOST_OS
14+ import GHC.Wasm.Prim
15+ #else
16+ import Data.JSString (JSString )
17+ import Language.Javascript.JSaddle (fromJSString , toJSString )
18+ #endif
19+
1220import Control.Monad.IO.Class
1321import Data.Aeson
1422import qualified Data.Map as M
1523import Data.Maybe
1624import qualified Data.Text as T
1725import GHC.Generics
18- import GHC.Wasm.Prim
1926
2027import Miso hiding (defaultOptions )
2128import Miso.String
@@ -135,14 +142,19 @@ instance FromJSON APIInfo where
135142getGitHubAPIInfo :: JSM APIInfo
136143getGitHubAPIInfo = do
137144 resp <- liftIO $
138- T. pack . fromJSString <$> js_fetch (toJSString " https://api.github.com" )
145+ T. pack . fromJSString <$> js_fetch (toJSString ( " https://api.github.com" :: String ) )
139146 case eitherDecodeStrictText resp :: Either String APIInfo of
140147 Left s -> error s
141148 Right j -> pure j
142149
150+ #ifdef wasi_HOST_OS
143151-- We use the WASM JS FFI here to access the more modern fetch API. If you want
144152-- your code to eg also work when compiling with non-cross GHC and using
145153-- jsaddle-warp, you can use fetch or XMLHttpRequest via JSaddle, for example
146154-- via ghcjs-dom, servant-jsaddle or servant-client-js.
147155foreign import javascript safe " const r = await fetch($1); return r.text();"
148156 js_fetch :: JSString -> IO JSString
157+ #else
158+ js_fetch :: JSString -> IO JSString
159+ js_fetch = error " not implemented"
160+ #endif
0 commit comments