Giter VIP home page Giter VIP logo

ghcjs-servant-client's Introduction

servant-client

servant

This library lets you automatically derive Haskell functions that let you query each endpoint of a servant webservice.

Example

type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
        :<|> "books" :> ReqBody Book :> Post '[JSON] Book -- POST /books

myApi :: Proxy MyApi
myApi = Proxy

getAllBooks :: EitherT String IO [Book]
postNewBook :: Book -> EitherT String IO Book
-- 'client' allows you to produce operations to query an API from a client.
(getAllBooks :<|> postNewBook) = client myApi host
  where host = BaseUrl Http "localhost" 8080

Testing

Testing right now is a bit fragile. It makes the following assumptions:

You are running the GHC servant-server in test-server:

cd test-server
stack build
stack exec test-server

And that you have the node package xmlhttprequest in the node directory that GHJCS is pointing to (on my computer it is ~/node_modules, it may differ on your system).

Then you can run:

stack setup
stack test

ghcjs-servant-client's People

Contributors

cotrone avatar mchaver avatar msewell17 avatar ondrap avatar shulhi avatar soenkehahn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ghcjs-servant-client's Issues

Runtime error

I'm getting a strange error while trying to use ghcjs-servant-client with ghcjs-0.2.1.9008021_ghc-8.0.2, lts-8.21.

error

This is the relevant code:

        performEventAsync $
        flip fmap ue $ \(u, t) callback -> do
            void $
                liftIO $
                forkIO $ do
                    burl <- parseBaseUrl "http://localhost:8000"
                    errOrRes <-
                        runEitherT $
                        (client (Proxy :: Proxy AddItem) (Just burl)) t
                    callback $
                        case errOrRes of
                            Left err -> ErrorWhileSyncing u err
                            Right r -> SyncedAfterAdding u r

Any help would be greatly appreciated.

Support servant-0.7.1?

Leaving this issue here as I'd like to understand whether newer versions of servant will be supported.

Optional BaseUrl for client

When running inside a browser, the BaseUrl doesn't have to be set (unless one wants to use CORS). It seems to me that it would make sense to change the BaseUrl parameter to Maybe BaseUrl. It would allow to declare all the functions as top-level in a module. Along with some type signature changes, this seems to do the trick:

buildUrl :: Req -> Maybe BaseUrl -> URI
buildUrl req@(Req path qText mBody rAccept hs) baseurl =
  (baseURI baseurl){uriPath = path, uriQuery = query}
  where
    query = unpack $ renderQuery True $ queryTextToQuery qText
    baseURI Nothing = nullURI
    baseURI (Just (BaseUrl scheme host port)) =
      nullURI {
        uriScheme = schemeText,
        uriAuthority = Just $ URIAuth "" host portText
      }
      where
        portText = ":" <> (show port)
        buildQuery request = unpack $ renderQuery True $ queryTextToQuery qText
        schemeText = case scheme of
                            Http -> "http:"
                            Https -> "https:"

Hardcoded response type blocks response body in the case of server error

Currently the response type is hardcoded as "json". If the server returns response of different type, typically in te case of server error, parsing fails and we get null response body.

It would be better not to set response type and leave parsing the response to the api user.

I wonder if doing this would break processing in the non-error case.. can somebody comment?

I can do a pull request if this change is deemed correct.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.