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

How to use this library with Affjax? #51

Open
diegovdc opened this issue Sep 14, 2018 · 5 comments
Open

How to use this library with Affjax? #51

diegovdc opened this issue Sep 14, 2018 · 5 comments

Comments

@diegovdc
Copy link

diegovdc commented Sep 14, 2018

I know that might not be an issue with the library itself, but as Affjax is probably the most famous purescript library for ajax, it would make sense to be able to easily use Simple.JSON with it.

Currently I am getting the following error:

 Could not match type

    NonEmptyList ForeignError

  with type

    ResponseFormatError

It would be nice to have an easy way to have this transformation.

This is my code:

type MyRecordAlias =
  { userId :: Int
  , id :: Int
  , title :: String
  , completed :: Boolean
  }

main = void $ launchAff_ $ do
  res <- AX.request (
    AX.defaultRequest
    {
      url = "https://jsonplaceholder.typicode.com/todos/1",
      method = Left GET,
      responseFormat = ResponseFormat.string
    }
  )
  case res.body >>= JSON.readJSON  of
    Right (r :: MyRecordAlias) -> do
      log "all good"
    Left e -> do
      log "all bad"
@diegovdc
Copy link
Author

diegovdc commented Sep 14, 2018

After a while, I think I got something. This might not be perfect, as I am quite a noob with purescript, but here it is:

transformError (ResponseFormat.ResponseFormatError e _) = cons' e Nil

main = void $ launchAff_ $ do
  res <- AX.request (
    AX.defaultRequest
    {
      url = "https://jsonplaceholder.typicode.com/todos/1",
      method = Left GET,
      responseFormat = ResponseFormat.string
    }
  )
 let body = bimap transfomError identity res.body
  case res.body >>= JSON.readJSON  of
    Right (r :: MyRecordAlias) -> do
      log "all good"
    Left e -> do
      log "all bad"

It was rather easy, perhaps a mention on the docs would be a good idea?

@justinwoo
Copy link
Owner

Yeah, if you'd like you could put that in a new markdown file to go in the docs and PR it

@justinwoo
Copy link
Owner

I'm not so sure about the transform code you have though, doesn't that just end up losing information? Might be better to return a nested either here

@diegovdc
Copy link
Author

diegovdc commented Sep 15, 2018

Yeah, if you'd like you could put that in a new markdown file to go in the docs and PR it

Sure, but...

I'm not so sure about the transform code you have though, doesn't that just end up losing information? Might be better to return a nested either here

So if you can suggest improvements it would be great! (I did not know where to fit the Foreign part of ResponseFormatError )

@dariooddenino
Copy link

@diegovdc I'm using Variant to handle errors (see here)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants