smaccoun / servant-ts

See the docs and live playground here

Home Page:https://smaccoun.github.io/servant-ts/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CircleCI

Servant TS

Instantly generate highly configurable typescript requests and defintion from your Servant API

Live playground of code and output here:

https://smaccoun.github.io/servant-ts/

This library relies heavily on the companion library:

aeson-generic-ts

Example

Consider the following common User API

type UserAPI = "user" :> Get '[JSON] [User]
              :<|> "user" :> Capture "userId" Int :> Get '[JSON] User

data User = User
    {name    :: Text
    ,age     :: Int
    ,isAdmin :: Bool
    ,hasMI   :: Maybe Text
    } deriving (Generic, TypescriptType)

Given a flavor configuration you can auto generate with the fullowing function the below typescript files.

main :: IO ()
main =
  apiToTSDocs asTS reqToTSFunction outputFileLocs
  where
    outputFileLocs = OutputFileNames "Server/types.tsx" "Server/api.tsx"
    asTS = servantToReqTS (Proxy :: Proxy FpTs) (Proxy :: Proxy SimpleAPI)
    reqToTSFunction = defaultReqToTSFunction (Proxy @Fetch)
// Declarations

interface User { 
  name : string
  age : number
  isAdmin : boolean
  hasMI : Option<string>
}
Array<User>
interface User { 
  name : string
  age : number
  isAdmin : boolean
  hasMI : Option<string>
}
// Function Declarations

function getUser(): Promise<Array<User>> {
  return fetch(withRemoteBaseUrl(`user`))
}
function getUserByUserId(userId : number): Promise<User> {
  return fetch(withRemoteBaseUrl(`user/${userId}`))
}

About

See the docs and live playground here

https://smaccoun.github.io/servant-ts/

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Haskell 54.9%Language:TypeScript 33.1%Language:JavaScript 5.7%Language:Shell 2.6%Language:Makefile 2.0%Language:HTML 1.5%Language:SCSS 0.2%