Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pages/api/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { fetchData as default } from "../../output/Pages.Api.Posts/index.js"
1 change: 1 addition & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
, "either"
, "http-methods"
, "maybe"
, "node-http"
, "prelude"
, "react-basic-dom"
, "react-basic-hooks"
Expand Down
3 changes: 3 additions & 0 deletions src/Next/Response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports._sendString = function(res, str) {
res.send(str)
}
11 changes: 11 additions & 0 deletions src/Next/Response.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Next.Response (sendString) where

import Prelude
import Effect (Effect)
import Effect.Uncurried (runEffectFn2, EffectFn2)
import Node.HTTP (Response)

foreign import _sendString :: EffectFn2 Response String Unit

sendString :: Response -> String -> Effect Unit
sendString = runEffectFn2 _sendString
46 changes: 46 additions & 0 deletions src/Pages/Api/Posts.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
Comment on lines +39 to +46
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this testing code? Can it be removed?

4 changes: 2 additions & 2 deletions src/Pages/Home.purs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mkHome = do
, R.p
{ className: "pt-4 text-sm"
, children:
[ R.text "Welcome to my Next.js with Purescript Example!"
[ R.text "Purescript Example - changed!"
]
}
]
Expand All @@ -59,7 +59,7 @@ mkHome = do
, children:
[ R.img
{ className: "shadow-2xl"
, src: "https://source.unsplash.com/IuLgi9PWETU"
, src: "https://source.unsplash.com/cvBBO4PzWPg"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My coffee! 😲

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
, src: "https://source.unsplash.com/cvBBO4PzWPg"
, src: "https://source.unsplash.com/tU3Bl2If2RM"

}
]
}
Expand Down