From f6963d308733951eaefc790a6f27a0921b8d385e Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Fri, 30 Apr 2021 08:42:07 -0700 Subject: [PATCH] Move API handler into a module. --- src/ApiHandler.elm | 95 +++++++++++++++++++++++++++++++++++++++ tests/ApiHandlerTests.elm | 94 +------------------------------------- 2 files changed, 96 insertions(+), 93 deletions(-) create mode 100644 src/ApiHandler.elm diff --git a/src/ApiHandler.elm b/src/ApiHandler.elm new file mode 100644 index 00000000..93282ce2 --- /dev/null +++ b/src/ApiHandler.elm @@ -0,0 +1,95 @@ +module ApiHandler exposing (..) + +import Regex + + +withRoutes : Handler Response (List (List String)) -> List String +withRoutes (Handler pattern handler toString dynamicSegments) = + dynamicSegments + |> List.map toString + + + +--|> List.map (\value -> toString value) + + +tryMatch : String -> Handler Response b -> Maybe Response +tryMatch path (Handler pattern handler toString dynamicSegments) = + let + matches = + Regex.find + (Regex.fromString (pattern |> Debug.log "pattern") + |> Maybe.withDefault Regex.never + ) + path + |> List.concatMap .submatches + |> List.filterMap identity + in + handler matches + |> Just + + + +--exampleHandler : Handler Response +--exampleHandler = +-- succeed +-- (\userId -> +-- { body = "Data for user " ++ userId } +-- ) +-- |> captureSegment + + +type Handler a b + = Handler String (List String -> a) (List String -> String) b + + +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 + + + +--handle : (a -> Response) -> Handler a b -> Handler response b +--handle function handler = +-- 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 + + +slash : Handler a b -> Handler a b +slash (Handler pattern handler toString dynamicSegments) = + Handler (pattern ++ "/") handler (\arg -> toString arg ++ "/") dynamicSegments + + +captureSegment : Handler (String -> a) ((String -> List String) -> List (List String)) -> Handler a (List (List String)) +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 of + first :: rest -> + toString s ++ first + + _ -> + "" + ) + (dynamicSegments (\string -> [ string ])) + + +captureRest : Handler (List String -> a) b -> Handler a b +captureRest previousHandler = + Debug.todo "" diff --git a/tests/ApiHandlerTests.elm b/tests/ApiHandlerTests.elm index fd99e53a..34711ea9 100644 --- a/tests/ApiHandlerTests.elm +++ b/tests/ApiHandlerTests.elm @@ -1,7 +1,7 @@ module ApiHandlerTests exposing (..) +import ApiHandler exposing (..) import Expect -import Regex import Test exposing (describe, only, test) @@ -62,95 +62,3 @@ all = , "users/101.json" ] ] - - -withRoutes : Handler Response (List (List String)) -> List String -withRoutes (Handler pattern handler toString dynamicSegments) = - dynamicSegments - |> List.map toString - - - ---|> List.map (\value -> toString value) - - -tryMatch : String -> Handler Response b -> Maybe Response -tryMatch path (Handler pattern handler toString dynamicSegments) = - let - matches = - Regex.find - (Regex.fromString (pattern |> Debug.log "pattern") - |> Maybe.withDefault Regex.never - ) - path - |> List.concatMap .submatches - |> List.filterMap identity - in - handler matches - |> Just - - - ---exampleHandler : Handler Response ---exampleHandler = --- succeed --- (\userId -> --- { body = "Data for user " ++ userId } --- ) --- |> captureSegment - - -type Handler a b - = Handler String (List String -> a) (List String -> String) b - - -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 - - - ---handle : (a -> Response) -> Handler a b -> Handler response b ---handle function handler = --- 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 - - -slash : Handler a b -> Handler a b -slash (Handler pattern handler toString dynamicSegments) = - Handler (pattern ++ "/") handler (\arg -> toString arg ++ "/") dynamicSegments - - -captureSegment : Handler (String -> a) ((String -> List String) -> List (List String)) -> Handler a (List (List String)) -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 of - first :: rest -> - toString s ++ first - - _ -> - "" - ) - (dynamicSegments (\string -> [ string ])) - - -captureRest : Handler (List String -> a) b -> Handler a b -captureRest previousHandler = - Debug.todo ""