-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestie.ts
More file actions
201 lines (180 loc) · 3.91 KB
/
testie.ts
File metadata and controls
201 lines (180 loc) · 3.91 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { APIServer, ValidationError, NotFoundError, z } from './dist'
const app = new APIServer({
port: 3000,
apiTitle: 'User API',
apiTags: [{ name: 'Users', description: 'User management' }],
})
// In-memory database
interface User {
id: string
name: string
email: string
createdAt: string
}
const users: User[] = []
// List users
app.createEndpoint({
method: 'GET',
url: '/users',
query: z.object({
limit: z.coerce.number().int().positive().max(100).default(10),
offset: z.coerce.number().int().nonnegative().default(0),
}),
response: z.object({
users: z.array(
z.object({
id: z.string(),
name: z.string(),
email: z.string(),
createdAt: z.string(),
}),
),
total: z.number(),
}),
config: {
description: 'List users with pagination',
tags: ['Users'],
},
handler: async (request) => {
const { limit, offset } = request.query
return {
users: users.slice(offset, offset + limit),
total: users.length,
}
},
})
// Get user by ID
app.createEndpoint({
method: 'GET',
url: '/users/:id',
params: z.object({
id: z.string(),
}),
response: z.object({
id: z.string(),
name: z.string(),
email: z.string(),
createdAt: z.string(),
}),
config: {
description: 'Get a user by ID',
tags: ['Users'],
},
handler: async (request) => {
const { id } = request.params
const user = users.find((u) => u.id === id)
if (!user) {
throw new NotFoundError('User not found')
}
return user
},
})
// Create user
app.createEndpoint({
method: 'POST',
url: '/users',
query: z.object({}),
body: z.object({
name: z.string().min(1).max(100),
email: z.string().email(),
}),
response: z.object({
id: z.string(),
name: z.string(),
email: z.string(),
createdAt: z.string(),
}),
config: {
description: 'Create a new user',
tags: ['Users'],
},
handler: async (request) => {
const { name, email } = request.body
// Check email uniqueness
if (users.some((u) => u.email === email)) {
throw new ValidationError([{ field: 'body.email', message: 'Email already exists' }])
}
const newUser: User = {
id: crypto.randomUUID(),
name,
email,
createdAt: new Date().toISOString(),
}
users.push(newUser)
return newUser
},
})
// Update user
app.createEndpoint({
method: 'PUT',
url: '/users/:id',
params: z.object({
id: z.string(),
}),
body: z.object({
name: z.string().min(1).max(100).optional(),
email: z.string().email().optional(),
}),
response: z.object({
id: z.string(),
name: z.string(),
email: z.string(),
createdAt: z.string(),
}),
config: {
description: 'Update a user',
tags: ['Users'],
},
handler: async (request) => {
const { id } = request.params
const updates = request.body
const userIndex = users.findIndex((u) => u.id === id)
if (userIndex === -1) {
throw new NotFoundError('User not found')
}
// Check email uniqueness if updating email
if (updates.email && users.some((u) => u.email === updates.email && u.id !== id)) {
throw new ValidationError([{ field: 'body.email', message: 'Email already exists' }])
}
users[userIndex] = { ...users[userIndex], ...updates }
return users[userIndex]
},
})
// Delete user
app.createEndpoint({
method: 'DELETE',
url: '/users/:id',
params: z.object({
id: z.string(),
}),
response: z.object({
message: z.string(),
}),
config: {
description: 'Delete a user',
tags: ['Users'],
},
handler: async (request) => {
const { id } = request.params
const index = users.findIndex((u) => u.id === id)
if (index === -1) {
throw new NotFoundError('User not found')
}
users.splice(index, 1)
return { message: 'User deleted' }
},
})
// Protected admin endpoint
app.instance.register(async (scope) => {
scope.addHook('onRequest', app.authenticateToken)
scope.route({
method: 'GET',
url: '/admin/stats',
handler: async () => ({
totalUsers: users.length,
timestamp: new Date().toISOString(),
}),
})
})
app.setupGracefulShutdown()
await app.start()