gg-express 1.0.140 → 1.0.142
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/v3/GGExpressV3.js +3 -2
- package/dist/v3/autoNumber.d.ts +4 -0
- package/dist/v3/autoNumber.js +32 -0
- package/dist/v3/generateGGApi_v3.js +1 -1
- package/dist/v3/output/apiConnector_hotel_v3.js +4 -6
- package/dist/v3/run_v3.test.js +27 -8
- package/package.json +1 -1
- package/src/v3/GGExpressV3.ts +3 -2
- package/src/v3/autoNumber.ts +35 -0
- package/src/v3/generateGGApi_v3.ts +1 -1
- package/src/v3/output/apiConnector_hotel_v3.ts +11 -12
- package/src/v3/output/staticRouteInterface_hotel_v3.ts +33 -37
- package/src/v3/run_v3.test.ts +28 -8
package/dist/v3/GGExpressV3.js
CHANGED
|
@@ -16,6 +16,7 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
16
16
|
const path_1 = __importDefault(require("path"));
|
|
17
17
|
const generateStaticRouteFileV3_1 = require("./generateStaticRouteFileV3");
|
|
18
18
|
const generateGGApi_v3_1 = require("./generateGGApi_v3");
|
|
19
|
+
const autoNumber_1 = require("./autoNumber");
|
|
19
20
|
const myExpressRouteList = [];
|
|
20
21
|
function wrapAsync(fn) {
|
|
21
22
|
return (req, res, next) => {
|
|
@@ -45,8 +46,8 @@ class GGExpressV3 {
|
|
|
45
46
|
});
|
|
46
47
|
});
|
|
47
48
|
return this.express[method](url,
|
|
48
|
-
//
|
|
49
|
-
|
|
49
|
+
// (req: Request, res: Response, next: NextFunction) => next(),
|
|
50
|
+
autoNumber_1.autoTypeMiddleware, (req, res, next) => {
|
|
50
51
|
if (method === "get")
|
|
51
52
|
//@ts-ignore
|
|
52
53
|
options.requireParams.data.parse(req.query.data.data);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.autoTypeMiddleware = void 0;
|
|
4
|
+
exports.smartParse = smartParse;
|
|
5
|
+
function autoNumber(value) {
|
|
6
|
+
if (Array.isArray(value)) {
|
|
7
|
+
return value.map(autoNumber);
|
|
8
|
+
}
|
|
9
|
+
if (value && typeof value === "object") {
|
|
10
|
+
const newObj = {};
|
|
11
|
+
for (const key in value) {
|
|
12
|
+
newObj[key] = autoNumber(value[key]);
|
|
13
|
+
}
|
|
14
|
+
return newObj;
|
|
15
|
+
}
|
|
16
|
+
// convert numeric string
|
|
17
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
18
|
+
const num = Number(value);
|
|
19
|
+
if (!Number.isNaN(num))
|
|
20
|
+
return num;
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function smartParse(schema, input) {
|
|
25
|
+
return schema.parse(autoNumber(input));
|
|
26
|
+
}
|
|
27
|
+
const autoTypeMiddleware = (req, res, next) => {
|
|
28
|
+
req.body = autoNumber(req.body);
|
|
29
|
+
req.query = autoNumber(req.query);
|
|
30
|
+
next();
|
|
31
|
+
};
|
|
32
|
+
exports.autoTypeMiddleware = autoTypeMiddleware;
|
|
@@ -23,7 +23,7 @@ export class GGApi_v3 {
|
|
|
23
23
|
): Promise<${tempInterfaceName}["get"][T]["responseStructure"]> {
|
|
24
24
|
return new Promise((resolve, reject) => {
|
|
25
25
|
axios
|
|
26
|
-
.get(this.urlPrefix + url as string, { params: requireParams })
|
|
26
|
+
.get(this.urlPrefix + url as string, { params: { data : requireParams} })
|
|
27
27
|
.then((response) => {
|
|
28
28
|
resolve(response.data)
|
|
29
29
|
})
|
|
@@ -12,7 +12,7 @@ class GGApi_v3 {
|
|
|
12
12
|
get(url, requireParams) {
|
|
13
13
|
return new Promise((resolve, reject) => {
|
|
14
14
|
axios_1.default
|
|
15
|
-
.get(
|
|
15
|
+
.get(this.urlPrefix + url, { params: { data: requireParams } })
|
|
16
16
|
.then((response) => {
|
|
17
17
|
resolve(response.data);
|
|
18
18
|
})
|
|
@@ -25,7 +25,7 @@ class GGApi_v3 {
|
|
|
25
25
|
post(url, requireParams) {
|
|
26
26
|
return new Promise((resolve, reject) => {
|
|
27
27
|
axios_1.default
|
|
28
|
-
.post(
|
|
28
|
+
.post(this.urlPrefix + url, { data: requireParams })
|
|
29
29
|
.then((response) => {
|
|
30
30
|
resolve(response.data);
|
|
31
31
|
})
|
|
@@ -38,7 +38,7 @@ class GGApi_v3 {
|
|
|
38
38
|
put(url, requireParams) {
|
|
39
39
|
return new Promise((resolve, reject) => {
|
|
40
40
|
axios_1.default
|
|
41
|
-
.put(
|
|
41
|
+
.put(this.urlPrefix + url, { data: requireParams })
|
|
42
42
|
.then((response) => {
|
|
43
43
|
resolve(response.data);
|
|
44
44
|
})
|
|
@@ -51,9 +51,7 @@ class GGApi_v3 {
|
|
|
51
51
|
delete(url, requireParams) {
|
|
52
52
|
return new Promise((resolve, reject) => {
|
|
53
53
|
axios_1.default
|
|
54
|
-
.delete(
|
|
55
|
-
data: { data: requireParams },
|
|
56
|
-
})
|
|
54
|
+
.delete(this.urlPrefix + url, { data: { data: requireParams } })
|
|
57
55
|
.then((response) => {
|
|
58
56
|
resolve(response.data);
|
|
59
57
|
})
|
package/dist/v3/run_v3.test.js
CHANGED
|
@@ -47,6 +47,7 @@ const addMyTestMW = (req, res, next) => {
|
|
|
47
47
|
exports.addMyTestMW = addMyTestMW;
|
|
48
48
|
function run() {
|
|
49
49
|
const app = (0, express_1.default)();
|
|
50
|
+
app.use(express_1.default.json());
|
|
50
51
|
const ggapp = new GGExpressV3_1.default(app, "hotel", ["./output"]);
|
|
51
52
|
ggapp.get("/api/v2/hotel/users/id", {
|
|
52
53
|
requireParams: {
|
|
@@ -68,7 +69,7 @@ function run() {
|
|
|
68
69
|
},
|
|
69
70
|
message: "",
|
|
70
71
|
});
|
|
71
|
-
}
|
|
72
|
+
});
|
|
72
73
|
ggapp.post("/api/v2/hotel/item", {
|
|
73
74
|
requireParams: {
|
|
74
75
|
data: zod_1.default.object({
|
|
@@ -92,17 +93,35 @@ function run() {
|
|
|
92
93
|
message: "",
|
|
93
94
|
});
|
|
94
95
|
});
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
})
|
|
96
|
+
// api
|
|
97
|
+
// .get("/api/v2/hotel/users/id", {
|
|
98
|
+
// data: {
|
|
99
|
+
// name: "tin",
|
|
100
|
+
// age: 30,
|
|
101
|
+
// },
|
|
102
|
+
// })
|
|
103
|
+
// .catch((error) => {})
|
|
102
104
|
app.listen(3002, () => __awaiter(this, void 0, void 0, function* () {
|
|
103
105
|
yield ggapp.generateAPIFiles();
|
|
104
106
|
console.log("done");
|
|
105
107
|
// process.exit(0)
|
|
108
|
+
const api = new apiConnector_hotel_v3_1.GGApi_v3({ urlPrefix: "http://127.0.0.1:3002" });
|
|
109
|
+
// api
|
|
110
|
+
// .post("/api/v2/hotel/item", {
|
|
111
|
+
// data: {
|
|
112
|
+
// name: "tin",
|
|
113
|
+
// price: 20,
|
|
114
|
+
// },
|
|
115
|
+
// })
|
|
116
|
+
// .catch((error) => {})
|
|
117
|
+
api
|
|
118
|
+
.get("/api/v2/hotel/users/id", {
|
|
119
|
+
data: {
|
|
120
|
+
name: "tin",
|
|
121
|
+
age: 30,
|
|
122
|
+
},
|
|
123
|
+
})
|
|
124
|
+
.catch((error) => { });
|
|
106
125
|
}));
|
|
107
126
|
}
|
|
108
127
|
run();
|
package/package.json
CHANGED
package/src/v3/GGExpressV3.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
routeListV3,
|
|
10
10
|
} from "./generateStaticRouteFileV3"
|
|
11
11
|
import { generateGGApi_v3 } from "./generateGGApi_v3"
|
|
12
|
+
import { autoTypeMiddleware } from "./autoNumber"
|
|
12
13
|
|
|
13
14
|
export type Method = "get" | "post" | "put" | "delete"
|
|
14
15
|
|
|
@@ -101,8 +102,8 @@ export default class GGExpressV3<
|
|
|
101
102
|
|
|
102
103
|
return this.express[method](
|
|
103
104
|
url,
|
|
104
|
-
//
|
|
105
|
-
|
|
105
|
+
// (req: Request, res: Response, next: NextFunction) => next(),
|
|
106
|
+
autoTypeMiddleware,
|
|
106
107
|
(req, res, next: NextFunction) => {
|
|
107
108
|
if (method === "get")
|
|
108
109
|
//@ts-ignore
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from "express"
|
|
2
|
+
import { z } from "zod"
|
|
3
|
+
function autoNumber(value: any): any {
|
|
4
|
+
if (Array.isArray(value)) {
|
|
5
|
+
return value.map(autoNumber)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (value && typeof value === "object") {
|
|
9
|
+
const newObj: any = {}
|
|
10
|
+
for (const key in value) {
|
|
11
|
+
newObj[key] = autoNumber(value[key])
|
|
12
|
+
}
|
|
13
|
+
return newObj
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// convert numeric string
|
|
17
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
18
|
+
const num = Number(value)
|
|
19
|
+
if (!Number.isNaN(num)) return num
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return value
|
|
23
|
+
}
|
|
24
|
+
export function smartParse<T>(schema: z.ZodType<T>, input: unknown): T {
|
|
25
|
+
return schema.parse(autoNumber(input))
|
|
26
|
+
}
|
|
27
|
+
export const autoTypeMiddleware = (
|
|
28
|
+
req: Request,
|
|
29
|
+
res: Response,
|
|
30
|
+
next: NextFunction,
|
|
31
|
+
) => {
|
|
32
|
+
req.body = autoNumber(req.body)
|
|
33
|
+
req.query = autoNumber(req.query)
|
|
34
|
+
next()
|
|
35
|
+
}
|
|
@@ -21,7 +21,7 @@ export class GGApi_v3 {
|
|
|
21
21
|
): Promise<${tempInterfaceName}["get"][T]["responseStructure"]> {
|
|
22
22
|
return new Promise((resolve, reject) => {
|
|
23
23
|
axios
|
|
24
|
-
.get(this.urlPrefix + url as string, { params: requireParams })
|
|
24
|
+
.get(this.urlPrefix + url as string, { params: { data : requireParams} })
|
|
25
25
|
.then((response) => {
|
|
26
26
|
resolve(response.data)
|
|
27
27
|
})
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
+
|
|
1
2
|
import axios from "axios"
|
|
2
3
|
import { staticRouteInterface_hotel_v3 } from "./staticRouteInterface_hotel_v3"
|
|
3
4
|
|
|
4
5
|
export class GGApi_v3 {
|
|
5
|
-
urlPrefix: string
|
|
6
|
-
constructor(input:
|
|
6
|
+
urlPrefix : string
|
|
7
|
+
constructor(input:{ urlPrefix : string }) {
|
|
7
8
|
this.urlPrefix = input.urlPrefix
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
get<T extends keyof staticRouteInterface_hotel_v3["get"]>(
|
|
11
12
|
url: T,
|
|
12
|
-
requireParams: staticRouteInterface_hotel_v3["get"][T]["requireParams"]
|
|
13
|
+
requireParams: staticRouteInterface_hotel_v3["get"][T]["requireParams"]
|
|
13
14
|
): Promise<staticRouteInterface_hotel_v3["get"][T]["responseStructure"]> {
|
|
14
15
|
return new Promise((resolve, reject) => {
|
|
15
16
|
axios
|
|
16
|
-
.get(
|
|
17
|
+
.get(this.urlPrefix + url as string, { params: { data : requireParams} })
|
|
17
18
|
.then((response) => {
|
|
18
19
|
resolve(response.data)
|
|
19
20
|
})
|
|
@@ -26,11 +27,11 @@ export class GGApi_v3 {
|
|
|
26
27
|
|
|
27
28
|
post<T extends keyof staticRouteInterface_hotel_v3["post"]>(
|
|
28
29
|
url: T,
|
|
29
|
-
requireParams: staticRouteInterface_hotel_v3["post"][T]["requireParams"]
|
|
30
|
+
requireParams: staticRouteInterface_hotel_v3["post"][T]["requireParams"]
|
|
30
31
|
): Promise<staticRouteInterface_hotel_v3["post"][T]["responseStructure"]> {
|
|
31
32
|
return new Promise((resolve, reject) => {
|
|
32
33
|
axios
|
|
33
|
-
.post(
|
|
34
|
+
.post(this.urlPrefix + url as string, { data: requireParams })
|
|
34
35
|
.then((response) => {
|
|
35
36
|
resolve(response.data)
|
|
36
37
|
})
|
|
@@ -42,11 +43,11 @@ export class GGApi_v3 {
|
|
|
42
43
|
}
|
|
43
44
|
put<T extends keyof staticRouteInterface_hotel_v3["put"]>(
|
|
44
45
|
url: T,
|
|
45
|
-
requireParams: staticRouteInterface_hotel_v3["put"][T]["requireParams"]
|
|
46
|
+
requireParams: staticRouteInterface_hotel_v3["put"][T]["requireParams"]
|
|
46
47
|
): Promise<staticRouteInterface_hotel_v3["put"][T]["responseStructure"]> {
|
|
47
48
|
return new Promise((resolve, reject) => {
|
|
48
49
|
axios
|
|
49
|
-
.put(
|
|
50
|
+
.put(this.urlPrefix + url as string, { data: requireParams })
|
|
50
51
|
.then((response) => {
|
|
51
52
|
resolve(response.data)
|
|
52
53
|
})
|
|
@@ -58,13 +59,11 @@ export class GGApi_v3 {
|
|
|
58
59
|
}
|
|
59
60
|
delete<T extends keyof staticRouteInterface_hotel_v3["delete"]>(
|
|
60
61
|
url: T,
|
|
61
|
-
requireParams: staticRouteInterface_hotel_v3["delete"][T]["requireParams"]
|
|
62
|
+
requireParams: staticRouteInterface_hotel_v3["delete"][T]["requireParams"]
|
|
62
63
|
): Promise<staticRouteInterface_hotel_v3["delete"][T]["responseStructure"]> {
|
|
63
64
|
return new Promise((resolve, reject) => {
|
|
64
65
|
axios
|
|
65
|
-
.delete(
|
|
66
|
-
data: { data: requireParams },
|
|
67
|
-
})
|
|
66
|
+
.delete(this.urlPrefix + url as string, { data: { data: requireParams } })
|
|
68
67
|
.then((response) => {
|
|
69
68
|
resolve(response.data)
|
|
70
69
|
})
|
|
@@ -1,38 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
1
|
+
|
|
2
|
+
export interface staticRouteInterface_hotel_v3 {
|
|
3
|
+
get : {
|
|
4
|
+
"/api/v2/hotel/users/id" : {
|
|
5
|
+
requireParams : { data :{
|
|
6
|
+
name: string;
|
|
7
|
+
age: number;
|
|
8
|
+
} },responseStructure : {
|
|
9
|
+
data : {
|
|
10
|
+
value: string;
|
|
11
|
+
address: string;
|
|
12
|
+
},
|
|
13
|
+
message: string
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
} },
|
|
17
|
+
post : {
|
|
18
|
+
"/api/v2/hotel/item" : {
|
|
19
|
+
requireParams : { data :{
|
|
20
|
+
name: string;
|
|
21
|
+
price?: number | undefined;
|
|
22
|
+
} },responseStructure : {
|
|
23
|
+
data : {
|
|
24
|
+
value: number;
|
|
25
|
+
address: string;
|
|
26
|
+
},
|
|
27
|
+
message: string
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
} },
|
|
31
|
+
put : { },
|
|
32
|
+
delete : { },
|
|
18
33
|
}
|
|
19
|
-
|
|
20
|
-
"/api/v2/hotel/item": {
|
|
21
|
-
requireParams: {
|
|
22
|
-
data: {
|
|
23
|
-
name: string
|
|
24
|
-
price?: number | undefined
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
responseStructure: {
|
|
28
|
-
data: {
|
|
29
|
-
value: number
|
|
30
|
-
address: string
|
|
31
|
-
}
|
|
32
|
-
message: string
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
put: {}
|
|
37
|
-
delete: {}
|
|
38
|
-
}
|
|
34
|
+
|
package/src/v3/run_v3.test.ts
CHANGED
|
@@ -33,6 +33,7 @@ export const addMyTestMW = (req: any, res: any, next: NextFunction) => {
|
|
|
33
33
|
|
|
34
34
|
function run() {
|
|
35
35
|
const app = express()
|
|
36
|
+
app.use(express.json())
|
|
36
37
|
const ggapp = new GGExpressV3<"hotel", "/api/v2">(app, "hotel", ["./output"])
|
|
37
38
|
ggapp.get(
|
|
38
39
|
"/api/v2/hotel/users/id",
|
|
@@ -58,7 +59,6 @@ function run() {
|
|
|
58
59
|
message: "",
|
|
59
60
|
})
|
|
60
61
|
},
|
|
61
|
-
() => {},
|
|
62
62
|
)
|
|
63
63
|
ggapp.post(
|
|
64
64
|
"/api/v2/hotel/item",
|
|
@@ -87,18 +87,38 @@ function run() {
|
|
|
87
87
|
})
|
|
88
88
|
},
|
|
89
89
|
)
|
|
90
|
-
|
|
91
|
-
api
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
90
|
+
|
|
91
|
+
// api
|
|
92
|
+
// .get("/api/v2/hotel/users/id", {
|
|
93
|
+
// data: {
|
|
94
|
+
// name: "tin",
|
|
95
|
+
// age: 30,
|
|
96
|
+
// },
|
|
97
|
+
// })
|
|
98
|
+
// .catch((error) => {})
|
|
97
99
|
|
|
98
100
|
app.listen(3002, async () => {
|
|
99
101
|
await ggapp.generateAPIFiles()
|
|
100
102
|
console.log("done")
|
|
101
103
|
// process.exit(0)
|
|
104
|
+
|
|
105
|
+
const api = new GGApi_v3({ urlPrefix: "http://127.0.0.1:3002" })
|
|
106
|
+
// api
|
|
107
|
+
// .post("/api/v2/hotel/item", {
|
|
108
|
+
// data: {
|
|
109
|
+
// name: "tin",
|
|
110
|
+
// price: 20,
|
|
111
|
+
// },
|
|
112
|
+
// })
|
|
113
|
+
// .catch((error) => {})
|
|
114
|
+
api
|
|
115
|
+
.get("/api/v2/hotel/users/id", {
|
|
116
|
+
data: {
|
|
117
|
+
name: "tin",
|
|
118
|
+
age: 30,
|
|
119
|
+
},
|
|
120
|
+
})
|
|
121
|
+
.catch((error) => {})
|
|
102
122
|
})
|
|
103
123
|
}
|
|
104
124
|
run()
|