-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPosts.purs
More file actions
46 lines (42 loc) · 1.53 KB
/
Posts.purs
File metadata and controls
46 lines (42 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Pages.Api.Posts (fetchData) where
import Prelude
import Affjax as AX
import Affjax.ResponseFormat as ResponseFormat
import Config (apiEndpoint)
import Control.Promise (Promise, fromAff)
import Data.Either (Either(..), either)
import Data.HTTP.Method (Method(..))
import Effect.Aff (Aff)
import Effect.Aff.Compat (mkEffectFn2)
import Effect.Class (liftEffect)
import Effect.Console as Console
import Effect.Uncurried (EffectFn2)
import Next.Response (sendString)
import Node.HTTP (Request, Response, setStatusCode)
--fetchData :: forall ctx. ctx -> Aff Props
_fetchData :: Request -> Response -> Aff Unit -- (Either Error String)
_fetchData _req resp = do
res <- AX.request (AX.defaultRequest { url = apiEndpoint <> "/posts/1", method = Left GET, responseFormat = ResponseFormat.string })
liftEffect
$ do
Console.log $ either AX.printError _.body res
case res of
Left e -> do
setStatusCode resp 500
sendString resp (AX.printError e)
Right r -> do
setStatusCode resp 200
sendString resp (r.body)
--pure ((arg <> _) <<< _.body <$> res)
fetchData :: EffectFn2 Request Response (Promise Unit)
fetchData = mkEffectFn2 f
where
f req resp = fromAff $ _fetchData req resp
-- _sayHi :: String -> Aff String
-- _sayHi name = do
-- delay $ Milliseconds 100.0
-- pure $ "hello " <> name
-- sayHiDelayed :: String -> Effect (Promise String)
-- sayHiDelayed name = fromAff $ _sayHi name
-- sayHiNow :: EffectFn1 String (Promise String)
-- sayHiNow = mkEffectFn1 sayHiDelayed