-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodules_all.ts
More file actions
60 lines (52 loc) · 1.86 KB
/
modules_all.ts
File metadata and controls
60 lines (52 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Integration example: all virtual modules
// Tests that zigttp:auth, zigttp:validate, and zigttp:cache resolve correctly
import { parseBearer, jwtVerify, jwtSign } from "zigttp:auth";
import { schemaCompile, validateJson, coerceJson } from "zigttp:validate";
import { cacheGet, cacheSet, cacheStats } from "zigttp:cache";
import { sha256 } from "zigttp:crypto";
type RequestBody = {
name: string;
age: number;
};
// Compile a schema on startup
const schema_ok = schemaCompile("user", JSON.stringify({
type: "object",
required: ["name", "age"],
properties: {
name: { type: "string", minLength: 1, maxLength: 100 },
age: { type: "integer", minimum: 0, maximum: 200 }
}
}));
function handler(req: Request): Response {
// Auth: check JWT from Authorization header
const auth_header = req.headers["authorization"];
if (auth_header !== undefined) {
const token = parseBearer(auth_header);
if (token !== undefined) {
const secret = "my-secret-key";
const result = jwtVerify(token, secret);
if (result.ok) {
// Cache the verified user
cacheSet("users", sha256(token), JSON.stringify(result.value), 300);
}
}
}
// Validate request body
if (req.method === "POST") {
const validation = validateJson("user", req.body);
if (!validation.ok) {
return Response.json({ errors: validation.errors }, { status: 400 });
}
// Sign a response token
const jwt = jwtSign(JSON.stringify({ sub: "user-123", iat: 1700000000 }), "my-secret-key");
return Response.json({
user: validation.value,
token: jwt,
cache: cacheStats()
}, { status: 201 });
}
return Response.json({
schema_compiled: schema_ok,
cache: cacheStats()
});
}