Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated code to purescript 0.12 and Generic.Rep #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
/src/.webpack.js
/setupPath.sh
/.idea
.psc-ide-port
.vscode/
25 changes: 13 additions & 12 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
"output"
],
"dependencies": {
"purescript-console": "^3.0.0",
"purescript-prelude": "^3.1.0",
"purescript-either": "^3.1.0",
"purescript-argonaut-core": "^3.1.0",
"purescript-globals": "^3.0.0",
"purescript-foldable-traversable": "^3.6.1",
"purescript-nullable": "^3.0.0",
"purescript-dom": "^4.9.0",
"purescript-affjax": "^5.0.0",
"purescript-argonaut-generic-codecs": "^6.0.4"
"purescript-console": "^4.1.0",
"purescript-prelude": "^4.0.1",
"purescript-either": "^4.0.0",
"purescript-foldable-traversable": "^4.0.0",
"purescript-generics-rep": "^6.0.0",
"purescript-effect": "^2.0.0",
"purescript-aff": "^5.0.0",
"purescript-exceptions": "^4.0.0",
"purescript-web-xhr": "^2.0.0",
"purescript-argonaut-generic": "https://github.com/CarstenKoenig/purescript-argonaut-generic.git#6402a87c8a35b4028429d270ca23af4eefd7e192",
"purescript-affjax": "^6.0.0"
},
"devDependencies": {
"purescript-psci-support": "^3.0.0"
"purescript-psci-support": "^4.0.0"
}
}
}
96 changes: 0 additions & 96 deletions src/Servant/PureScript/Affjax.js

This file was deleted.

123 changes: 0 additions & 123 deletions src/Servant/PureScript/Affjax.purs

This file was deleted.

84 changes: 84 additions & 0 deletions src/Servant/PureScript/Ajax.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{--
This file contains code copied from the purescript-affjax project from slamdata.
It is therefore licensed under Apache License version 2.0.
--}

module Servant.PureScript.Ajax where

import Prelude

import Control.Monad.Error.Class (class MonadError, catchError, throwError)
import Data.Argonaut.Decode.Generic.Rep (class DecodeRep, genericDecodeJson)
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Effect.Aff (Aff, message)
import Effect.Aff.Class (class MonadAff, liftAff)
import Network.HTTP.Affjax (AffjaxRequest, AffjaxResponse, affjax)
import Network.HTTP.Affjax.Response as Response
import Servant.PureScript.JsUtils (unsafeToString)


newtype AjaxError res
= AjaxError
{ request :: AffjaxRequest
, description :: ErrorDescription res
}

data ErrorDescription res
= UnexpectedHTTPStatus (AffjaxResponse res)
| ParsingError String
| DecodingError String
| ConnectionError String


makeAjaxError :: forall res. AffjaxRequest -> ErrorDescription res -> AjaxError res
makeAjaxError req desc =
AjaxError
{ request : req
, description : desc
}

runAjaxError :: forall res. AjaxError res -> { request :: AffjaxRequest, description :: ErrorDescription res }
runAjaxError (AjaxError err) = err

errorToString :: forall res. AjaxError res -> String
errorToString = unsafeToString

requestToString :: AffjaxRequest -> String
requestToString = unsafeToString

responseToString :: forall res. AffjaxResponse res -> String
responseToString = unsafeToString


-- | Do an affjax call but report Aff exceptions in our own MonadError
ajax :: forall m res rep. Generic res rep => DecodeRep rep => MonadError (AjaxError res) m => MonadAff m
=> AffjaxRequest -> m (AffjaxResponse res)
ajax req = do
jsonResponse <- liftWithError $ affjax Response.json req
decoded <- toDecodingError $ genericDecodeJson jsonResponse.response
pure
{ status: jsonResponse.status
, statusText: jsonResponse.statusText
, headers: jsonResponse.headers
, response: decoded
}
where
liftWithError :: forall a. Aff a -> m a
liftWithError action = do
res <- liftAff $ toEither action
toAjaxError res

toEither :: forall a. Aff a -> Aff (Either String a)
toEither action = catchError (Right <$> action) $ \e ->
pure $ Left (message e)

toAjaxError :: forall a. Either String a -> m a
toAjaxError r = case r of
Left err -> throwError $ makeAjaxError req $ ConnectionError err
Right v -> pure v

toDecodingError :: forall a. Either String a -> m a
toDecodingError r = case r of
Left err -> throwError $ makeAjaxError req $ DecodingError err
Right v -> pure v
10 changes: 10 additions & 0 deletions src/Servant/PureScript/JsUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* JsUtils exports */
"use strict";

// module JsUtils

exports.encodeUriComponent = encodeURIComponent;

exports.unsafeToString = function (obj) {
return JSON.stringify(obj, null, 4)
}
8 changes: 8 additions & 0 deletions src/Servant/PureScript/JsUtils.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- | This module defines types for some global Javascript functions
-- | and values.
module Servant.PureScript.JsUtils where

-- | uri component encoding
foreign import encodeUriComponent :: String -> String

foreign import unsafeToString :: forall obj. obj -> String
Loading