fast-twitch.middlewares.cookies
Complete generated reference for fast-twitch.middlewares.cookies
Usage and execution boundary
Adds keyword-keyed cookies containing value maps to requests. Response cookie instructions are serialized into separate Set-Cookie entries and the cookies field is removed. Names/values are encoded; incoming values are decoded.
(ns application.middleware
(:require [fast-twitch.middlewares.cookies :as middleware]))
(defn handler [_]
{:status 200 :headers {"content-type" "text/plain"} :body "Hello"})
(def app (middleware/wrap-cookies handler))
This defines the Ring handler portion. Adapt app through routing/ft-handler or pass it to server/listen!; startup instructions and a complete walkthrough show the surrounding project.
Settings and defaults
| Setting | Meaning |
|---|---|
:value |
Cookie value; a scalar instruction is also accepted |
:path / :domain |
Cookie attributes |
:max-age / :expires |
Age in seconds / native supported expiry representation |
:secure / :http-only |
Boolean attributes |
:same-site |
Strict/lax/none keyword or supplied string |
Wrapper order and all middleware families. The function reference below preserves the upstream signatures and source links; helper functions do not all share the wrapper's arity.
Set and read a preference
(defn preference-handler [request]
{:status 200 :headers {"content-type" "text/plain"}
:body (str "Previous preference: " (get-in request [:cookies :theme :value]))
:cookies {:theme {:value "dark" :path "/" :http-only true :same-site :lax}}})
(def preferences (middleware/wrap-cookies preference-handler))
The first response sets the preference; the next request carrying that cookie reports it. Cookie expires accepts a date object, milliseconds timestamp or supplied expiry string; max-age is seconds. A session cookie and a directly stored preference cookie serve different purposes. Session semantics.
Upstream function reference
Parses incoming cookies and serializes outgoing cookie instructions.
cookies-request
(cookies-request request)
(cookies-request request _options)
Function.
Associates parsed cookies on the request map. Source
cookies-response
(cookies-response response)
(cookies-response response _options)
Function.
Serializes response cookies into Set-Cookie headers and removes :cookies. Source
wrap-cookies
(wrap-cookies handler)
(wrap-cookies handler options)
Function.
Wraps a handler with cookie parsing on the way in and serialization on the way out. Source