| 1 |
module Web.Scotty.Trans.Lazy where |
| 2 |
|
| 3 |
import Control.Monad (join) |
| 4 |
import Control.Monad.IO.Class |
| 5 |
import Data.Bifunctor (bimap) |
| 6 |
import qualified Data.Text.Lazy as T |
| 7 |
|
| 8 |
import Network.HTTP.Types (Status) |
| 9 |
|
| 10 |
import qualified Web.Scotty.Action as Base |
| 11 |
import Web.Scotty.Internal.Types |
| 12 |
|
| 13 |
-- | Throw a "500 Server Error" 'StatusError', which can be caught with 'rescue'. |
| 14 |
-- |
| 15 |
-- Uncaught exceptions turn into HTTP 500 responses. |
| 16 |
raise :: (MonadIO m) => |
| 17 |
T.Text -- ^ Error text |
| 18 |
-> ActionT m a |
| 19 |
raise = Base.raise . T.toStrict |
| 20 |
{-# DEPRECATED raise "Throw an exception instead" #-} |
| 21 |
|
| 22 |
-- | Throw a 'StatusError' exception that has an associated HTTP error code and can be caught with 'rescue'. |
| 23 |
-- |
| 24 |
-- Uncaught exceptions turn into HTTP responses corresponding to the given status. |
| 25 |
raiseStatus :: Monad m => Status -> T.Text -> ActionT m a |
| 26 |
raiseStatus s = Base.raiseStatus s . T.toStrict |
| 27 |
{-# DEPRECATED raiseStatus "Use status, text, and finish instead" #-} |
| 28 |
|
| 29 |
-- | Redirect to given URL. Like throwing an uncatchable exception. Any code after the call to redirect |
| 30 |
-- will not be run. |
| 31 |
-- |
| 32 |
-- > redirect "http://www.google.com" |
| 33 |
-- |
| 34 |
-- OR |
| 35 |
-- |
| 36 |
-- > redirect "/foo/bar" |
| 37 |
redirect :: (Monad m) => T.Text -> ActionT m a |
| 38 |
redirect = Base.redirect . T.toStrict |
| 39 |
|
| 40 |
-- | Get a request header. Header name is case-insensitive. |
| 41 |
header :: (Monad m) => T.Text -> ActionT m (Maybe T.Text) |
| 42 |
header h = fmap T.fromStrict <$> Base.header (T.toStrict h) |
| 43 |
|
| 44 |
-- | Get all the request headers. Header names are case-insensitive. |
| 45 |
headers :: (Monad m) => ActionT m [(T.Text, T.Text)] |
| 46 |
headers = map (join bimap T.fromStrict) <$> Base.headers |
| 47 |
|
| 48 |
-- | Add to the response headers. Header names are case-insensitive. |
| 49 |
addHeader :: MonadIO m => T.Text -> T.Text -> ActionT m () |
| 50 |
addHeader k v = Base.addHeader (T.toStrict k) (T.toStrict v) |
| 51 |
|
| 52 |
-- | Set one of the response headers. Will override any previously set value for that header. |
| 53 |
-- Header names are case-insensitive. |
| 54 |
setHeader :: MonadIO m => T.Text -> T.Text -> ActionT m () |
| 55 |
setHeader k v = Base.addHeader (T.toStrict k) (T.toStrict v) |
| 56 |
|
| 57 |
text :: (MonadIO m) => T.Text -> ActionT m () |
| 58 |
text = Base.textLazy |
| 59 |
|
| 60 |
html :: (MonadIO m) => T.Text -> ActionT m () |
| 61 |
html = Base.htmlLazy |