A type operator lets a type-level API description combine routing, request, and response information. Servant uses TypeOperators alongside DataKinds to build typed API definitions. section 17.1.1
Use Servant's operators to assemble endpoint descriptions:
- Use (:>) to connect path segments and request or response details into one endpoint description. section 17.1.1
- Use (:<|>) to combine multiple endpoint descriptions into one API. section 17.1.1 section 17.4
- Combine operators with Get, PostCreated, PatchNoContent, ReqBody, Capture, and JSON to describe HTTP behavior and types. section 17.1.1
Todo API type
A todo API can combine an entries endpoint, an add endpoint accepting a TodoEntry request body, and a toggle endpoint capturing an integer identifier. The resulting type describes three endpoints in one API. section 17.1.1
Listing 17.1 A type for the todo list API server
{-# LANGUAGE TypeOperators #-} #1
{-# LANGUAGE DataKinds #-} #1
module Todos.Api where
import Servant.API
import Todos.Types
type TodoApi =
"entries" :> Get '[JSON] [TodoEntry] #2
:<|> "add" :> ReqBody '[JSON] TodoEntry :>
PostCreated '[JSON] Int #3
:<|> "toggle" :> Capture "id" Int :> PatchNoContent #4
Inspect the complete TodoApi declaration, including the required language extensions and each operator-based endpoint. section 17.1.1
