-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathlist-users-paginated.ts
More file actions
56 lines (51 loc) · 1.71 KB
/
list-users-paginated.ts
File metadata and controls
56 lines (51 loc) · 1.71 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 { z } from "zod";
import { defaultEndpointsFactory, ez } from "express-zod-api";
const roleSchema = z.enum(["manager", "operator", "admin"]);
const userSchema = z.object({
name: z.string(),
role: roleSchema,
});
const paginatedUsers = ez.paginated({
style: "offset",
itemSchema: userSchema,
itemsName: "users",
maxLimit: 100,
defaultLimit: 20,
});
const users: z.output<typeof userSchema>[] = [
{ name: "Maria Merian", role: "manager" },
{ name: "Mary Anning", role: "operator" },
{ name: "Marie Skłodowska Curie", role: "admin" },
{ name: "Henrietta Leavitt", role: "manager" },
{ name: "Lise Meitner", role: "operator" },
{ name: "Alice Ball", role: "admin" },
{ name: "Gerty Cori", role: "manager" },
{ name: "Helen Taussig", role: "operator" },
];
/**
* Lists users with offset pagination and optional role filter.
* Uses ez.paginated() for input params (limit, offset) and output shape (users, total, limit, offset).
*/
export const listUsersPaginatedEndpoint = defaultEndpointsFactory.build({
tag: "users",
summary: "Lists users with pagination.",
description:
"Returns a page of users. Optionally filter by roles. Uses offset-based pagination (limit and offset).",
input: paginatedUsers.input.and(
z.object({
roles: z
.array(roleSchema)
.optional()
.describe("Filter by roles; omit for all"),
}),
),
output: paginatedUsers.output,
handler: async ({ input: { limit, offset, roles } }) => {
const filtered = roles
? users.filter(({ role }) => roles.includes(role))
: users;
const total = filtered.length;
const page = filtered.slice(offset, offset + limit);
return { users: page, total, limit, offset };
},
});