Handle variabl length params for api route codec.

This commit is contained in:
Dillon Kearns 2021-04-30 16:01:18 -07:00
parent 357e49548b
commit d95ccebc60
2 changed files with 49 additions and 55 deletions

View File

@ -3,15 +3,9 @@ module ApiHandler exposing (..)
import Regex
withRoutes : (constructor -> List String) -> Handler a constructor -> List String
withRoutes : (constructor -> List String) -> Handler a constructor -> String
withRoutes buildUrls (Handler pattern handler toString constructor) =
buildUrls constructor
--dynamicSegments
-- |> List.map toString
--|> List.map (\value -> toString value)
toString (buildUrls (constructor []))
tryMatch : String -> Handler Response constructor -> Maybe Response
@ -19,7 +13,7 @@ tryMatch path (Handler pattern handler toString constructor) =
let
matches =
Regex.find
(Regex.fromString (pattern |> Debug.log "pattern")
(Regex.fromString pattern
|> Maybe.withDefault Regex.never
)
path
@ -41,7 +35,7 @@ tryMatch path (Handler pattern handler toString constructor) =
type Handler a constructor
= Handler String (List String -> a) (List String -> String) constructor
= Handler String (List String -> a) (List String -> String) (List String -> constructor)
type alias Response =
@ -67,9 +61,9 @@ type alias Response =
--succeedNew : a -> b -> Handler a b
succeedNew : a -> Handler a String
succeedNew : a -> Handler a (List String)
succeedNew a =
Handler "" (\args -> a) (\_ -> "") ""
Handler "" (\args -> a) (\_ -> "") (\list -> list)
@ -168,23 +162,27 @@ captureNew (Handler pattern previousHandler toString constructor) =
_ ->
Debug.todo "Expected non-empty list"
)
--(Debug.todo "")
(\s ->
case s |> Debug.log "@@@ s" of
case s of
first :: rest ->
toString s ++ first
toString rest ++ first
_ ->
""
)
--(\_ -> dynamicSegments [])
--(Debug.todo "")
(\string ->
constructor
(\matches ->
\string ->
constructor (string :: matches)
)
--(\_ -> constructor)
--(Debug.todo "")
--)
--foo : a -> List a -> List a
--foo =
-- (::)
--(dynamicSegments (\string -> [ string ]))
--(Debug.todo "")

View File

@ -61,58 +61,54 @@ all =
-- [ "users/100.json"
-- , "users/101.json"
-- ],
only <|
describe "multi-part"
[ --test "multi-level routes" <|
-- \() ->
-- newThing
-- |> withRoutes
-- |> Expect.equal
-- [ "repos/dillonkearns/elm-pages.json"
--
-- --, "users/101.json"
-- ],
test "3-level route" <|
\() ->
threeParts
|> withRoutes
(\constructor ->
[ constructor "dillonkearns" "elm-pages" "static-files"
]
)
|> Expect.equal
[ "repos/dillonkearns/elm-pages.json"
describe "multi-part"
[ test "multi-level routes" <|
\() ->
newThing
|> withRoutes
(\a ->
--constructor "dillonkearns" "elm-pages"
--, constructor "101"
a "dillonkearns" "elm-pages"
--, constructor "elm-pages"
--]
)
|> Expect.equal
--[
"repos/dillonkearns/elm-pages.json"
--, "users/101.json"
]
]
--, "users/101.json"
--]
, test "3-level route" <|
\() ->
threeParts
|> withRoutes
(\constructor ->
constructor "dillonkearns" "elm-pages" "static-files"
)
|> Expect.equal
"repos/dillonkearns/elm-pages/static-files"
--, "users/101.json"
]
]
newThing : Handler { body : String } (String -> String -> String)
newThing : Handler Response (String -> String -> List String)
newThing =
succeedNew
(\userName repoName ->
{ body = "Data for user" }
)
--(Debug.todo "")
--(\a ->
-- [ --constructor "dillonkearns" "elm-pages"
-- --, constructor "101"
-- a "dillonkearns" "elm-pages" -- [ "1", "2" ]
--
-- --, constructor "elm-pages"
-- ]
--)
--(Debug.todo "")
|> literalSegment "repos"
|> slash
|> captureNew
|> slash
|> captureNew
|> literalSegment ".json"
threeParts : Handler { body : String } (String -> String -> String -> String)
threeParts : Handler Response (String -> String -> String -> List String)
threeParts =
succeedNew
(\username repo branch ->