All Projects → Woody88 → purescript-swerve

Woody88 / purescript-swerve

Licence: MIT License
Swerve is a library that offers a type-level DSL for describing server and client web applications. Inspired by Haskell's Servant library.

Programming Languages

purescript
368 projects
Dhall
116 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to purescript-swerve

simple-php-api
An extremely simple API (with authentication) example, written in PHP (server) and JS (client), using JSON
Stars: ✭ 23 (+15%)
Mutual labels:  api-client, api-server
noire-server
Hapi Boilerplate
Stars: ✭ 20 (+0%)
Mutual labels:  api-client, api-server
Openapi Generator
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
Stars: ✭ 10,634 (+53070%)
Mutual labels:  api-client, api-server
pimba
Pimba is a minimalist command-line tool written in Go to publish and serve static files.
Stars: ✭ 12 (-40%)
Mutual labels:  api-client, api-server
drowsy
😪 Lazy integrations tool for RESTful interfaces to aid POC development and streamline integrations
Stars: ✭ 19 (-5%)
Mutual labels:  api-client, api-server
webapi
WAI based library for web api
Stars: ✭ 27 (+35%)
Mutual labels:  api-client, api-server
public-information-map-template-js
An ArcGIS Online mapping template to showcase social media on a map for disaster response and public information.
Stars: ✭ 66 (+230%)
Mutual labels:  web-application
r5r
ipeagit.github.io/r5r/
Stars: ✭ 90 (+350%)
Mutual labels:  routing
vsphere-automation-sdk-rest
REST (Postman and JavaScript) samples and API reference documentation for vSphere using the VMware REST API
Stars: ✭ 194 (+870%)
Mutual labels:  api-client
go router
The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
Stars: ✭ 380 (+1800%)
Mutual labels:  routing
Swiftly
Swiftly is an easy to use Qt/C++ web framework
Stars: ✭ 20 (+0%)
Mutual labels:  web-application
siteleaf-node
Node library for Siteleaf v2 API
Stars: ✭ 14 (-30%)
Mutual labels:  api-client
ContextIO-node
[DEPRECATED] - Official Node.js client library for the Context.IO Email API
Stars: ✭ 86 (+330%)
Mutual labels:  api-client
youtube-deno
A Deno client library of the YouTube Data API.
Stars: ✭ 30 (+50%)
Mutual labels:  api-client
GenomicDataCommons
Provide R access to the NCI Genomic Data Commons portal.
Stars: ✭ 64 (+220%)
Mutual labels:  api-client
rack-idempotency
Rack middleware for idempotency guarantees in mutating endpoints.
Stars: ✭ 20 (+0%)
Mutual labels:  api-server
yew-router
Router extension to yew
Stars: ✭ 27 (+35%)
Mutual labels:  routing
adyen-dotnet-api-library
Adyen API Library for .NET
Stars: ✭ 69 (+245%)
Mutual labels:  api-client
fusionauth-typescript-client
A TypeScript client for FusionAuth
Stars: ✭ 37 (+85%)
Mutual labels:  api-client
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (+190%)
Mutual labels:  routing

License CI

Swerve

Swerve is a library that offers a type-level DSL for describing server and client web applications.

Installation

This library is not yet published to pursuit.
You will need to use purescript 0.14 and above
You can install this package by adding the following in your packages.dhall:

in  upstream
  with swerve = 
    { dependencies =
      [ "affjax"
      , "argonaut"
      , "argonaut-codecs"
      , "arrays"
      , "b64"
      , "console"
      , "debug"
      , "debugged"
      , "effect"
      , "form-urlencoded"
      , "heterogeneous"
      , "http-media"
      , "http-types"
      , "node-http"
      , "psci-support"
      , "wai"
      , "warp"
      ]
    , repo = "https://github.com/Woody88/purescript-swerve.git"
    , version = "master"
    }
  with b64 = 
    { dependencies =
      [ "console", "effect", "node-fs-aff", "wai", "encoding" ]
    , repo = "https://github.com/CarstenKoenig/purescript-b64.git"
    , version = "purescript-0.14"
    }
  with warp =
    { dependencies =
      [ "console", "effect", "node-fs-aff", "wai" ]
    , repo = "https://github.com/Woody88/purescript-warp.git"
    , version = "master"
    }
  with debugged =
    { dependencies =
      [ "console"
      , "effect"
      , "prelude"
      , "strings"
      , "record"
      , "ordered-collections"
      , "either"
      , "tuples"
      , "lists"
      , "arrays"
      , "bifunctors"
      , "datetime"
      , "enums"
      ]
    , repo = "https://github.com/Woody88/purescript-debugged.git"
    , version = "ps-0.14"
    }
  with wai =
    { dependencies = [ "effect", "aff", "http-types", "node-net" ]
    , repo = "https://github.com/Woody88/purescript-wai.git"
    , version = "master"
    }
  with http-types =
    { dependencies = [ "js-uri", "tuples", "unicode" ]
    , repo = "https://github.com/Woody88/purescript-http-types.git"
    , version = "master"
    }
  with http-media =
    { dependencies =
      [ "console"
      , "effect"
      , "exceptions"
      , "foldable-traversable"
      , "maybe"
      , "newtype"
      , "numbers"
      , "ordered-collections"
      , "psci-support"
      , "strings"
      , "stringutils"
      , "unicode"
      ]
    , repo = "https://github.com/Woody88/purescript-http-media.git"
    , version = "master"
    }
user@user:~$ spago install swerve

Usage

Basic Example

For more examples please refer to the test folder.

import Prelude

import Data.Debug.Eval as D
import Data.Maybe (Maybe(..))
import Data.Newtype (unwrap, wrap)
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Aff as Aff
import Effect.Class (liftEffect)
import Effect.Class.Console as Console
import Network.HTTP.Types (hAuthorization, hContentType, ok200)
import Network.Wai (Application, Response(..), defaultRequest, responseStr) as Wai
import Network.Warp.Settings (defaultSettings)
import Network.Warp.Run (runSettings)
import Swerve.API
import Swerve.Server 
import Swerve.Server (lift, compose) as Server
import Test.Stream (newStream)
import Type.Proxy (Proxy(..))

-- API Spec
type API = Record 
    ( users :: "users" :> UserAPI 
    , raw   :: RawApp
    )

type UserAPI = Record 
    ( get :: GetUser 
    )

type RawApp = "raw" :> Raw 

type User = String
type UserId = Int  
type MaxAge = Int 
type Authorization = String 

type GetUser 
  =  Capture "userId" UserId 
  :> QueryParam "maxAge" MaxAge 
  :> Header "authorization" Authorization 
  :> ReqBody PlainText String  
  :> Get JSON (Ok User + BadRequest + NotFound + Nil)

-- Handlers 
getUser 
  :: UserId 
  -> Maybe MaxAge 
  -> Authorization 
  -> String 
  -> Handler (Ok User + BadRequest + NotFound + Nil)
getUser userId _ _ body = case userId of 
  13        -> pure <<< respond (Proxy :: _ BadRequest') $ "Invalid User Id"
  17        -> pure <<< respond (Proxy :: _ NotFound') $ mempty
  otherwise -> do
    Console.log $ "Body: " <> body
    pure <<< respond (Proxy :: _ Ok') $ "User" <> show userId

rawApp :: HandlerM Wai.Application
rawApp = pure $ \req send -> send $ Wai.responseStr ok200 [] "Hello, World!"

-- Servers 
raw :: Server RawApp 
raw = Server.lift rawApp 

users :: Server UserAPI
users = Server.lift { get: getUser } 

server :: Server API 
server = Server.compose { users, raw }

-- WAI Application
app :: Wai.Application
app = serve (Proxy :: _ API) server

main :: Effect Unit
main = do 
    let beforeMainLoop = Console.log $ "Listening on port " <> show defaultSettings.port
    void $ runSettings defaultSettings { beforeMainLoop = beforeMainLoop } app
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].