Giter Club home page Giter Club logo

servant-reason's Introduction

Servant Reason

Build Status reason reason

Automatically derive bindings for Servant APIs in Reason. Originally build by converting servant-elm to Reason. Types are generated using reason-export.

More docs on Hackage.

A full example of how to integrate Servant and Reason is available at servant-reason-example.

Usage

Run the tests if you want a concrete example. They will build a _test-cache directory that contains a sample Reason repository for a servant API. test also contain example output files for different kinds of bindings.

Usage is simple and automatic. If you have an API like the one below

{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators     #-}

import           Reason          (Spec (Spec), specsToDir, toReasonDecoderSource,
                               toReasonTypeSource)
import           GHC.Generics (Generic)
import           Servant.API  ((:>), Capture, Get, JSON)
import           Servant.Reason  (ReasonType, Proxy (Proxy), defReasonImports,
                               generateReasonForAPI)

data Book = Book
    { name :: String
    } deriving (Generic)

instance ReasonType Book

type BooksApi = "books" :> Capture "bookId" Int :> Get '[JSON] Book

You can expose your API to reason with:

module Main where

import Data.Proxy
import Reason
import           Data.Text hiding (intercalate, map)
import Db

main :: IO ()
main = do
  let code = defReasonImports
             : toReasonTypeSource    (Proxy :: Proxy Book)
             : toReasonDecoderSource (Proxy :: Proxy Book)
             : generateReasonForAPI  (Proxy :: Proxy BooksApi))
  writeFile "Api.re" $ intercalate "\n\n" $ map unpack code

That's it. You can now include Api.re in a Reason project.

Reason setup

The generated code needs access to two Reason libraries @glennsl/bs-json and bs-fetch. Get the latest install instructions from the upstream repos, but at the time of writing these were:

npm install --save @glennsl/bs-json
npm install --save bs-fetch

Then add @glennsl/bs-json and bs-fetch to bs-dependencies in your bsconfig.json:

{
  ...
  "bs-dependencies": ["@glennsl/bs-json", "bs-fetch"]
}

servant-reason's People

Contributors

abarbu avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

damianfral

servant-reason's Issues

Generated reason code for processing empty responses (`NoContent` or `()`) does not fail on HTTP 500

Hi @abarbu!

Firstly, Thanks for creating this library. :)

I think there is a problem with the generated reason code. I have an API with a single endpoint that returns empty responses, (), I got some problems with the server, it began to reject requests with HTTP 500's and, the reason client continued succeeding.

It seems the processing of empty responses (NoContent, ()) relies only on the length of the response body. Fetch promises don't get rejected when receiving HTTP errors, so I think it needs to check the status code of the response before proceeding to check the length of the body, 'resolving' a Belt_Result.Error for non 2XX responses.

The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, as soon as the server responds with headers, the Promise will resolve normally (with the ok property of the response set to false if the response isn’t in the range 200–299), and it will only reject on network failure or if anything prevented the request from completing.
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

If you agree, i could work on a PR for this.

Instances of ReasonType for Time types?

This is a great library!

How hard would it be to add ReasonType instances for LocalTime and TimeOfDay from the Time library? These are types that are very frequently encountered in real world Servant apps.

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.