Send multiValueHeaders in netlify adapter.

This commit is contained in:
Dillon Kearns 2021-12-21 17:33:41 -08:00
parent 9a8bf732a0
commit 42126f0d4a
2 changed files with 20 additions and 3 deletions

View File

@ -154,7 +154,7 @@ async function render(event, context) {
const serverResponse = renderResult.body;
return {
body: serverResponse.body,
headers: serverResponse.headers,
multiValueHeaders: serverResponse.headers,
statusCode: serverResponse.statusCode,
isBase64Encoded: serverResponse.isBase64Encoded,
};

View File

@ -1,6 +1,7 @@
module ServerResponse exposing (ServerResponse, json, permanentRedirect, stringBody, success, temporaryRedirect, toJson, withStatusCode)
module ServerResponse exposing (ServerResponse, json, permanentRedirect, stringBody, success, temporaryRedirect, toJson, withHeader, withStatusCode)
import Json.Encode
import List.Extra
type alias ServerResponse =
@ -70,6 +71,11 @@ withStatusCode statusCode serverResponse =
{ serverResponse | statusCode = statusCode }
withHeader : String -> String -> ServerResponse -> ServerResponse
withHeader name value serverResponse =
{ serverResponse | headers = ( name, value ) :: serverResponse.headers }
toJson : ServerResponse -> Json.Encode.Value
toJson serverResponse =
Json.Encode.object
@ -77,9 +83,20 @@ toJson serverResponse =
, ( "statusCode", serverResponse.statusCode |> Json.Encode.int )
, ( "headers"
, serverResponse.headers
|> List.map (Tuple.mapSecond Json.Encode.string)
|> collectMultiValueHeaders
|> List.map (Tuple.mapSecond (Json.Encode.list Json.Encode.string))
|> Json.Encode.object
)
, ( "kind", Json.Encode.string "server-response" )
, ( "isBase64Encoded", Json.Encode.bool serverResponse.isBase64Encoded )
]
collectMultiValueHeaders : List ( String, String ) -> List ( String, List String )
collectMultiValueHeaders headers =
headers
|> List.Extra.groupWhile (\( key1, _ ) ( key2, _ ) -> key1 == key2)
|> List.map
(\( ( key, _ ), values ) ->
( key, values |> List.map Tuple.second )
)