miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

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 (Maybe Cookie)
  | SessionError MisoString
  | SaveTheme
  | ThemeSaved

update :: Action -> Effect p props Model Action
update LoadSession =
  cookieGet "session" GotSession SessionError
update SaveTheme =
  cookieSet "theme" "dark" ThemeSaved SessionError
update _ = pure ()

Types

API groups

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

Synopsis

Types

data Cookie Source #

A cookie from the CookieStore API.

Fields map directly to the browser's CookieListItem.

Constructors

Cookie 

Fields

Instances

Instances details
Show Cookie Source # 
Instance details

Defined in Miso.Cookie

Eq Cookie Source # 
Instance details

Defined in Miso.Cookie

Methods

(==) :: Cookie -> Cookie -> Bool #

(/=) :: Cookie -> Cookie -> Bool #

FromJSVal Cookie Source # 
Instance details

Defined in Miso.Cookie

ToJSVal Cookie Source # 
Instance details

Defined in Miso.Cookie

Methods

toJSVal :: Cookie -> IO JSVal Source #

data CookieChangeEvent Source #

The event payload delivered to cookieStore change listeners. Consumed by cookieChangeSub.

Constructors

CookieChangeEvent 

Fields

Read

cookieGet Source #

Arguments

:: MisoString

Cookie name

-> (Maybe MisoString -> action)

Successful callback (Nothing when the cookie is absent)

-> (MisoString -> action)

Errorful callback

-> Effect parent props model action 

Retrieve a cookie value by name.

Calls successful with Just value when found, Nothing when absent, or errorful with the error message if the operation fails.

https://developer.mozilla.org/en-US/docs/Web/API/CookieStore/get

cookieGetAll Source #

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

cookieSet Source #

Arguments

:: Cookie

Cookie to set (use defaultCookie for the common case)

-> 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

cookieDelete Source #

Arguments

:: MisoString

Cookie name

-> action

Successful callback

-> (MisoString -> action)

Errorful callback

-> Effect parent props model action 

cookieDeleteWith Source #

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 #

Synchronous API variant of cookieSet.

Blocks the calling thread until the browser resolves the promise, returning Right () on success or Left err on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

cookieGet_ :: MisoString -> IO (Either MisoString (Maybe MisoString)) Source #

Synchronous API variant of cookieGet.

Blocks the calling thread until the browser resolves the promise, returning Right (Just value) when the cookie exists, Right Nothing when absent, or Left err on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.

cookieDelete_ :: MisoString -> IO (Either MisoString ()) Source #

Synchronous API variant of cookieDelete.

Blocks the calling thread until the browser resolves the promise, returning Right () on success or Left err on failure.

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 Right () on success or Left err on failure.

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 Right cookies on success or Left err on failure.

Note: best used with io or io_, to avoid blocking the scheduler thread.