/
getUser.ts
47 lines (39 loc) · 1.14 KB
/
getUser.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
36
37
38
39
40
41
42
43
44
45
46
47
import { Static, Type } from "@sinclair/typebox";
import fp from "fastify-plugin";
const QuerystringSchema = Type.Object({
identityId: Type.String(),
});
const ResponsesSchema = Type.Object({
200: Type.Object({ user: Type.Object({ identityId: Type.String() }) }),
404: Type.Object({ code: Type.Literal("USER_NOT_FOUND") }),
501: Type.Null({ description: "此功能在当前服务器配置下不可用" }),
});
/**
* 查询用户信息
*/
export const getUserRoute = fp(async (f) => {
f.get<{
Querystring: Static<typeof QuerystringSchema>
Responses: Static<typeof ResponsesSchema>,
}>(
"/user",
{
schema: {
querystring: QuerystringSchema,
response: ResponsesSchema.properties,
},
},
async (req, rep) => {
if (!f.auth.getUser) {
return await rep.code(501).send(null);
}
const { identityId } = req.query;
const result = await f.auth.getUser(identityId, req);
if (result) {
return rep.code(200).send({ user: { identityId: result.identityId } });
} else {
return rep.code(404).send({ code: "USER_NOT_FOUND" });
}
},
);
});