diff --git a/Web/Scotty.hs b/Web/Scotty.hs index deced82..fae5180 100644 --- a/Web/Scotty.hs +++ b/Web/Scotty.hs @@ -23,13 +23,14 @@ module Web.Scotty , middleware, get, post, put, delete, patch, options, addroute, matchAny, notFound, nested, setMaxRequestBodySize -- ** Route Patterns , capture, regex, function, literal - -- ** Accessing the Request, Captures, and Query Parameters + -- ** Accessing the Request and its fields , request, header, headers, body, bodyReader + , jsonData, files + -- ** Accessing Path, Form and Query Parameters , param, params , pathParam, captureParam, formParam, queryParam , pathParamMaybe, captureParamMaybe, formParamMaybe, queryParamMaybe , pathParams, captureParams, formParams, queryParams - , jsonData, files -- ** Modifying the Response and Redirecting , status, addHeader, setHeader, redirect -- ** Setting Response Body @@ -267,6 +268,8 @@ param = Trans.param . toStrict {-# DEPRECATED param "(#204) Not a good idea to treat all parameters identically. Use pathParam, formParam and queryParam instead. "#-} -- | Synonym for 'pathParam' +-- +-- /Since: 0.20/ captureParam :: Trans.Parsable a => Text -> ActionM a captureParam = Trans.captureParam . toStrict @@ -306,11 +309,13 @@ queryParam = Trans.queryParam . toStrict -- NB : Doesn't throw exceptions. In particular, route pattern matching will not continue, so developers -- must 'raiseStatus' or 'throw' to signal something went wrong. -- --- /Since: FIXME/ +-- /Since: 0.21/ pathParamMaybe :: (Trans.Parsable a) => Text -> ActionM (Maybe a) pathParamMaybe = Trans.pathParamMaybe . toStrict -- | Synonym for 'pathParamMaybe' +-- +-- /Since: 0.21/ captureParamMaybe :: (Trans.Parsable a) => Text -> ActionM (Maybe a) captureParamMaybe = Trans.pathParamMaybe . toStrict @@ -318,7 +323,7 @@ captureParamMaybe = Trans.pathParamMaybe . toStrict -- -- NB : Doesn't throw exceptions, so developers must 'raiseStatus' or 'throw' to signal something went wrong. -- --- /Since: FIXME/ +-- /Since: 0.21/ formParamMaybe :: (Trans.Parsable a) => Text -> ActionM (Maybe a) formParamMaybe = Trans.formParamMaybe . toStrict @@ -326,7 +331,7 @@ formParamMaybe = Trans.formParamMaybe . toStrict -- -- NB : Doesn't throw exceptions, so developers must 'raiseStatus' or 'throw' to signal something went wrong. -- --- /Since: FIXME/ +-- /Since: 0.21/ queryParamMaybe :: (Trans.Parsable a) => Text -> ActionM (Maybe a) queryParamMaybe = Trans.queryParamMaybe . toStrict @@ -398,12 +403,18 @@ raw = Trans.raw -- | Access the HTTP 'Status' of the Response +-- +-- /Since: 0.21/ getResponseStatus :: ActionM Status getResponseStatus = Trans.getResponseStatus -- | Access the HTTP headers of the Response +-- +-- /Since: 0.21/ getResponseHeaders :: ActionM ResponseHeaders getResponseHeaders = Trans.getResponseHeaders -- | Access the content of the Response +-- +-- /Since: 0.21/ getResponseContent :: ActionM Content getResponseContent = Trans.getResponseContent diff --git a/Web/Scotty/Action.hs b/Web/Scotty/Action.hs index b73e691..d5da953 100644 --- a/Web/Scotty/Action.hs +++ b/Web/Scotty/Action.hs @@ -358,7 +358,7 @@ queryParam = paramWith QueryParameterNotFound envQueryParams -- NB : Doesn't throw exceptions. In particular, route pattern matching will not continue, so developers -- must 'raiseStatus' or 'throw' to signal something went wrong. -- --- /Since: FIXME/ +-- /Since: 0.21/ pathParamMaybe :: (Parsable a, Monad m) => T.Text -> ActionT m (Maybe a) pathParamMaybe = paramWithMaybe envPathParams @@ -367,7 +367,7 @@ pathParamMaybe = paramWithMaybe envPathParams -- NB : Doesn't throw exceptions. In particular, route pattern matching will not continue, so developers -- must 'raiseStatus' or 'throw' to signal something went wrong. -- --- /Since: FIXME/ +-- /Since: 0.21/ captureParamMaybe :: (Parsable a, Monad m) => T.Text -> ActionT m (Maybe a) captureParamMaybe = paramWithMaybe envPathParams @@ -375,7 +375,7 @@ captureParamMaybe = paramWithMaybe envPathParams -- -- NB : Doesn't throw exceptions, so developers must 'raiseStatus' or 'throw' to signal something went wrong. -- --- /Since: FIXME/ +-- /Since: 0.21/ formParamMaybe :: (Parsable a, Monad m) => T.Text -> ActionT m (Maybe a) formParamMaybe = paramWithMaybe envFormParams @@ -383,7 +383,7 @@ formParamMaybe = paramWithMaybe envFormParams -- -- NB : Doesn't throw exceptions, so developers must 'raiseStatus' or 'throw' to signal something went wrong. -- --- /Since: FIXME/ +-- /Since: 0.21/ queryParamMaybe :: (Parsable a, Monad m) => T.Text -> ActionT m (Maybe a) queryParamMaybe = paramWithMaybe envQueryParams @@ -428,7 +428,7 @@ paramWithMaybe f k = do params :: Monad m => ActionT m [Param] params = paramsWith getParams {-# DEPRECATED params "(#204) Not a good idea to treat all parameters identically. Use pathParams, formParams and queryParams instead. "#-} - + -- | Get path parameters pathParams :: Monad m => ActionT m [Param] pathParams = paramsWith envPathParams @@ -455,12 +455,18 @@ getParams e = envPathParams e <> envFormParams e <> envQueryParams e -- === access the fields of the Response being constructed -- | Access the HTTP 'Status' of the Response +-- +-- /SINCE 0.21/ getResponseStatus :: (MonadIO m) => ActionT m Status getResponseStatus = srStatus <$> getResponseAction -- | Access the HTTP headers of the Response +-- +-- /SINCE 0.21/ getResponseHeaders :: (MonadIO m) => ActionT m ResponseHeaders getResponseHeaders = srHeaders <$> getResponseAction -- | Access the content of the Response +-- +-- /SINCE 0.21/ getResponseContent :: (MonadIO m) => ActionT m Content getResponseContent = srContent <$> getResponseAction diff --git a/Web/Scotty/Trans.hs b/Web/Scotty/Trans.hs index d78eda3..4bdc5c2 100644 --- a/Web/Scotty/Trans.hs +++ b/Web/Scotty/Trans.hs @@ -28,13 +28,14 @@ module Web.Scotty.Trans , middleware, get, post, put, delete, patch, options, addroute, matchAny, notFound, setMaxRequestBodySize -- ** Route Patterns , capture, regex, function, literal - -- ** Accessing the Request, Captures, and Query Parameters + -- ** Accessing the Request and its fields , request, Lazy.header, Lazy.headers, body, bodyReader + , jsonData, files + -- ** Accessing Path, Form and Query Parameters , param, params , pathParam, captureParam, formParam, queryParam , pathParamMaybe, captureParamMaybe, formParamMaybe, queryParamMaybe , pathParams, captureParams, formParams, queryParams - , jsonData, files -- ** Modifying the Response and Redirecting , status, Lazy.addHeader, Lazy.setHeader, Lazy.redirect -- ** Setting Response Body diff --git a/Web/Scotty/Util.hs b/Web/Scotty/Util.hs index 7a04135..cd278f9 100644 --- a/Web/Scotty/Util.hs +++ b/Web/Scotty/Util.hs @@ -1,5 +1,6 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE LambdaCase #-} +{-# options_ghc -Wno-unused-imports #-} module Web.Scotty.Util ( lazyTextToStrictByteString , strictByteStringToLazyText diff --git a/changelog.md b/changelog.md index 9341363..267a3a1 100644 --- a/changelog.md +++ b/changelog.md @@ -1,19 +1,28 @@ ## next [????.??.??] -* add getResponseHeaders, getResponseStatus, getResponseContent (#214) + +## 0.21 [2023.12.17] +### New +* add `getResponseHeaders`, `getResponseStatus`, `getResponseContent` (#214) * add `captureParamMaybe`, `formParamMaybe`, `queryParamMaybe` (#322) -* deprecate `rescue` and `liftAndCatchIO` (#332) * add `Web.Scotty.Trans.Strict` and `Web.Scotty.Trans.Lazy` (#334) -* Reverted the `MonadReader` instance of `ActionT` so that it inherits the base monad (#342) * renamed `captureParam`, `captureParamMaybe`, and `captureParams` to `pathParam`, `pathParamMaybe`, `pathParams` respectively, keeping the old names as their synonyms (#344) + +### Deprecated +* deprecate `rescue` and `liftAndCatchIO` (#332) +* Deprecate `StatusError`, `raise` and `raiseStatus` (#351) + +### Fixes +* Reverted the `MonadReader` instance of `ActionT` so that it inherits the base monad (#342) * Scotty's API such as `queryParam` now throws `ScottyException` rather than `StatusException`. Uncaught exceptions are handled by `scottyExceptionHandler`, resembling the existing behaviour -* Deprecate `StatusError`, `raise` and `raiseStatus` (#351) + +### Documentation * Add doctest, refactor some inline examples into doctests (#353) * document "`defaultHandler` only applies to endpoints defined after it" (#237) -## 0.20.1 [2023.10.03] +## 0.20.1 [2023.10.03] * remove dependencies on 'base-compat' and 'base-compat-batteries' (#318) * re-add MonadFail (ActionT m) instance (#325) * re-add MonadError (ActionT m) instance, but the error type is now specialized to 'StatusError' (#325) diff --git a/scotty.cabal b/scotty.cabal index 660f270..b79e8e2 100644 --- a/scotty.cabal +++ b/scotty.cabal @@ -1,5 +1,5 @@ Name: scotty -Version: 0.20.1 +Version: 0.21 Synopsis: Haskell web framework inspired by Ruby's Sinatra, using WAI and Warp Homepage: https://github.com/scotty-web/scotty Bug-reports: https://github.com/scotty-web/scotty/issues diff --git a/test/Web/ScottySpec.hs b/test/Web/ScottySpec.hs index 1b04bdb..c73b5f5 100644 --- a/test/Web/ScottySpec.hs +++ b/test/Web/ScottySpec.hs @@ -140,6 +140,16 @@ spec = do request "POST" "/" [("Content-Type","multipart/form-data; boundary=--33")] large `shouldRespondWith` 200 + describe "middleware" $ do + context "can rewrite the query string (#348)" $ do + withApp (do + Scotty.middleware $ \app req sendResponse -> + app req{queryString = [("query", Just "haskell")]} sendResponse + Scotty.matchAny "/search" $ queryParam "query" >>= text + ) $ do + it "returns query parameter with given name" $ do + get "/search" `shouldRespondWith` "haskell" + describe "ActionM" $ do context "MonadBaseControl instance" $ do @@ -178,16 +188,6 @@ spec = do it "Responds with a 302 Redirect" $ do get "/a" `shouldRespondWith` 302 { matchHeaders = ["Location" <:> "/b"] } - describe "middleware" $ do - context "rewrites the query string" $ do - withApp (do - Scotty.middleware $ \app req sendResponse -> - app req{queryString = [("query", Just "haskell")]} sendResponse - Scotty.matchAny "/search" $ queryParam "query" >>= text - ) $ do - it "returns query parameter with given name" $ do - get "/search" `shouldRespondWith` "haskell" - describe "captureParam" $ do withApp ( do