-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathregistryRoutersV2.ts
127 lines (110 loc) · 4.07 KB
/
registryRoutersV2.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
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
import { FastifyInstance, FastifyRequestTypebox, Response } from "../types/Server";
import { FastifyReply } from "fastify";
import { Status } from "../constants/Project";
import { FError } from "../error/ControllerError";
import { ErrorCode } from "../ErrorCode";
import { kAPILogger } from "../plugins/fastify/api-logger";
import { parseError } from "../logger";
const registerRouters =
(version: `v${number}`) =>
(fastifyServer: FastifyInstance, controllers: Array<(server: Server) => void>) => {
const routerHandle = (method: "get" | "post"): Router => {
return <S>(
path: string,
handler: (req: FastifyRequestTypebox<S>, reply: FastifyReply) => Promise<any>,
config: {
auth?: boolean;
schema: S;
autoHandle?: boolean;
enable?: boolean;
},
) => {
const autoHandle = config.autoHandle === undefined || config.autoHandle;
const auth = config.auth === undefined || config.auth;
const enable = config.enable === undefined || config.enable;
if (!enable) {
return;
}
fastifyServer[method](
`/${version}/${path}`,
{
preValidation: auth ? [(fastifyServer as any).authenticate] : undefined,
schema: config.schema,
},
async (req, reply: FastifyReply) => {
if (!autoHandle) {
req.notAutoHandle = true;
}
let resp: Response | null = null;
const request = Object.assign(req, {
// @ts-ignore
userUUID: req?.user?.userUUID,
// @ts-ignore
loginSource: req?.user?.loginSource,
DBTransaction: req.queryRunner.manager,
});
try {
const result = await handler(request, reply);
if (autoHandle) {
resp = result as Response;
}
} catch (error) {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
req[kAPILogger].error("request failed", parseError(error));
if (autoHandle) {
resp = errorToResp(error as Error);
} else {
throw error;
}
}
if (resp) {
await reply.send(resp);
}
return reply;
},
);
};
};
const server: Server = {
get: routerHandle("get"),
post: routerHandle("post"),
};
controllers.forEach(controller => {
controller(server);
});
};
const errorToResp = (error: Error): Response => {
if (error instanceof FError) {
return {
status: error.status,
code: error.errorCode,
};
} else {
return {
status: Status.Failed,
code: ErrorCode.CurrentProcessFailed,
};
}
};
export const registerV2Routers = registerRouters("v2");
interface R<O> {
<S>(
path: string,
handler: (
req: FastifyRequestTypebox<S>,
reply: FastifyReply,
) => Promise<O extends false ? void : Response>,
config: {
auth?: boolean;
schema: S;
autoHandle?: O;
enable?: boolean;
},
): void;
}
interface Router extends R<true>, R<false> {}
export interface Server {
get: Router;
post: Router;
}