diff --git a/elm.json b/elm.json index 2db81e8c..b4c8254c 100644 --- a/elm.json +++ b/elm.json @@ -32,6 +32,7 @@ "elm-community/result-extra": "2.2.1 <= v < 3.0.0", "lukewestby/elm-string-interpolate": "1.0.4 <= v < 2.0.0", "mdgriffith/elm-markup": "3.0.1 <= v < 4.0.0", + "mgold/elm-nonempty-list": "4.0.2 <= v < 5.0.0", "miniBill/elm-codec": "1.2.0 <= v < 2.0.0", "noahzgordon/elm-color-extra": "1.0.2 <= v < 2.0.0", "tripokey/elm-fuzzy": "5.2.1 <= v < 6.0.0" diff --git a/src/StaticHttp.elm b/src/StaticHttp.elm index f00425c4..f7bcc8c0 100644 --- a/src/StaticHttp.elm +++ b/src/StaticHttp.elm @@ -1,6 +1,6 @@ module StaticHttp exposing ( Request - , jsonRequest, jsonRequestWithSecrets + , jsonRequest, jsonRequestWithSecrets, reducedJsonRequest , map, succeed , andThen , map2, map3, map4, map5, map6, map7, map8, map9 @@ -9,7 +9,7 @@ module StaticHttp exposing {-| TODO @docs Request -@docs jsonRequest, jsonRequestWithSecrets +@docs jsonRequest, jsonRequestWithSecrets, reducedJsonRequest @docs map, succeed @docs andThen @@ -23,6 +23,7 @@ import Dict exposing (Dict) import Head import Html exposing (Html) import Json.Decode as Decode exposing (Decoder) +import Json.Decode.Exploration import Pages.Internal.Secrets import Pages.StaticHttpRequest exposing (Request(..)) import Secrets exposing (Secrets) @@ -217,7 +218,10 @@ jsonRequest url decoder = case maybeResponse of Just rawResponse -> -- @@@@@ TODO reduce raw responses - Ok ( rawResponseDict, rawResponse ) + Ok + ( rawResponseDict + , rawResponse + ) Nothing -> Err <| Pages.StaticHttpRequest.MissingHttpResponse url @@ -234,6 +238,60 @@ jsonRequest url decoder = ) +{-| TODO +-} +reducedJsonRequest : String -> Json.Decode.Exploration.Decoder a -> Request a +reducedJsonRequest url decoder = + Request + ( [ Pages.Internal.Secrets.urlWithoutSecrets url ] + , \rawResponseDict -> + rawResponseDict + |> Dict.get url + |> (\maybeResponse -> + case maybeResponse of + Just rawResponse -> + -- @@@@@ TODO reduce raw responses + Ok + ( rawResponseDict + -- |> Dict.update url (\maybeValue -> Just """{"fake": 123}""") + , rawResponse + ) + + Nothing -> + Err <| Pages.StaticHttpRequest.MissingHttpResponse url + ) + |> Result.andThen + (\( strippedResponses, rawResponse ) -> + let + reduced = + Json.Decode.Exploration.stripString decoder rawResponse + |> Result.withDefault "TODO" + in + rawResponse + |> Json.Decode.Exploration.decodeString decoder + -- |> Result.mapError Json.Decode.Exploration.errorsToString + |> (\decodeResult -> + case decodeResult |> Debug.log "decodeResult" of + Json.Decode.Exploration.BadJson -> + Pages.StaticHttpRequest.DecoderError "" |> Err + + Json.Decode.Exploration.Errors errors -> + Pages.StaticHttpRequest.DecoderError "" |> Err + + Json.Decode.Exploration.WithWarnings warnings a -> + -- Pages.StaticHttpRequest.DecoderError "" |> Err + Ok a + + Json.Decode.Exploration.Success a -> + Ok a + ) + -- |> Result.mapError Pages.StaticHttpRequest.DecoderError + |> Result.map Done + |> Result.map (\finalRequest -> ( strippedResponses |> Dict.insert url reduced, finalRequest )) + ) + ) + + {-| TODO -} jsonRequestWithSecrets : (Secrets -> Result BuildError String) -> Decoder a -> Request a diff --git a/tests/StaticHttpRequestsTests.elm b/tests/StaticHttpRequestsTests.elm index 432ff803..14fec2a2 100644 --- a/tests/StaticHttpRequestsTests.elm +++ b/tests/StaticHttpRequestsTests.elm @@ -5,6 +5,7 @@ import Dict exposing (Dict) import Expect import Html import Json.Decode as Decode +import Json.Decode.Exploration as Reduce import Pages.ContentCache as ContentCache import Pages.Document as Document import Pages.ImagePath as ImagePath @@ -142,6 +143,37 @@ all = } ] ) + , only <| + test "reduced JSON is sent out" <| + \() -> + start + [ ( [] + , StaticHttp.reducedJsonRequest "https://api.github.com/repos/dillonkearns/elm-pages" (Reduce.field "stargazer_count" Reduce.int) + ) + ] + |> ProgramTest.simulateHttpOk + "GET" + "https://api.github.com/repos/dillonkearns/elm-pages" + """{ "stargazer_count": 86, "unused_field": 123 }""" + |> ProgramTest.expectOutgoingPortValues + "toJsPort" + (Codec.decoder Main.toJsCodec) + (Expect.equal + [ Main.Success + { pages = + Dict.fromList + [ ( "/" + , Dict.fromList + [ ( "https://api.github.com/repos/dillonkearns/elm-pages" + , """{"stargazer_count":86}""" + ) + ] + ) + ] + , manifest = manifest + } + ] + ) , test "the port sends out even if there are no http requests" <| \() -> start