choonkeat / elm-ext-http / Ext.Http.Cookie


type Attribute
    = SameSite String
    | Path String
    | Domain String
    | MaxAge Basics.Int
    | Expires String
    | Secure
    | HttpOnly

See https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies

But in general, don't use Expires anymore,

[ SameSite "Lax"
, Path "/"
, Domain "example.com"
, MaxAge 86400
, Secure
, HttpOnly
]


type alias Input =
{ name : String
, value : String
, attributes : List Attribute 
}

See https://datatracker.ietf.org/doc/html/rfc6265 for valid characters in cookie names and values

get : String -> String -> Maybe String

Obtains the cookie value for a given cookie name from a Cookie http request header

NOTE: the Cookie http request header that server sees does not come with the attributes you see in the Set-Cookie response header.

requestString : String
requestString =
    "somekey=somevalue; sess=somejwt"

get "sess" requestString
--> Just "somejwt"

get "somekey" requestString
--> Just "somevalue"

responseString : Input -> String

Return a String value that can be used as the value of a Set-Cookie response header

Http.header "Set-Cookie" (responseString Input)

The generated String will be in the format:

responseString
    { name = "somekey"
    , value = "somevalue"
    , attributes =
        [ SameSite "Lax"
        , Path "/"
        , Domain "example.com"
        , MaxAge 86400
        , Expires "Wed, 21 Oct 2015 07:28:00 GMT"
        , Secure
        , HttpOnly
        ]
    }
--> "somekey=somevalue; SameSite=Lax; Path=/; Domain=example.com; Max-Age=86400; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOnly"