wai-middleware-content-type: Route to different middlewares based on the incoming Accept header.

[ bsd3, library, web ] [ Propose Tags ]
Versions [RSS] 0.0.0, 0.0.1, 0.0.2, 0.0.3,,,, 0.0.4, 0.1.0,, 0.1.1,, 0.2.0, 0.2.1, 0.3.0, 0.4.0, 0.4.1, 0.5.0,, 0.5.1, 0.5.2, 0.6.0, 0.6.1,,, 0.6.2 (info)
Dependencies aeson, base (>=4.11 && <5), blaze-builder, blaze-html, bytestring, clay, exceptions, extractable-singleton, hashable, http-media, http-types, lucid, mmorph, monad-control, monad-control-aligned, monad-logger, mtl, resourcet, shakespeare, text, transformers, transformers-base, unordered-containers, urlpath (>=9.0.1), wai (>=3.2), wai-logger, wai-transformers (>=0.1.0) [details]
License BSD-3-Clause
Copyright 2018 Athan Clark
Author Athan Clark
Maintainer athan.clark@localcooking.com
Category Web
Home page https://github.com/athanclark/wai-middleware-content-type#readme
Bug tracker https://github.com/athanclark/wai-middleware-content-type/issues
Source repo head: git clone https://github.com/athanclark/wai-middleware-content-type
Uploaded by athanclark at 2018-11-21T04:25:29Z
Distributions NixOS:0.6.2
Reverse Dependencies 2 direct, 1 indirect [details]
Downloads 10942 total (86 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-11-21 [all 1 reports]

Readme for wai-middleware-content-type-0.6.2

[back to package description]


Route middlewares based on the incoming Accept HTTP header, and other hints like the file extension (foo.txt) of the route requested.


This package provides many combinators for turning various data types into the response you'd expect. For instance, blaze-html gives us strictly Html data, right? We can be sure to only respond with Html-compatible requests with our toolset:

import Network.Wai.Middleware.ContentType
import Network.Wai.Trans

myMiddleware :: MiddlewareT (ReaderT Env m)

contentTypeRoutes :: Monad m =>
                     FileExtListenerT (Status -> ResponseHeaders -> Response) m ()
contentTypeRoutes = do
  blaze myBlazeResponse
  cassius myCassiusResponse
  text myTextResponse

contentMiddleware :: Monad m => MiddlewareT m
contentMiddleware app req respond =
  fileExtsToMiddleware $
    mapResponse (\f -> f status200 []) contentTypeRoutes

This library was designed for use with nested-routes, but it's all good if you want to use it separately.