-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathderive-types.ts
More file actions
53 lines (44 loc) · 1.49 KB
/
Copy pathderive-types.ts
File metadata and controls
53 lines (44 loc) · 1.49 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
// Derive related shapes from one source type with the utility-type family:
// Pick, Omit, Partial, Required. zigts resolves these structurally - a named
// source type is looked up and transformed - so each field stays declared in
// exactly one place instead of being copied into hand-written aliases that can
// drift.
import type { Spec } from "zigttp:types";
type Guardrails = Spec<
| "deterministic"
| "read_only"
| "retry_safe"
| "idempotent"
| "state_isolated"
| "injection_safe"
| "no_secret_leakage"
| "input_validated"
>;
type User = {
id: number;
name: string;
email: string;
age: number;
};
// Pick: a public summary that carries only id and name.
type Summary = Pick<User, "id" | "name">;
// Omit: the same record without the sensitive email.
type Safe = Omit<User, "email">;
// Partial: every field optional, for a patch payload.
type UserPatch = Partial<User>;
// Required: force every optional field present, deriving a fully-specified
// config from a loosely-typed source.
type RawConfig = { host?: string; port?: number };
type Config = Required<RawConfig>;
const summary: Summary = { id: 1, name: "Ada" };
const safe: Safe = { id: 2, name: "Grace", age: 36 };
const patch: UserPatch = { name: "Hedy" };
const config: Config = { host: "0.0.0.0", port: 8080 };
function handler(req: Request): Response & Guardrails {
return Response.json({
summary: summary,
safe: safe,
patch: patch,
config: config,
});
}