mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-27 21:29:55 +03:00
Add BackendTask.Http.bytesBody.
This commit is contained in:
parent
ef0f091790
commit
8f34381954
@ -251,6 +251,9 @@ function toBody(body) {
|
||||
case "StringBody": {
|
||||
return body.args[1];
|
||||
}
|
||||
case "BytesBody": {
|
||||
return Buffer.from(body.args[1], "base64");
|
||||
}
|
||||
case "JsonBody": {
|
||||
return JSON.stringify(body.args[0]);
|
||||
}
|
||||
@ -269,13 +272,16 @@ function toContentType(body) {
|
||||
case "StringBody": {
|
||||
return { "Content-Type": body.args[0] };
|
||||
}
|
||||
case "BytesBody": {
|
||||
return { "Content-Type": body.args[0] };
|
||||
}
|
||||
case "JsonBody": {
|
||||
return { "Content-Type": "application/json" };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @typedef { { tag: 'EmptyBody'} | { tag: 'StringBody'; args: [string, string] } | {tag: 'JsonBody'; args: [ Object ] } } Body */
|
||||
/** @typedef { { tag: 'EmptyBody'} |{ tag: 'BytesBody'; args: [string, string] } | { tag: 'StringBody'; args: [string, string] } | {tag: 'JsonBody'; args: [ Object ] } } Body */
|
||||
function requireUncached(mode, filePath) {
|
||||
if (mode === "dev-server") {
|
||||
// for the build command, we can skip clearing the cache because it won't change while the build is running
|
||||
|
@ -5,7 +5,7 @@ module BackendTask.Http exposing
|
||||
, Error(..)
|
||||
, RequestDetails, request
|
||||
, withMetadata, Metadata
|
||||
, Body, emptyBody, stringBody, jsonBody
|
||||
, Body, emptyBody, stringBody, jsonBody, bytesBody
|
||||
, CacheStrategy(..), Options
|
||||
)
|
||||
|
||||
@ -70,7 +70,7 @@ The way you build a body is analogous to the `elm/http` package. Currently, only
|
||||
`stringBody` are supported. If you have a use case that calls for a different body type, please open a Github issue
|
||||
and describe your use case!
|
||||
|
||||
@docs Body, emptyBody, stringBody, jsonBody
|
||||
@docs Body, emptyBody, stringBody, jsonBody, bytesBody
|
||||
|
||||
|
||||
## Caching Options
|
||||
@ -81,6 +81,7 @@ and describe your use case!
|
||||
|
||||
import BackendTask exposing (BackendTask)
|
||||
import Base64
|
||||
import Bytes exposing (Bytes)
|
||||
import Bytes.Decode
|
||||
import Dict exposing (Dict)
|
||||
import Exception exposing (Catchable)
|
||||
@ -100,6 +101,13 @@ emptyBody =
|
||||
Body.EmptyBody
|
||||
|
||||
|
||||
{-| Build a body from `Bytes` for a BackendTask.Http request. See [elm/http's `Http.bytesBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#bytesBody).
|
||||
-}
|
||||
bytesBody : String -> Bytes -> Body
|
||||
bytesBody =
|
||||
Body.BytesBody
|
||||
|
||||
|
||||
{-| Builds a string body for a BackendTask.Http request. See [elm/http's `Http.stringBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#stringBody).
|
||||
|
||||
Note from the `elm/http` docs:
|
||||
|
@ -1,6 +1,9 @@
|
||||
module Pages.Internal.StaticHttpBody exposing (Body(..), codec, encode)
|
||||
|
||||
import Base64
|
||||
import Bytes exposing (Bytes)
|
||||
import Codec exposing (Codec)
|
||||
import Json.Decode
|
||||
import Json.Encode as Encode
|
||||
|
||||
|
||||
@ -8,6 +11,7 @@ type Body
|
||||
= EmptyBody
|
||||
| StringBody String String
|
||||
| JsonBody Encode.Value
|
||||
| BytesBody String Bytes
|
||||
|
||||
|
||||
encode : Body -> Encode.Value
|
||||
@ -26,6 +30,15 @@ encode body =
|
||||
[ ( "content", content )
|
||||
]
|
||||
|
||||
BytesBody _ content ->
|
||||
encodeWithType "bytes"
|
||||
[ ( "content"
|
||||
, Base64.fromBytes content
|
||||
|> Maybe.withDefault ""
|
||||
|> Encode.string
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
encodeWithType : String -> List ( String, Encode.Value ) -> Encode.Value
|
||||
encodeWithType typeName otherFields =
|
||||
@ -37,7 +50,7 @@ encodeWithType typeName otherFields =
|
||||
codec : Codec Body
|
||||
codec =
|
||||
Codec.custom
|
||||
(\vEmpty vString vJson value ->
|
||||
(\vEmpty vString vJson vBytes value ->
|
||||
case value of
|
||||
EmptyBody ->
|
||||
vEmpty
|
||||
@ -47,8 +60,29 @@ codec =
|
||||
|
||||
JsonBody body ->
|
||||
vJson body
|
||||
|
||||
BytesBody contentType body ->
|
||||
vBytes contentType body
|
||||
)
|
||||
|> Codec.variant0 "EmptyBody" EmptyBody
|
||||
|> Codec.variant2 "StringBody" StringBody Codec.string Codec.string
|
||||
|> Codec.variant1 "JsonBody" JsonBody Codec.value
|
||||
|> Codec.variant2 "BytesBody" BytesBody Codec.string bytesCodec
|
||||
|> Codec.buildCustom
|
||||
|
||||
|
||||
bytesCodec : Codec Bytes
|
||||
bytesCodec =
|
||||
Codec.build (Base64.fromBytes >> Maybe.withDefault "" >> Encode.string)
|
||||
(Json.Decode.string
|
||||
|> Json.Decode.map Base64.toBytes
|
||||
|> Json.Decode.andThen
|
||||
(\decodedBytes ->
|
||||
case decodedBytes of
|
||||
Just bytes ->
|
||||
Json.Decode.succeed bytes
|
||||
|
||||
Nothing ->
|
||||
Json.Decode.fail "Couldn't parse bytes."
|
||||
)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user