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

Stabilityexperimental
MaintainerAlp Mestanogullari <alp@zalora.com>
Safe HaskellNone

Servant.Response

Contents

Description

This module contains a generic Response class for tying the result of some "database operation" to some response type of yours, or to the standard ones from Servant.Scotty.Prelude for example.

Synopsis

The Response class

class ToJSON resp => Response resp result | result -> resp whereSource

A class that ties return types of your database operations and the output that will be generated to communicate the result.

  • The first type, resp, is the response type that will be encoded in JSON and sent as the response body.
  • The second type, result, is the result type of your "database" or "context" operation.

For example, if you're adding an item, and if you're using postgresql-simple, you'll probably want to use the Response instances defined in the servant-postgresql package, in the Servant.PostgreSQL.Prelude module.

It lets you specify, given a value of your result, if no exception is thrown, what response should be sent as JSON to the client along with what HTTP status.

There's a functional dependency at play: the result type of a database operation determines the representation that'll be picked for generating the json output.

Methods

toResponse :: result -> (resp, Status)Source

Instances

ToJSON a => Response [a] [a]

Just send the list of entries as a JSON array, with status code 200. Used by ListAll.

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.

Useful for defining your instances