| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.Cookie
Description
Overview
Miso.Cookie wraps the browser's
CookieStore API
as Effect combinators that integrate directly into the
Model-View-Update loop.
Quick start
import Miso import Miso.Cookie data Action = LoadSession | GotSession (MaybeCookie) | SessionErrorMisoString| SaveTheme | ThemeSaved update :: Action ->Effectp props Model Action update LoadSession =cookieGet"session" GotSession SessionError update SaveTheme =cookieSet"theme" "dark" ThemeSaved SessionError update _ = pure ()
Types
Cookie— a single cookie with all standard fieldsCookieChangeEvent— payload forcookieChangeSub
API groups
- Read:
cookieGet,cookieGetAll - Write:
cookieSet - Delete:
cookieDelete
Availability
The CookieStore API requires a
secure context
(HTTPS or localhost) and is not yet universally supported. Operations
silently call the error callback when the API is unavailable.
See also
- Miso.Subscription.Cookie —
cookieChangeSubfor subscribing to cookie change events - Miso.FFI — raw FFI primitives (
cookieGetetc.) for advanced use-cases
Synopsis
- data Cookie = Cookie {}
- data CookieChangeEvent = CookieChangeEvent {
- cookiesChanged :: [Cookie]
- cookiesDeleted :: [Cookie]
- cookieGet :: MisoString -> (Maybe MisoString -> action) -> (MisoString -> action) -> Effect parent props model action
- cookieGetAll :: ([Cookie] -> action) -> (MisoString -> action) -> Effect parent props model action
- cookieSet :: Cookie -> action -> (MisoString -> action) -> Effect parent props model action
- cookieDelete :: MisoString -> action -> (MisoString -> action) -> Effect parent props model action
- cookieDeleteWith :: Cookie -> action -> (MisoString -> action) -> Effect parent props model action
- defaultCookie :: MisoString -> MisoString -> Cookie
- cookieSet_ :: Cookie -> IO (Either MisoString ())
- cookieGet_ :: MisoString -> IO (Either MisoString (Maybe MisoString))
- cookieDelete_ :: MisoString -> IO (Either MisoString ())
- cookieDeleteWith_ :: Cookie -> IO (Either MisoString ())
- cookieGetAll_ :: IO (Either MisoString [Cookie])
Types
A cookie from the CookieStore API.
Fields map directly to the browser's CookieListItem.
Constructors
| Cookie | |
Fields
| |
data CookieChangeEvent Source #
The event payload delivered to
cookieStore change
listeners. Consumed by cookieChangeSub.
Constructors
| CookieChangeEvent | |
Fields
| |
Instances
| Show CookieChangeEvent Source # | |
Defined in Miso.Cookie Methods showsPrec :: Int -> CookieChangeEvent -> ShowS # show :: CookieChangeEvent -> String # showList :: [CookieChangeEvent] -> ShowS # | |
| Eq CookieChangeEvent Source # | |
Defined in Miso.Cookie Methods (==) :: CookieChangeEvent -> CookieChangeEvent -> Bool # (/=) :: CookieChangeEvent -> CookieChangeEvent -> Bool # | |
| FromJSVal CookieChangeEvent Source # | |
Defined in Miso.Cookie Methods fromJSVal :: JSVal -> IO (Maybe CookieChangeEvent) Source # fromJSValUnchecked :: JSVal -> IO CookieChangeEvent Source # | |
| ToArgs CookieChangeEvent Source # | |
Defined in Miso.Cookie | |
| ToJSVal CookieChangeEvent Source # | |
Defined in Miso.Cookie | |
Read
Arguments
| :: MisoString | Cookie name |
| -> (Maybe MisoString -> action) | Successful callback ( |
| -> (MisoString -> action) | Errorful callback |
| -> Effect parent props model action |
Retrieve a cookie value by name.
Calls successful with when found, Just value when
absent, or Nothingerrorful with the error message if the operation fails.
https://developer.mozilla.org/en-US/docs/Web/API/CookieStore/get
Arguments
| :: ([Cookie] -> action) | Successful callback |
| -> (MisoString -> action) | Errorful callback |
| -> Effect parent props model action |
Retrieve all cookies visible to the current document.
https://developer.mozilla.org/en-US/docs/Web/API/CookieStore/getAll
Write
Arguments
| :: Cookie | Cookie to set (use |
| -> action | Successful callback |
| -> (MisoString -> action) | Errorful callback |
| -> Effect parent props model action |
Set a cookie via the CookieStore API.
Use defaultCookie to construct a Cookie with sensible defaults, or
supply a fully specified Cookie record for custom path, domain, expiry etc.
Important: You cannot use both expires and maxAge in the same call.
If you do, the set() method will fail with a TypeError.
https://developer.mozilla.org/en-US/docs/Web/API/CookieStore/set
Delete
Arguments
| :: MisoString | Cookie name |
| -> action | Successful callback |
| -> (MisoString -> action) | Errorful callback |
| -> Effect parent props model action |
Delete a cookie by name.
https://developer.mozilla.org/en-US/docs/Web/API/CookieStore/delete
Arguments
| :: Cookie | Cookie whose name, path, domain and partitioned fields are used for matching |
| -> action | Successful callback |
| -> (MisoString -> action) | Errorful callback |
| -> Effect parent props model action |
Delete a cookie by matching on name, path, domain, and/or partitioned flag — useful when multiple cookies share the same name across different paths or domains.
Use defaultCookie to build the argument and override only the fields
you need:
cookieDeleteWith(defaultCookie"session" "") {cookiePath= "/admin" } Deleted SessionError
https://developer.mozilla.org/en-US/docs/Web/API/CookieStore/delete
Construction
defaultCookie :: MisoString -> MisoString -> Cookie Source #
A Cookie with sensible defaults: path = "/", session expiry,
SameSite = "lax", not secure, not partitioned, no domain restriction.
Synchronous API variants
cookieSet_ :: Cookie -> IO (Either MisoString ()) Source #
cookieGet_ :: MisoString -> IO (Either MisoString (Maybe MisoString)) Source #
cookieDelete_ :: MisoString -> IO (Either MisoString ()) Source #
Synchronous API variant of cookieDelete.
Blocks the calling thread until the browser resolves the promise, returning
on success or Right () on failure.Left err
Note: best used with io or io_, to avoid blocking the scheduler thread.
cookieDeleteWith_ :: Cookie -> IO (Either MisoString ()) Source #
Synchronous API variant of cookieDeleteWith.
Blocks the calling thread until the browser resolves the promise, returning
on success or Right () on failure.Left err
Note: best used with io or io_, to avoid blocking the scheduler thread.
cookieGetAll_ :: IO (Either MisoString [Cookie]) Source #
Synchronous API variant of cookieGetAll.
Blocks the calling thread until the browser resolves the promise, returning
on success or Right cookies on failure.Left err
Note: best used with io or io_, to avoid blocking the scheduler thread.