Cookie
To access cookies from your request, you can use the Cookie
parameter.
INFO
This parameter automatically registers @fastify/cookie
in your Fastify instance.
You can further configure the plugin by passing fastifyCookie
to the Kita plugin options.
Cookies are always of type string
.
ts
import type { Cookie } from '@kitajs/runtime';
export function get(cookie: Cookie) {
return `You used the header 'Cookie: cookie=${cookie}'!`;
}
1
json
{
"paths": {
"/": {
"get": {
"operationId": "getIndex",
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "string" } }
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Custom name
You can customize the name of the cookie you want to access by passing a string literal as an argument to the Cookie
parameter.
ts
import type { Cookie } from '@kitajs/runtime';
// Access the cookie named 'token'
export function get(anything: Cookie<'token'>) {
return `This is your token: ${anything}`;
}
1
json
{
"paths": {
"/": {
"get": {
"operationId": "getIndex",
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "string" } }
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Default values
Default values can be used with Cookie
.
ts
import type { Cookie } from '@kitajs/runtime';
export function get(cookie: Cookie = 'Arthur') {
return `Hello ${cookie}!`;
}
1
json
{
"paths": {
"/": {
"get": {
"operationId": "getIndex",
"responses": {
"2XX": {
"description": "Default Response",
"content": {
"application/json": { "schema": { "type": "string" } }
}
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17