jh-be-tools 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jh-be-tools",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
6
5
|
"types": "index.d.ts",
|
|
7
6
|
"scripts": {
|
|
8
7
|
"build": "npx tsc --build && cp package.json dist/.",
|
|
@@ -20,6 +19,7 @@
|
|
|
20
19
|
"dotenv": "^16.4.5",
|
|
21
20
|
"express": "^4.21.1",
|
|
22
21
|
"knex": "^3.1.0",
|
|
22
|
+
"pg": "^8.13.1",
|
|
23
23
|
"zod": "^3.23.8"
|
|
24
24
|
},
|
|
25
25
|
"bin": {
|
|
@@ -1,56 +1,46 @@
|
|
|
1
|
-
import { Request, Response } from
|
|
2
|
-
import { z } from
|
|
1
|
+
import { Request, Response } from 'express'
|
|
2
|
+
import { z } from 'zod'
|
|
3
3
|
|
|
4
4
|
export interface RouteSchema {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
request?: z.ZodSchema
|
|
6
|
+
response?: z.ZodSchema
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
interface ReturnValue<T> {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
data: T
|
|
11
|
+
status?: number
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
export const routeHandler = async (
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
});
|
|
27
|
-
} catch (error) {
|
|
28
|
-
console.log(error);
|
|
29
|
-
console.log(req.body);
|
|
30
|
-
console.log(req.query);
|
|
31
|
-
console.log(req.params);
|
|
32
|
-
return res.status(400).json(error);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
14
|
+
export const routeHandler = async (req: Request, res: Response, fun: (req?: any) => Promise<ReturnValue<unknown> | void>, schema: RouteSchema) => {
|
|
15
|
+
if (schema.request) {
|
|
16
|
+
try {
|
|
17
|
+
await schema.request.parseAsync({
|
|
18
|
+
body: req.body,
|
|
19
|
+
query: req.query,
|
|
20
|
+
params: req.params,
|
|
21
|
+
})
|
|
22
|
+
} catch (error) {
|
|
23
|
+
return res.status(400).json(error)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
35
26
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
27
|
+
let data
|
|
28
|
+
let status
|
|
29
|
+
try {
|
|
30
|
+
const result = await fun(req as any)
|
|
31
|
+
data = result?.data
|
|
32
|
+
const defaultStatus = result?.data ? 200 : 204
|
|
33
|
+
status = result?.status ?? defaultStatus
|
|
34
|
+
} catch (error) {
|
|
35
|
+
return res.status(500).json(error)
|
|
36
|
+
}
|
|
46
37
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
};
|
|
38
|
+
if (schema.response) {
|
|
39
|
+
try {
|
|
40
|
+
await schema.response.parseAsync({ data })
|
|
41
|
+
} catch (error) {
|
|
42
|
+
return res.status(500).json(error)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
res.status(status).json({ data })
|
|
46
|
+
}
|