mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-28 22:37:08 +03:00
New experiment for Api routes prototype.
This commit is contained in:
parent
f6963d3087
commit
357e49548b
@ -3,18 +3,19 @@ module ApiHandler exposing (..)
|
||||
import Regex
|
||||
|
||||
|
||||
withRoutes : Handler Response (List (List String)) -> List String
|
||||
withRoutes (Handler pattern handler toString dynamicSegments) =
|
||||
dynamicSegments
|
||||
|> List.map toString
|
||||
withRoutes : (constructor -> List String) -> Handler a constructor -> List String
|
||||
withRoutes buildUrls (Handler pattern handler toString constructor) =
|
||||
buildUrls constructor
|
||||
|
||||
|
||||
|
||||
--dynamicSegments
|
||||
-- |> List.map toString
|
||||
--|> List.map (\value -> toString value)
|
||||
|
||||
|
||||
tryMatch : String -> Handler Response b -> Maybe Response
|
||||
tryMatch path (Handler pattern handler toString dynamicSegments) =
|
||||
tryMatch : String -> Handler Response constructor -> Maybe Response
|
||||
tryMatch path (Handler pattern handler toString constructor) =
|
||||
let
|
||||
matches =
|
||||
Regex.find
|
||||
@ -39,17 +40,36 @@ tryMatch path (Handler pattern handler toString dynamicSegments) =
|
||||
-- |> captureSegment
|
||||
|
||||
|
||||
type Handler a b
|
||||
= Handler String (List String -> a) (List String -> String) b
|
||||
type Handler a constructor
|
||||
= Handler String (List String -> a) (List String -> String) constructor
|
||||
|
||||
|
||||
type alias Response =
|
||||
{ body : String }
|
||||
|
||||
|
||||
succeed : a -> ((b -> List String) -> List (List String)) -> Handler a ((b -> List String) -> List (List String))
|
||||
succeed a buildTimePaths =
|
||||
Handler "" (\args -> a) (\_ -> "") buildTimePaths
|
||||
|
||||
--succeed : a -> ((b -> List String) -> List (List String)) -> Handler a ((b -> List String) -> List (List String))
|
||||
--succeed a buildTimePaths =
|
||||
-- Handler "" (\args -> a) (\_ -> "") buildTimePaths
|
||||
--succeedNew :
|
||||
-- a
|
||||
-- ->
|
||||
-- ((b -> List String)
|
||||
-- -> List (List String)
|
||||
-- )
|
||||
-- ->
|
||||
-- Handler
|
||||
-- a
|
||||
-- ((b -> List String)
|
||||
-- -> List (List String)
|
||||
-- )
|
||||
--succeedNew : a -> b -> Handler a b
|
||||
|
||||
|
||||
succeedNew : a -> Handler a String
|
||||
succeedNew a =
|
||||
Handler "" (\args -> a) (\_ -> "") ""
|
||||
|
||||
|
||||
|
||||
@ -58,19 +78,88 @@ succeed a buildTimePaths =
|
||||
-- Debug.todo ""
|
||||
|
||||
|
||||
literalSegment : String -> Handler a b -> Handler a b
|
||||
literalSegment segment (Handler pattern handler toString dynamicSegments) =
|
||||
Handler (pattern ++ segment) handler (\values -> toString values ++ segment) dynamicSegments
|
||||
literalSegment : String -> Handler a constructor -> Handler a constructor
|
||||
literalSegment segment (Handler pattern handler toString constructor) =
|
||||
Handler (pattern ++ segment) handler (\values -> toString values ++ segment) constructor
|
||||
|
||||
|
||||
slash : Handler a b -> Handler a b
|
||||
slash (Handler pattern handler toString dynamicSegments) =
|
||||
Handler (pattern ++ "/") handler (\arg -> toString arg ++ "/") dynamicSegments
|
||||
slash : Handler a constructor -> Handler a constructor
|
||||
slash (Handler pattern handler toString constructor) =
|
||||
Handler (pattern ++ "/") handler (\arg -> toString arg ++ "/") constructor
|
||||
|
||||
|
||||
captureSegment : Handler (String -> a) ((String -> List String) -> List (List String)) -> Handler a (List (List String))
|
||||
captureSegment (Handler pattern previousHandler toString dynamicSegments) =
|
||||
Handler (pattern ++ "(.*)")
|
||||
|
||||
--captureSegment :
|
||||
-- Handler
|
||||
-- (String -> a)
|
||||
-- ((String -> List String)
|
||||
-- -> b
|
||||
-- )
|
||||
-- constructor
|
||||
-- ->
|
||||
-- Handler
|
||||
-- a
|
||||
-- b
|
||||
-- constructor
|
||||
--captureSegment (Handler pattern previousHandler toString dynamicSegments) =
|
||||
-- Handler (pattern ++ "(.*)")
|
||||
-- (\matches ->
|
||||
-- case matches of
|
||||
-- first :: rest ->
|
||||
-- previousHandler rest first
|
||||
--
|
||||
-- _ ->
|
||||
-- Debug.todo "Expected non-empty list"
|
||||
-- )
|
||||
-- (\s ->
|
||||
-- case s |> Debug.log "@@@ s" of
|
||||
-- first :: rest ->
|
||||
-- toString rest ++ first
|
||||
--
|
||||
-- _ ->
|
||||
-- ""
|
||||
-- )
|
||||
-- --(dynamicSegments (\string -> [ string ]))
|
||||
-- --(dynamicSegments (\_ -> []))
|
||||
-- (dynamicSegments
|
||||
-- (\string ->
|
||||
-- [ string ]
|
||||
-- )
|
||||
-- )
|
||||
--(Debug.todo "")
|
||||
--captureNew :
|
||||
-- Handler
|
||||
-- (String -> a)
|
||||
-- (String
|
||||
-- -> b
|
||||
-- )
|
||||
-- ->
|
||||
-- Handler
|
||||
-- a
|
||||
-- b
|
||||
|
||||
|
||||
captureNew :
|
||||
Handler
|
||||
(String -> a)
|
||||
constructor
|
||||
->
|
||||
Handler
|
||||
a
|
||||
(String -> constructor)
|
||||
captureNew (Handler pattern previousHandler toString constructor) =
|
||||
Handler
|
||||
(pattern ++ "(.*)")
|
||||
--(Debug.todo "")
|
||||
--(\matches ->
|
||||
-- case matches of
|
||||
-- first :: rest ->
|
||||
-- previousHandler rest
|
||||
--
|
||||
-- -- first
|
||||
-- _ ->
|
||||
-- Debug.todo "Expected non-empty list"
|
||||
--)
|
||||
(\matches ->
|
||||
case matches of
|
||||
first :: rest ->
|
||||
@ -79,15 +168,25 @@ captureSegment (Handler pattern previousHandler toString dynamicSegments) =
|
||||
_ ->
|
||||
Debug.todo "Expected non-empty list"
|
||||
)
|
||||
--(Debug.todo "")
|
||||
(\s ->
|
||||
case s of
|
||||
case s |> Debug.log "@@@ s" of
|
||||
first :: rest ->
|
||||
toString s ++ first
|
||||
|
||||
_ ->
|
||||
""
|
||||
)
|
||||
(dynamicSegments (\string -> [ string ]))
|
||||
--(\_ -> dynamicSegments [])
|
||||
--(Debug.todo "")
|
||||
(\string ->
|
||||
constructor
|
||||
)
|
||||
|
||||
|
||||
|
||||
--(dynamicSegments (\string -> [ string ]))
|
||||
--(Debug.todo "")
|
||||
|
||||
|
||||
captureRest : Handler (List String -> a) b -> Handler a b
|
||||
|
@ -7,58 +7,121 @@ import Test exposing (describe, only, test)
|
||||
|
||||
all =
|
||||
describe "api routes"
|
||||
[ test "match top-level file with no extension" <|
|
||||
\() ->
|
||||
succeed
|
||||
(\userId ->
|
||||
{ body = "Data for user " ++ userId }
|
||||
)
|
||||
(\_ -> [])
|
||||
|> captureSegment
|
||||
|> tryMatch "123"
|
||||
|> Expect.equal (Just { body = "Data for user 123" })
|
||||
, test "file with extension" <|
|
||||
\() ->
|
||||
succeed
|
||||
(\userId ->
|
||||
{ body = "Data for user " ++ userId }
|
||||
)
|
||||
(\_ -> [])
|
||||
|> captureSegment
|
||||
|> literalSegment ".json"
|
||||
|> tryMatch "124.json"
|
||||
|> Expect.equal (Just { body = "Data for user 124" })
|
||||
, test "file path with multiple segments" <|
|
||||
\() ->
|
||||
succeed
|
||||
(\userId ->
|
||||
{ body = "Data for user " ++ userId }
|
||||
)
|
||||
(\_ -> [])
|
||||
|> literalSegment "users"
|
||||
|> slash
|
||||
|> captureSegment
|
||||
|> literalSegment ".json"
|
||||
|> tryMatch "users/123.json"
|
||||
|> Expect.equal (Just { body = "Data for user 123" })
|
||||
, test "routes" <|
|
||||
\() ->
|
||||
succeed
|
||||
(\userId ->
|
||||
{ body = "Data for user " ++ userId }
|
||||
)
|
||||
(\constructor ->
|
||||
[ constructor "100"
|
||||
, constructor "101"
|
||||
]
|
||||
)
|
||||
|> literalSegment "users"
|
||||
|> slash
|
||||
|> captureSegment
|
||||
|> literalSegment ".json"
|
||||
|> withRoutes
|
||||
|> Expect.equal
|
||||
[ "users/100.json"
|
||||
, "users/101.json"
|
||||
]
|
||||
[ -- test "match top-level file with no extension" <|
|
||||
-- \() ->
|
||||
-- succeed
|
||||
-- (\userId ->
|
||||
-- { body = "Data for user " ++ userId }
|
||||
-- )
|
||||
-- (\_ -> [])
|
||||
-- |> captureSegment
|
||||
-- |> tryMatch "123"
|
||||
-- |> Expect.equal (Just { body = "Data for user 123" })
|
||||
--, test "file with extension" <|
|
||||
-- \() ->
|
||||
-- succeed
|
||||
-- (\userId ->
|
||||
-- { body = "Data for user " ++ userId }
|
||||
-- )
|
||||
-- (\_ -> [])
|
||||
-- |> captureSegment
|
||||
-- |> literalSegment ".json"
|
||||
-- |> tryMatch "124.json"
|
||||
-- |> Expect.equal (Just { body = "Data for user 124" })
|
||||
--, test "file path with multiple segments" <|
|
||||
-- \() ->
|
||||
-- succeed
|
||||
-- (\userId ->
|
||||
-- { body = "Data for user " ++ userId }
|
||||
-- )
|
||||
-- (\_ -> [])
|
||||
-- |> literalSegment "users"
|
||||
-- |> slash
|
||||
-- |> captureSegment
|
||||
-- |> literalSegment ".json"
|
||||
-- |> tryMatch "users/123.json"
|
||||
-- |> Expect.equal (Just { body = "Data for user 123" })
|
||||
--, test "routes" <|
|
||||
-- \() ->
|
||||
-- succeed
|
||||
-- (\userId ->
|
||||
-- { body = "Data for user " ++ userId }
|
||||
-- )
|
||||
-- (\constructor ->
|
||||
-- [ constructor "100"
|
||||
-- , constructor "101"
|
||||
-- ]
|
||||
-- )
|
||||
-- |> literalSegment "users"
|
||||
-- |> slash
|
||||
-- |> captureSegment
|
||||
-- |> literalSegment ".json"
|
||||
-- |> withRoutes
|
||||
-- |> Expect.equal
|
||||
-- [ "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"
|
||||
|
||||
--, "users/101.json"
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
newThing : Handler { body : String } (String -> String -> 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
|
||||
|
||||
|
||||
threeParts : Handler { body : String } (String -> String -> String -> String)
|
||||
threeParts =
|
||||
succeedNew
|
||||
(\username repo branch ->
|
||||
{ body = [ username, repo, branch ] |> String.join " - " }
|
||||
)
|
||||
|> literalSegment "repos"
|
||||
|> slash
|
||||
|> captureNew
|
||||
|> slash
|
||||
|> captureNew
|
||||
|> slash
|
||||
|> captureNew
|
||||
|
Loading…
Reference in New Issue
Block a user