Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use "pathParam" instead of "captureParam" to refer to path parameters #346

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Web.Scotty

main = scotty 3000 $
get "/:word" $ do
beam <- captureParam "word"
beam <- pathParam "word"
html $ mconcat ["<h1>Scotty, ", beam, " me up!</h1>"]
```

Expand Down
43 changes: 27 additions & 16 deletions Web/Scotty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
-- ** Accessing the Request, Captures, and Query Parameters
, request, header, headers, body, bodyReader
, param, params
, captureParam, formParam, queryParam
, captureParamMaybe, formParamMaybe, queryParamMaybe
, captureParams, formParams, queryParams
, pathParam, captureParam, formParam, queryParam
, pathParamMaybe, captureParamMaybe, formParamMaybe, queryParamMaybe
, pathParams, captureParams, formParams, queryParams
, jsonData, files
-- ** Modifying the Response and Redirecting
, status, addHeader, setHeader, redirect
Expand Down Expand Up @@ -141,12 +141,12 @@
-- ever run is if the first one calls 'next'.
--
-- > get "/foo/:bar" $ do
-- > w :: Text <- captureParam "bar"
-- > w :: Text <- pathParam "bar"
-- > unless (w == "special") next
-- > text "You made a request to /foo/special"
-- >
-- > get "/foo/:baz" $ do
-- > w <- captureParam "baz"
-- > w <- pathParam "baz"
-- > text $ "You made a request to: " <> w
next :: ActionM ()
next = Trans.next
Expand All @@ -158,7 +158,7 @@
-- content the text message.
--
-- > get "/foo/:bar" $ do
-- > w :: Text <- captureParam "bar"
-- > w :: Text <- pathParam "bar"
-- > unless (w == "special") finish
-- > text "You made a request to /foo/special"
--
Expand All @@ -170,12 +170,12 @@
--
-- > raise JustKidding `catch` (\msg -> text msg)
rescue :: E.Exception e => ActionM a -> (e -> ActionM a) -> ActionM a
rescue = Trans.rescue

Check warning on line 173 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

In the use of ‘rescue’

Check warning on line 173 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

In the use of ‘rescue’

Check warning on line 173 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

In the use of ‘rescue’

Check warning on line 173 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

In the use of ‘rescue’

Check warning on line 173 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

In the use of ‘rescue’
{-# DEPRECATED rescue "Use catch instead" #-}

-- | Like 'liftIO', but catch any IO exceptions and turn them into Scotty exceptions.
liftAndCatchIO :: IO a -> ActionM a
liftAndCatchIO = Trans.liftAndCatchIO

Check warning on line 178 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

In the use of ‘liftAndCatchIO’

Check warning on line 178 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

In the use of ‘liftAndCatchIO’

Check warning on line 178 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

In the use of ‘liftAndCatchIO’

Check warning on line 178 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

In the use of ‘liftAndCatchIO’

Check warning on line 178 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

In the use of ‘liftAndCatchIO’
{-# DEPRECATED liftAndCatchIO "Use liftIO instead" #-}

-- | Redirect to given URL. Like throwing an uncatchable exception. Any code after the call to redirect
Expand Down Expand Up @@ -227,18 +227,22 @@
-- This means captures are somewhat typed, in that a route won't match if a correctly typed
-- capture cannot be parsed.
param :: Trans.Parsable a => Text -> ActionM a
param = Trans.param . toStrict

Check warning on line 230 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

In the use of ‘param’

Check warning on line 230 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

In the use of ‘param’

Check warning on line 230 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

In the use of ‘param’

Check warning on line 230 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

In the use of ‘param’

Check warning on line 230 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

In the use of ‘param’
{-# DEPRECATED param "(#204) Not a good idea to treat all parameters identically. Use captureParam, formParam and queryParam instead. "#-}
{-# DEPRECATED param "(#204) Not a good idea to treat all parameters identically. Use pathParam, formParam and queryParam instead. "#-}

-- | Get a capture parameter.
-- | Synonym for 'pathParam'
captureParam :: Trans.Parsable a => Text -> ActionM a
captureParam = Trans.captureParam . toStrict

-- | Get a path parameter.
--
-- * Raises an exception which can be caught by 'catch' if parameter is not found. If the exception is not caught, scotty will return a HTTP error code 500 ("Internal Server Error") to the client.
--
-- * If the parameter is found, but 'parseParam' fails to parse to the correct type, 'next' is called.
--
-- /Since: 0.20/
captureParam :: Trans.Parsable a => Text -> ActionM a
captureParam = Trans.captureParam . toStrict
-- /Since: 0.21/
pathParam :: Trans.Parsable a => Text -> ActionM a
pathParam = Trans.pathParam . toStrict

-- | Get a form parameter.
--
Expand All @@ -261,14 +265,18 @@
queryParam = Trans.queryParam . toStrict


-- | Look up a capture parameter. Returns 'Nothing' if the parameter is not found or cannot be parsed at the right type.
-- | Look up a path parameter. Returns 'Nothing' if the parameter is not found or cannot be parsed at the right type.
--
-- 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/
pathParamMaybe :: (Trans.Parsable a) => Text -> ActionM (Maybe a)
pathParamMaybe = Trans.pathParamMaybe . toStrict

-- | Synonym for 'pathParamMaybe'
captureParamMaybe :: (Trans.Parsable a) => Text -> ActionM (Maybe a)
captureParamMaybe = Trans.captureParamMaybe . toStrict
captureParamMaybe = Trans.pathParamMaybe . toStrict

-- | Look up a form parameter. Returns 'Nothing' if the parameter is not found or cannot be parsed at the right type.
--
Expand All @@ -289,14 +297,17 @@



-- | Get all parameters from capture, form and query (in that order).
-- | Get all parameters from path, form and query (in that order).
params :: ActionM [Param]
params = Trans.params

Check warning on line 302 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 8.10.7

In the use of ‘params’

Check warning on line 302 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.2.8

In the use of ‘params’

Check warning on line 302 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.0.2

In the use of ‘params’

Check warning on line 302 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

In the use of ‘params’

Check warning on line 302 in Web/Scotty.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

In the use of ‘params’
{-# DEPRECATED params "(#204) Not a good idea to treat all parameters identically. Use captureParams, formParams and queryParams instead. "#-}
{-# DEPRECATED params "(#204) Not a good idea to treat all parameters identically. Use pathParams, formParams and queryParams instead. "#-}

-- | Get capture parameters
-- | Synonym for 'pathParams'
captureParams :: ActionM [Param]
captureParams = Trans.captureParams
-- | Get path parameters
pathParams :: ActionM [Param]
pathParams = Trans.pathParams
-- | Get form parameters
formParams :: ActionM [Param]
formParams = Trans.formParams
Expand Down
49 changes: 35 additions & 14 deletions Web/Scotty/Action.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
, jsonData
, next
, param
, pathParam
, captureParam
, formParam
, queryParam
, pathParamMaybe
, captureParamMaybe
, formParamMaybe
, queryParamMaybe
, params
, pathParams
, captureParams
, formParams
, queryParams
Expand Down Expand Up @@ -89,7 +92,7 @@
import Numeric.Natural

import Web.Scotty.Internal.Types
import Web.Scotty.Util (mkResponse, addIfNotPresent, add, replace, lazyTextToStrictByteString, decodeUtf8Lenient)

Check warning on line 95 in Web/Scotty/Action.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.4.6

The import of ‘decodeUtf8Lenient’

Check warning on line 95 in Web/Scotty/Action.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / ghc 9.6.2

The import of ‘decodeUtf8Lenient’
import UnliftIO.Exception (Handler(..), catch, catches)

import Network.Wai.Internal (ResponseReceived(..))
Expand Down Expand Up @@ -169,12 +172,12 @@
-- ever run is if the first one calls 'next'.
--
-- > get "/foo/:bar" $ do
-- > w :: Text <- captureParam "bar"
-- > w :: Text <- pathParam "bar"
-- > unless (w == "special") next
-- > text "You made a request to /foo/special"
-- >
-- > get "/foo/:baz" $ do
-- > w <- captureParam "baz"
-- > w <- pathParam "baz"
-- > text $ "You made a request to: " <> w
next :: Monad m => ActionT m a
next = E.throw AENext
Expand Down Expand Up @@ -286,15 +289,19 @@
Just v -> either (const next) return $ parseParam (TL.fromStrict v)
{-# DEPRECATED param "(#204) Not a good idea to treat all parameters identically. Use captureParam, formParam and queryParam instead. "#-}

-- | Look up a capture parameter.
-- | Synonym for 'pathParam'
captureParam :: (Parsable a, Monad m) => T.Text -> ActionT m a
captureParam = pathParam

-- | Look up a path parameter.
--
-- * Raises an exception which can be caught by 'catch' if parameter is not found. If the exception is not caught, scotty will return a HTTP error code 500 ("Internal Server Error") to the client.
--
-- * If the parameter is found, but 'parseParam' fails to parse to the correct type, 'next' is called.
--
-- /Since: 0.20/
captureParam :: (Parsable a, Monad m) => T.Text -> ActionT m a
captureParam = paramWith CaptureParam envCaptureParams status500
pathParam :: (Parsable a, Monad m) => T.Text -> ActionT m a
pathParam = paramWith PathParam envPathParams status500


-- | Look up a form parameter.
Expand All @@ -317,14 +324,23 @@
queryParam :: (Parsable a, Monad m) => T.Text -> ActionT m a
queryParam = paramWith QueryParam envQueryParams status400

-- | Look up a path parameter. Returns 'Nothing' if the parameter is not found or cannot be parsed at the right type.
--
-- 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/
pathParamMaybe :: (Parsable a, Monad m) => T.Text -> ActionT m (Maybe a)
pathParamMaybe = paramWithMaybe envPathParams

-- | Look up a capture parameter. Returns 'Nothing' if the parameter is not found or cannot be parsed at the right type.
--
-- 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/
captureParamMaybe :: (Parsable a, Monad m) => T.Text -> ActionT m (Maybe a)
captureParamMaybe = paramWithMaybe envCaptureParams
captureParamMaybe = paramWithMaybe envPathParams

-- | Look up a form parameter. Returns 'Nothing' if the parameter is not found or cannot be parsed at the right type.
--
Expand All @@ -342,12 +358,12 @@
queryParamMaybe :: (Parsable a, Monad m) => T.Text -> ActionT m (Maybe a)
queryParamMaybe = paramWithMaybe envQueryParams

data ParamType = CaptureParam
data ParamType = PathParam
| FormParam
| QueryParam
instance Show ParamType where
show = \case
CaptureParam -> "capture"
PathParam -> "path"
FormParam -> "form"
QueryParam -> "query"

Expand All @@ -363,7 +379,7 @@
Nothing -> raiseStatus err (T.unwords [T.pack (show ty), "parameter:", k, "not found!"])
Just v ->
let handleParseError = \case
CaptureParam -> next
PathParam -> next
_ -> raiseStatus err (T.unwords ["Cannot parse", v, "as a", T.pack (show ty), "parameter"])
in either (const $ handleParseError ty) return $ parseParam $ TL.fromStrict v

Expand All @@ -382,14 +398,19 @@
Nothing -> pure Nothing
Just v -> either (const $ pure Nothing) (pure . Just) $ parseParam $ TL.fromStrict v

-- | Get all parameters from capture, form and query (in that order).
-- | Get all parameters from path, form and query (in that order).
params :: Monad m => ActionT m [Param]
params = paramsWith getParams
{-# DEPRECATED params "(#204) Not a good idea to treat all parameters identically. Use captureParams, formParams and queryParams instead. "#-}
{-# 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

-- | Get capture parameters
-- | Get path parameters
captureParams :: Monad m => ActionT m [Param]
captureParams = paramsWith envCaptureParams
captureParams = paramsWith envPathParams

-- | Get form parameters
formParams :: Monad m => ActionT m [Param]
formParams = paramsWith envFormParams
Expand All @@ -402,7 +423,7 @@

{-# DEPRECATED getParams "(#204) Not a good idea to treat all parameters identically" #-}
getParams :: ActionEnv -> [Param]
getParams e = envCaptureParams e <> envFormParams e <> envQueryParams e
getParams e = envPathParams e <> envFormParams e <> envQueryParams e


-- === access the fields of the Response being constructed
Expand Down
2 changes: 1 addition & 1 deletion Web/Scotty/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ type Param = (Text, Text)
type File = (Text, FileInfo LBS8.ByteString)

data ActionEnv = Env { envReq :: Request
, envCaptureParams :: [Param]
, envPathParams :: [Param]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change (since the Types module is exposed) and should be marked as such.

, envFormParams :: [Param]
, envQueryParams :: [Param]
, envBody :: IO LBS8.ByteString
Expand Down
6 changes: 3 additions & 3 deletions Web/Scotty/Trans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ module Web.Scotty.Trans
-- ** Accessing the Request, Captures, and Query Parameters
, request, Lazy.header, Lazy.headers, body, bodyReader
, param, params
, captureParam, formParam, queryParam
, captureParamMaybe, formParamMaybe, queryParamMaybe
, captureParams, formParams, queryParams
, 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
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* deprecate `rescue` and `liftAndCatchIO`
* add `Web.Scotty.Trans.Strict` and `Web.Scotty.Trans.Lazy`
* Reverted the `MonadReader` instance of `ActionT` so that it inherits the base monad
* renamed `captureParam`, `captureParamMaybe`, and `captureParams` to `pathParam`, `pathParamMaybe`, `pathParams` respectively, keeping the old names as their synonyms

## 0.20.1 [2023.10.03]

Expand Down
6 changes: 3 additions & 3 deletions examples/basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ main = scotty 3000 $ do
-- Using a parameter in the query string. Since it has
-- not been given, a 500 page is generated.
get "/foo" $ do
v <- captureParam "fooparam"
v <- pathParam "fooparam"
html $ mconcat ["<h1>", v, "</h1>"]

-- An uncaught error becomes a 500 page.
Expand All @@ -64,7 +64,7 @@ main = scotty 3000 $ do
-- any string, and capture that value as a parameter.
-- URL captures take precedence over query string parameters.
get "/foo/:bar/required" $ do
v <- captureParam "bar"
v <- pathParam "bar"
html $ mconcat ["<h1>", v, "</h1>"]

-- Files are streamed directly to the client.
Expand All @@ -81,7 +81,7 @@ main = scotty 3000 $ do
json $ take 20 $ randomRs (1::Int,100) g

get "/ints/:is" $ do
is <- captureParam "is"
is <- pathParam "is"
json $ [(1::Int)..10] ++ is

get "/setbody" $ do
Expand Down
4 changes: 2 additions & 2 deletions examples/cookies.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ main = scotty 3000 $ do
H.input H.! type_ "submit" H.! value "set a cookie"

post "/set-a-cookie" $ do
name' <- captureParam "name"
value' <- captureParam "value"
name' <- pathParam "name"
value' <- pathParam "value"
setSimpleCookie name' value'
redirect "/"
2 changes: 1 addition & 1 deletion examples/exceptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ main = scottyT 3000 id $ do -- note, we aren't using any additional transformer
]

get "/switch/:val" $ do
v <- captureParam "val"
v <- pathParam "val"
_ <- if even v then throw Forbidden else throw (NotFound v)
text "this will never be reached"

Expand Down
2 changes: 1 addition & 1 deletion examples/urlshortener.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ main = do
H.input H.! type_ "submit"

post "/shorten" $ do
url <- captureParam "url"
url <- formParam "url"
liftIO $ modifyMVar_ m $ \(i,db) -> return (i+1, M.insert i (T.pack url) db)
redirect "/list"

Expand Down
2 changes: 1 addition & 1 deletion scotty.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Description:
.
main = scotty 3000 $
&#32;&#32;get &#34;/:word&#34; $ do
&#32;&#32;&#32;&#32;beam <- captureParam &#34;word&#34;
&#32;&#32;&#32;&#32;beam <- pathParam &#34;word&#34;
&#32;&#32;&#32;&#32;html $ mconcat [&#34;&#60;h1&#62;Scotty, &#34;, beam, &#34; me up!&#60;/h1&#62;&#34;]
@
.
Expand Down
Loading