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, 0.7.0 (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 (>=0.0.2), monad-logger, mtl, resourcet, shakespeare, text, transformers, transformers-base, unordered-containers, urlpath (>=11.0.0), wai (>=3.2), wai-logger, wai-transformers (>=0.1.0) [details]
License BSD-3-Clause
Copyright Copyright (c) 2023 Athan Clark
Author Athan Clark
Maintainer athan.clark@gmail.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 2023-09-28T09:25:07Z
Reverse Dependencies 2 direct, 1 indirect [details]
Downloads 11328 total (33 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2023-09-28 [all 1 reports]

Readme for wai-middleware-content-type-0.7.0

[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 urlbase (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.