From 39443ef90ffae559c3d546bf88005acf2e315d3a Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Fri, 3 Jan 2020 14:19:35 -0800 Subject: [PATCH] Add a jsonBody helper. --- src/Pages/Internal/StaticHttpBody.elm | 6 ++++++ src/Pages/StaticHttp.elm | 16 ++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Pages/Internal/StaticHttpBody.elm b/src/Pages/Internal/StaticHttpBody.elm index f9ab73ae..17326a3b 100644 --- a/src/Pages/Internal/StaticHttpBody.elm +++ b/src/Pages/Internal/StaticHttpBody.elm @@ -6,6 +6,7 @@ import Json.Encode as Encode type Body = EmptyBody | StringBody String + | JsonBody Encode.Value encode : Body -> Encode.Value @@ -19,6 +20,11 @@ encode body = [ ( "content", Encode.string content ) ] + JsonBody content -> + encodeWithType "json" + [ ( "content", content ) + ] + encodeWithType typeName otherFields = Encode.object <| diff --git a/src/Pages/StaticHttp.elm b/src/Pages/StaticHttp.elm index 8f7e2cb9..0a210c48 100644 --- a/src/Pages/StaticHttp.elm +++ b/src/Pages/StaticHttp.elm @@ -61,6 +61,7 @@ and describe your use case! import Dict exposing (Dict) import Dict.Extra import Json.Decode.Exploration as Decode exposing (Decoder) +import Json.Encode as Encode import Pages.Internal.StaticHttpBody as Body import Pages.Secrets import Pages.StaticHttp.Request as HashRequest @@ -82,6 +83,13 @@ stringBody content = Body.StringBody content +{-| Builds a JSON body for a StaticHttp request. See [elm/http's `Http.jsonBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#jsonBody). +-} +jsonBody : Encode.Value -> Body +jsonBody content = + Body.JsonBody content + + type alias Body = Body.Body @@ -449,14 +457,6 @@ type alias RequestDetails = } - ---"[" --- ++ requestDetails.method --- ++ "]" --- ++ requestDetails.url --- ++ String.join "," (requestDetails.headers |> List.map (\( key, value ) -> key ++ " : " ++ value)) - - requestToString : RequestDetails -> String requestToString requestDetails = requestDetails.url