@elysiajs/cookie
Plugin for Elysia that add support for reading, and setting cookie.
Installation
bun add @elysiajs/cookie
Example
import { Elysia } from 'elysia'
import { cookie } from '@elysiajs/cookie'
const app = new Elysia()
.use(cookie())
.get('/', ({ cookie: { user }, setCookie, removeCookie }) => {
if (user === 'admin') unsetCookie('user')
if (!user)
setCookie('user', 'saltyaom', {
httpOnly: true
})
return `Hi, ${user}!`
})
.listen(8080)
API
cookie
Parsed cookie from request in form of Record<string, string>
cookie: Record<string, string>
setCookie
Function to set new cookie
setCookie: (
name: string,
value: string,
options?: CookieSerializeOptions
) => void
removeCookie
A function to remove cookie
removeCookie: (name: string) => void
unsignCookie
Unsign cookie
unsignCookie: (name: string) => void
Serialize Options
Serialize option is based on cookie package
export interface CookieSerializeOptions {
domain?: string | undefined;
encode?(value: string): string;
expires?: Date | undefined;
httpOnly?: boolean | undefined;
maxAge?: number | undefined;
path?: string | undefined;
priority?: 'low' | 'medium' | 'high' | undefined;
sameSite?: true | false | 'lax' | 'strict' | 'none' | undefined;
secure?: boolean | undefined;
}
Caveat
As the current state of Bun (0.1.4), there's no current way to set multiple cookie at once as Bun doesn't support setting multiple headers with same name which is required for setting multiple cookie in one response.