jh-be-tools 1.0.0
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/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/route-handler/routeHandler.d.ts +12 -0
- package/dist/route-handler/routeHandler.js +37 -0
- package/package.json +24 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./route-handler/routeHandler";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./route-handler/routeHandler"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Request, Response } from "express";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export interface RouteSchema {
|
|
4
|
+
request?: z.ZodSchema;
|
|
5
|
+
response?: z.ZodSchema;
|
|
6
|
+
}
|
|
7
|
+
interface ReturnValue {
|
|
8
|
+
data: unknown;
|
|
9
|
+
status?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare const RouteHandler: (req: Request, res: Response, fun: (req: Request) => Promise<ReturnValue>, schema: RouteSchema) => Promise<Response<any, Record<string, any>> | undefined>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RouteHandler = void 0;
|
|
4
|
+
const RouteHandler = async (req, res, fun, schema) => {
|
|
5
|
+
if (schema.request) {
|
|
6
|
+
try {
|
|
7
|
+
await schema.request.parseAsync({
|
|
8
|
+
body: req.body,
|
|
9
|
+
query: req.query,
|
|
10
|
+
params: req.params,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
return res.status(400).json(error);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
let data;
|
|
18
|
+
let status;
|
|
19
|
+
try {
|
|
20
|
+
const result = await fun(req);
|
|
21
|
+
data = result.data;
|
|
22
|
+
status = result.status ?? 200;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
return res.status(500).json(error);
|
|
26
|
+
}
|
|
27
|
+
if (schema.response) {
|
|
28
|
+
try {
|
|
29
|
+
await schema.response.parseAsync(data);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
return res.status(400).json(error);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
res.status(status).json(data);
|
|
36
|
+
};
|
|
37
|
+
exports.RouteHandler = RouteHandler;
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "jh-be-tools",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "npx tsc --build",
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/express": "^4.17.21",
|
|
14
|
+
"@types/node": "^20.11.5",
|
|
15
|
+
"typescript": "^5.3.3"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"express": "^4.18.2",
|
|
19
|
+
"zod": "^3.22.4"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist/*"
|
|
23
|
+
]
|
|
24
|
+
}
|