-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathretrieve-user.ts
More file actions
56 lines (54 loc) · 1.63 KB
/
retrieve-user.ts
File metadata and controls
56 lines (54 loc) · 1.63 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
import createHttpError from "http-errors";
import assert from "node:assert/strict";
import { z } from "zod";
import { defaultEndpointsFactory } from "express-zod-api";
import { methodProviderMiddleware } from "../middlewares.ts";
// Demonstrating circular schemas using z.object()
const feature = z.object({
title: z.string(),
get features() {
return z.array(feature).optional();
},
});
export const retrieveUserEndpoint = defaultEndpointsFactory
.addMiddleware(methodProviderMiddleware)
.build({
tag: "users",
shortDescription: "Retrieves the user.",
description: "Example user retrieval endpoint.",
input: z.object({
id: z
.string()
.trim()
.regex(/\d+/)
.transform((id) => parseInt(id, 10))
.describe("a numeric string containing the id of the user"),
}),
output: z.object({
id: z.int().nonnegative(),
name: z.string(),
features: feature.array(), // @link https://github.com/colinhacks/zod/issues/4592
}),
handler: async ({ input: { id }, options: { method }, logger }) => {
logger.debug(`Requested id: ${id}, method ${method}`);
const name = "John Doe";
assert(id <= 100, createHttpError(404, "User not found"));
return {
id,
name,
features: [
{ title: "Tall", features: [{ title: "Above 180cm" }] },
{ title: "Young" },
{
title: "Cute",
features: [
{
title: "Tells funny jokes",
features: [{ title: "About Typescript" }],
},
],
},
],
};
},
});