servant-response-0.1: Machinery to express how servant should turn results of database operations into proper JSON-encodable response types

Safe HaskellNone

Servant.Response.Prelude

Contents

Synopsis

Response class

Useful reusable types and instances

data UpdateResponse o Source

A generic response type for any effectul operation on a Resource, like adding, updating or deleting an item.

It simply holds a Bool that indicates whether the operation was successful or not, and if it wasn't, it'll embed a text describing what went wrong and is meant to be tagged (see the o type parameter) with the operation it's associated to.

This lets us have different instances for the standard Add and Update operations for example, where the former should respond with HTTP status code 201 if the entry was created, whereas the latter should just use status code 200.

You can of course skip this one and use a more appropriate for your particular application.

Constructors

UpdateResponse 

Fields

success :: !Bool
 
msg :: !Text
 

Instances

Eq (UpdateResponse o) 
Show (UpdateResponse o) 
Generic (UpdateResponse o) 
ToJSON (UpdateResponse o)

e.g:

 { "success" : false, "msg" : "couldn't add item: blabla"}

data LookupResponse a Source

A generic response type for an operation performing some kind of (potentially failing) lookup of an item

This is useful when writing a web application, where you want to send for example a JSON message saying the item wasn't found along with status 404 when the item isn't found, but just send the item if it could be found. This is (purposefully) isomorphic to Maybe.

Constructors

NotFound 
Found !a 

Instances

Eq a => Eq (LookupResponse a) 
Show a => Show (LookupResponse a) 
ToJSON a => ToJSON (LookupResponse a)

If you have some type convertible to JSON, you can wrap it in LookupResponse whenever you are looking up a value associated to some identifier where the lookup may fail. It'll send the JSON-encoded value if found or

 { "message" : "Not found" }

if not found. This makes sure you send valid JSON through the wires even when the target doesn't exist.

ToJSON a => Response (LookupResponse a) (Maybe a)

Make LookupResponse a proper Response for Context lookups returning a Maybe value, returning 404 when Nothing is returned, along with a not found message in json. Used by View.