Skip to content

Commit c0d4d71

Browse files
committed
Add cookies documentation
1 parent 41a75f9 commit c0d4d71

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

src/app/views/documentation/_sidebar.zmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<li>
3131
@partial documentation_link("Headers", "requests/headers")
3232
</li>
33+
<li>
34+
@partial documentation_link("Cookies", "requests/cookies")
35+
</li>
3336
<li>
3437
@partial documentation_link("Rendering", "requests/rendering")
3538
</li>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Cookies
2+
3+
Cookies in _Jetzig_ are accessible via the `cookies` method on a `Request` within a view function:
4+
5+
```zig
6+
pub fn index(request: *jetzig.Request) !jetzig.View {
7+
var cookies = try request.cookies();
8+
try cookies.put(.{ .name = "foo", .value = "bar", .same_site = .lax });
9+
const cookie = cookies.get("foo");
10+
// ...
11+
}
12+
```
13+
14+
The `Cookie` type provides the following fields which can be passed to `put`:
15+
16+
```zig
17+
name: []const u8,
18+
value: []const u8,
19+
secure: bool,
20+
http_only: bool,
21+
partitioned: bool,
22+
domain: ?[]const u8,
23+
path: ?[]const u8,
24+
same_site: ?SameSite,
25+
expires: ?i64,
26+
max_age: ?i64,
27+
```
28+
29+
The `same_site` field is an optional `enum`:
30+
31+
```zig
32+
const SameSite = enum { strict, lax, none };
33+
```
34+
35+
## Global Configuration
36+
37+
Default values for most cookie fields can be specified by the `cookies` declaration in your application's `jetzig_options` (in `src/main.zig`).
38+
39+
For example:
40+
41+
```zig
42+
pub const cookies: jetzig.http.Cookies.CookieOptions = switch (jetzig.environment) {
43+
.development, .testing => .{
44+
.domain = "localhost",
45+
.path = "/",
46+
},
47+
.production => .{
48+
.same_site = .lax,
49+
.secure = true,
50+
.http_only = true,
51+
.path = "/",
52+
},
53+
};
54+
```
55+
56+
## Cookie Store Operations
57+
58+
### `get`
59+
60+
```zig
61+
pub fn get(self: *Cookies, key: []const u8) ?*Cookie
62+
```
63+
64+
Return a `Cookie` if found in the cookie store, `null` otherwise.
65+
66+
#### Example
67+
68+
```zig
69+
pub fn index(request: *jetzig.Request) !jetzig.View {
70+
var root = try request.data(.object);
71+
72+
var cookies = try request.cookies();
73+
74+
if (cookies.get("country-code")) |cookie| {
75+
try root.put("location", cookie.value);
76+
} else {
77+
try root.put("location", null);
78+
}
79+
80+
return request.render(.ok);
81+
}
82+
```
83+
84+
### `put`
85+
86+
```zig
87+
pub fn put(self: *Cookies, cookie: Cookie) !void
88+
```
89+
90+
Add a cookie to the cookie store. See `Cookie` field specification above for available struct initialization parameters.
91+
92+
#### Example
93+
94+
```zig
95+
pub fn index(request: *jetzig.Request) !jetzig.View {
96+
var cookies = try request.cookies();
97+
const random_number = try std.fmt.allocPrint(request.allocator, "{}", std.crypto.random.int(u16));
98+
try cookies.put(.{
99+
.name = "random-number",
100+
.value = random_number,
101+
102+
return request.render(.ok);
103+
}
104+
```
105+
106+
### `delete`
107+
108+
```zig
109+
pub fn delete(self: *Cookies, key: []const u8) !void
110+
```
111+
112+
Delete a cookie.
113+
114+
Note that deletion is implemented by setting a cookie whose value is an empty string and whose expiry is in the past (`0`, i.e. Unix epoch).
115+
116+
#### Example
117+
118+
```zig
119+
pub fn index(request: *jetzig.Request) !jetzig.View {
120+
var cookies = try request.cookies();
121+
try cookies.delete("temporary-token");
122+
123+
return request.render(.ok);
124+
}
125+
```

0 commit comments

Comments
 (0)