-
Notifications
You must be signed in to change notification settings - Fork 4
/
[id].ts
35 lines (30 loc) · 887 Bytes
/
[id].ts
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
import type { NextApiRequest, NextApiResponse } from 'next'
import { User } from 'models/user'
import { deleteUser, getUser, updateUser } from 'services/users'
import { Error, methodNotAllowed, notFound } from 'models/error'
export default function handler(
req: NextApiRequest,
res: NextApiResponse<User | Error>
) {
const id = req.query.id as string
// GET /api/v1/users/:id
if (req.method === 'GET') {
const user = getUser(id)
if (!user) {
return res.status(404).json(notFound)
} else {
return res.status(200).json(user)
}
}
// PATCH /api/v1/users/:id
if (req.method === 'PATCH') {
const user = updateUser(id, req.body)
return res.status(200).json(user)
}
// DELETE /api/v1/users/:id
if (req.method === 'DELETE') {
deleteUser(id)
return res.status(200).end()
}
return res.status(405).json(methodNotAllowed)
}