Cookie â
To use Cookie, you can extract the cookie property and access its name and value directly.
There's no get/set, you can extract the cookie name and retrieve or update its value directly.
app.get('/', ({ cookie: { name } }) => {
// Get
name.value
// Set
name.value = "New Value"
name.value = {
hello: 'world'
}
})
app.get('/', ({ cookie: { name } }) => {
// Get
name.value
// Set
name.value = "New Value"
name.value = {
hello: 'world'
}
})
By default, Reactive Cookie can encode/decode type of object automatically allowing us to treat cookie as an object without worrying about the encoding/decoding. It just works.
Reactivity â
The Elysia cookie is reactive. This means that when you change the cookie value, the cookie will be updated automatically based on approach like signal.
A single source of truth for handling cookies is provided by Elysia cookies, which have the ability to automatically set headers and sync cookie values.
Since cookies are Proxy-dependent objects by default, the extract value can never be undefined; instead, it will always be a value of Cookie<unknown>
, which can be obtained by invoking the .value property.
We can treat the cookie jar as a regular object, iteration over it will only iterate over an already-existing cookie value.
Cookie Attribute â
To use Cookie attribute, you can either use one of the following:
- Setting the property directly
- Using
set
oradd
to update cookie property.
See cookie attribute config for more information.
Assign Property â
You can get/set the property of a cookie like any normal object, the reactivity model synchronizes the cookie value automatically.
app.get('/', ({ cookie: { name } }) => {
// get
name.domain
// set
name.domain = 'millennium.sh'
name.httpOnly = true
})
app.get('/', ({ cookie: { name } }) => {
// get
name.domain
// set
name.domain = 'millennium.sh'
name.httpOnly = true
})
set â
set permits updating multiple cookie properties all at once through reset all property and overwrite the property with a new value.
app.get('/', ({ cookie: { name } }) => {
name.set({
domain: 'millennium.sh',
httpOnly: true
})
})
app.get('/', ({ cookie: { name } }) => {
name.set({
domain: 'millennium.sh',
httpOnly: true
})
})
add â
Like set, add allow us to update multiple cookie properties at once, but instead, will only overwrite the property defined instead of resetting.
remove â
To remove a cookie, you can use either:
- name.remove
- delete cookie.name
app.get('/', ({ cookie, cookie: { name } }) => {
name.remove()
delete cookie.name
})
app.get('/', ({ cookie, cookie: { name } }) => {
name.remove()
delete cookie.name
})
Cookie Schema â
You can strictly validate cookie type and providing type inference for cookie by using cookie schema with t.Cookie
.
app.get('/', ({ cookie: { name } }) => {
// Set
name.value = {
id: 617,
name: 'Summoning 101'
}
}, {
cookie: t.Cookie({
name: t.Object({
id: t.Numeric(),
name: t.String()
})
})
})
app.get('/', ({ cookie: { name } }) => {
// Set
name.value = {
id: 617,
name: 'Summoning 101'
}
}, {
cookie: t.Cookie({
name: t.Object({
id: t.Numeric(),
name: t.String()
})
})
})
Nullable Cookie â
To handle nullable cookie value, you can use t.Optional
on the cookie name you want to be nullable.
app.get('/', ({ cookie: { name } }) => {
// Set
name.value = {
id: 617,
name: 'Summoning 101'
}
}, {
cookie: t.Cookie({
value: t.Optional(
t.Object({
id: t.Numeric(),
name: t.String()
})
)
})
})
app.get('/', ({ cookie: { name } }) => {
// Set
name.value = {
id: 617,
name: 'Summoning 101'
}
}, {
cookie: t.Cookie({
value: t.Optional(
t.Object({
id: t.Numeric(),
name: t.String()
})
)
})
})