gg-express 1.0.142 → 1.0.144

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.
@@ -3,12 +3,12 @@ import Express, { Request, Response } from "express-serve-static-core";
3
3
  import z from "zod";
4
4
  export type Method = "get" | "post" | "put" | "delete";
5
5
  type ReqStructure<RQ extends z.ZodTypeAny> = {
6
- data: {
7
- data: z.infer<RQ>;
8
- };
6
+ data: z.infer<RQ>;
9
7
  };
10
8
  type MyQueryRequest<RQ extends z.ZodTypeAny> = Request<{}, {}, {}, ReqStructure<RQ>, {}>;
11
- type MyBodyRequest<RQ extends z.ZodTypeAny> = Request<{}, {}, ReqStructure<RQ>, {}, {}>;
9
+ type MyBodyRequest<RQ extends z.ZodTypeAny> = Request<{}, {}, {
10
+ data: ReqStructure<RQ>;
11
+ }, {}, {}>;
12
12
  export type RequestStructure<RQ extends z.ZodTypeAny> = {
13
13
  data: RQ;
14
14
  };
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const fs_1 = __importDefault(require("fs"));
16
16
  const path_1 = __importDefault(require("path"));
17
+ const zod_1 = __importDefault(require("zod"));
17
18
  const generateStaticRouteFileV3_1 = require("./generateStaticRouteFileV3");
18
19
  const generateGGApi_v3_1 = require("./generateGGApi_v3");
19
20
  const autoNumber_1 = require("./autoNumber");
@@ -48,11 +49,18 @@ class GGExpressV3 {
48
49
  return this.express[method](url,
49
50
  // (req: Request, res: Response, next: NextFunction) => next(),
50
51
  autoNumber_1.autoTypeMiddleware, (req, res, next) => {
51
- if (method === "get")
52
- //@ts-ignore
53
- options.requireParams.data.parse(req.query.data.data);
54
- else
55
- options.requireParams.data.parse(req.body.data.data);
52
+ // console.log("req.url", req.url)
53
+ // if (req.query) console.log("req.query", req.query)
54
+ // if (req.body) console.log("req.body", req.body)
55
+ if (method === "get" &&
56
+ isEmptyZodObject(options.requireParams.data) === false) {
57
+ options.requireParams.data.parse(autoCastNumbers(req.query.data));
58
+ }
59
+ else if (method === "post" ||
60
+ method === "put" ||
61
+ method === "delete") {
62
+ options.requireParams.data.parse(autoCastNumbers(req.body.data.data));
63
+ }
56
64
  next();
57
65
  },
58
66
  // แต่ middleware ของคุณจะถูก wrap ทีละตัว
@@ -88,3 +96,19 @@ class GGExpressV3 {
88
96
  }
89
97
  }
90
98
  exports.default = GGExpressV3;
99
+ function isEmptyZodObject(schema) {
100
+ return schema instanceof zod_1.default.ZodObject && Object.keys(schema.shape).length === 0;
101
+ }
102
+ function autoCastNumbers(obj) {
103
+ if (Array.isArray(obj)) {
104
+ return obj.map(autoCastNumbers);
105
+ }
106
+ if (obj !== null && typeof obj === "object") {
107
+ return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, autoCastNumbers(v)]));
108
+ }
109
+ // auto convert string number
110
+ if (typeof obj === "string" && obj.trim() !== "" && !isNaN(Number(obj))) {
111
+ return Number(obj);
112
+ }
113
+ return obj;
114
+ }
@@ -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: { data : requireParams} })
26
+ .get(this.urlPrefix + url as string, { params: 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(this.urlPrefix + url, { params: { data: requireParams } })
15
+ .get(this.urlPrefix + url, { params: requireParams })
16
16
  .then((response) => {
17
17
  resolve(response.data);
18
18
  })
@@ -15,13 +15,25 @@ export interface staticRouteInterface_hotel_v3 {
15
15
  message: string;
16
16
  };
17
17
  };
18
+ "/api/v2/hotel/users/empty": {
19
+ requireParams: {
20
+ data: {};
21
+ };
22
+ responseStructure: {
23
+ data: {
24
+ value: string;
25
+ address: string;
26
+ };
27
+ message: string;
28
+ };
29
+ };
18
30
  };
19
31
  post: {
20
32
  "/api/v2/hotel/item": {
21
33
  requireParams: {
22
34
  data: {
23
35
  name: string;
24
- price?: number | undefined;
36
+ price: number;
25
37
  };
26
38
  };
27
39
  responseStructure: {
@@ -48,6 +48,11 @@ exports.addMyTestMW = addMyTestMW;
48
48
  function run() {
49
49
  const app = (0, express_1.default)();
50
50
  app.use(express_1.default.json());
51
+ // app.use((req, res, next) => {
52
+ // if (req.query) console.log("req.query", req.query)
53
+ // if (req.body) console.log("req.body", req.body)
54
+ // next()
55
+ // })
51
56
  const ggapp = new GGExpressV3_1.default(app, "hotel", ["./output"]);
52
57
  ggapp.get("/api/v2/hotel/users/id", {
53
58
  requireParams: {
@@ -61,7 +66,24 @@ function run() {
61
66
  address: zod_1.default.string(),
62
67
  }),
63
68
  }, (req, res, next) => {
64
- req.query.data.data.age;
69
+ req.query.data.age;
70
+ res.json({
71
+ data: {
72
+ value: "",
73
+ address: "",
74
+ },
75
+ message: "",
76
+ });
77
+ });
78
+ ggapp.get("/api/v2/hotel/users/empty", {
79
+ requireParams: {
80
+ data: zod_1.default.object({}),
81
+ },
82
+ responseStructure: zod_1.default.object({
83
+ value: zod_1.default.string(),
84
+ address: zod_1.default.string(),
85
+ }),
86
+ }, (req, res, next) => {
65
87
  res.json({
66
88
  data: {
67
89
  value: "",
@@ -74,7 +96,7 @@ function run() {
74
96
  requireParams: {
75
97
  data: zod_1.default.object({
76
98
  name: zod_1.default.string(),
77
- price: zod_1.default.number().optional(),
99
+ price: zod_1.default.number(),
78
100
  }),
79
101
  },
80
102
  responseStructure: zod_1.default.object({
@@ -82,7 +104,6 @@ function run() {
82
104
  address: zod_1.default.string(),
83
105
  }),
84
106
  }, (req, res, next) => {
85
- // price/name come from body for POST
86
107
  req.body.data.data.price;
87
108
  req.body.data.data.name;
88
109
  res.json({
@@ -93,14 +114,6 @@ function run() {
93
114
  message: "",
94
115
  });
95
116
  });
96
- // api
97
- // .get("/api/v2/hotel/users/id", {
98
- // data: {
99
- // name: "tin",
100
- // age: 30,
101
- // },
102
- // })
103
- // .catch((error) => {})
104
117
  app.listen(3002, () => __awaiter(this, void 0, void 0, function* () {
105
118
  yield ggapp.generateAPIFiles();
106
119
  console.log("done");
@@ -113,15 +126,24 @@ function run() {
113
126
  // price: 20,
114
127
  // },
115
128
  // })
129
+ // .catch((error) => {
130
+ // console.log(error)
131
+ // })
132
+ // api
133
+ // .get("/api/v2/hotel/users/id", {
134
+ // data: {
135
+ // name: "tin",
136
+ // age: 31,
137
+ // },
138
+ // })
116
139
  // .catch((error) => {})
117
- api
118
- .get("/api/v2/hotel/users/id", {
119
- data: {
120
- name: "tin",
121
- age: 30,
122
- },
123
- })
124
- .catch((error) => { });
140
+ // api
141
+ // .get("/api/v2/hotel/users/empty", {
142
+ // data: {},
143
+ // })
144
+ // .catch((error) => {
145
+ // console.log(error)
146
+ // })
125
147
  }));
126
148
  }
127
149
  run();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-express",
3
- "version": "1.0.142",
3
+ "version": "1.0.144",
4
4
  "description": "",
5
5
  "main": "dist/main.js",
6
6
  "scripts": {
@@ -16,7 +16,7 @@ export type Method = "get" | "post" | "put" | "delete"
16
16
  const myExpressRouteList: routeListV3[] = []
17
17
 
18
18
  type ReqStructure<RQ extends z.ZodTypeAny> = {
19
- data: { data: z.infer<RQ> }
19
+ data: z.infer<RQ>
20
20
  }
21
21
  type MyQueryRequest<RQ extends z.ZodTypeAny> = Request<
22
22
  {},
@@ -28,7 +28,7 @@ type MyQueryRequest<RQ extends z.ZodTypeAny> = Request<
28
28
  type MyBodyRequest<RQ extends z.ZodTypeAny> = Request<
29
29
  {},
30
30
  {},
31
- ReqStructure<RQ>,
31
+ { data: ReqStructure<RQ> },
32
32
  {},
33
33
  {}
34
34
  >
@@ -104,11 +104,22 @@ export default class GGExpressV3<
104
104
  url,
105
105
  // (req: Request, res: Response, next: NextFunction) => next(),
106
106
  autoTypeMiddleware,
107
- (req, res, next: NextFunction) => {
108
- if (method === "get")
109
- //@ts-ignore
110
- options.requireParams.data.parse(req.query.data.data)
111
- else options.requireParams.data.parse(req.body.data.data)
107
+ (req, res, next) => {
108
+ // console.log("req.url", req.url)
109
+ // if (req.query) console.log("req.query", req.query)
110
+ // if (req.body) console.log("req.body", req.body)
111
+ if (
112
+ method === "get" &&
113
+ isEmptyZodObject(options.requireParams.data) === false
114
+ ) {
115
+ options.requireParams.data.parse(autoCastNumbers(req.query.data))
116
+ } else if (
117
+ method === "post" ||
118
+ method === "put" ||
119
+ method === "delete"
120
+ ) {
121
+ options.requireParams.data.parse(autoCastNumbers(req.body.data.data))
122
+ }
112
123
  next()
113
124
  },
114
125
 
@@ -238,3 +249,27 @@ export default class GGExpressV3<
238
249
  }
239
250
  }
240
251
  }
252
+ function isEmptyZodObject(
253
+ schema: z.ZodTypeAny,
254
+ ): schema is z.ZodObject<Record<string, never>> {
255
+ return schema instanceof z.ZodObject && Object.keys(schema.shape).length === 0
256
+ }
257
+
258
+ function autoCastNumbers(obj: unknown): unknown {
259
+ if (Array.isArray(obj)) {
260
+ return obj.map(autoCastNumbers)
261
+ }
262
+
263
+ if (obj !== null && typeof obj === "object") {
264
+ return Object.fromEntries(
265
+ Object.entries(obj).map(([k, v]) => [k, autoCastNumbers(v)]),
266
+ )
267
+ }
268
+
269
+ // auto convert string number
270
+ if (typeof obj === "string" && obj.trim() !== "" && !isNaN(Number(obj))) {
271
+ return Number(obj)
272
+ }
273
+
274
+ return obj
275
+ }
@@ -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: { data : requireParams} })
24
+ .get(this.urlPrefix + url as string, { params: requireParams })
25
25
  .then((response) => {
26
26
  resolve(response.data)
27
27
  })
@@ -14,7 +14,7 @@ export class GGApi_v3 {
14
14
  ): Promise<staticRouteInterface_hotel_v3["get"][T]["responseStructure"]> {
15
15
  return new Promise((resolve, reject) => {
16
16
  axios
17
- .get(this.urlPrefix + url as string, { params: { data : requireParams} })
17
+ .get(this.urlPrefix + url as string, { params: requireParams })
18
18
  .then((response) => {
19
19
  resolve(response.data)
20
20
  })
@@ -13,12 +13,22 @@
13
13
  message: string
14
14
  },
15
15
 
16
+ },
17
+ "/api/v2/hotel/users/empty" : {
18
+ requireParams : { data :{} },responseStructure : {
19
+ data : {
20
+ value: string;
21
+ address: string;
22
+ },
23
+ message: string
24
+ },
25
+
16
26
  } },
17
27
  post : {
18
28
  "/api/v2/hotel/item" : {
19
29
  requireParams : { data :{
20
30
  name: string;
21
- price?: number | undefined;
31
+ price: number;
22
32
  } },responseStructure : {
23
33
  data : {
24
34
  value: number;
@@ -34,6 +34,11 @@ export const addMyTestMW = (req: any, res: any, next: NextFunction) => {
34
34
  function run() {
35
35
  const app = express()
36
36
  app.use(express.json())
37
+ // app.use((req, res, next) => {
38
+ // if (req.query) console.log("req.query", req.query)
39
+ // if (req.body) console.log("req.body", req.body)
40
+ // next()
41
+ // })
37
42
  const ggapp = new GGExpressV3<"hotel", "/api/v2">(app, "hotel", ["./output"])
38
43
  ggapp.get(
39
44
  "/api/v2/hotel/users/id",
@@ -50,7 +55,28 @@ function run() {
50
55
  }),
51
56
  },
52
57
  (req, res, next) => {
53
- req.query.data.data.age
58
+ req.query.data.age
59
+ res.json({
60
+ data: {
61
+ value: "",
62
+ address: "",
63
+ },
64
+ message: "",
65
+ })
66
+ },
67
+ )
68
+ ggapp.get(
69
+ "/api/v2/hotel/users/empty",
70
+ {
71
+ requireParams: {
72
+ data: z.object({}),
73
+ },
74
+ responseStructure: z.object({
75
+ value: z.string(),
76
+ address: z.string(),
77
+ }),
78
+ },
79
+ (req, res, next) => {
54
80
  res.json({
55
81
  data: {
56
82
  value: "",
@@ -66,7 +92,7 @@ function run() {
66
92
  requireParams: {
67
93
  data: z.object({
68
94
  name: z.string(),
69
- price: z.number().optional(),
95
+ price: z.number(),
70
96
  }),
71
97
  },
72
98
  responseStructure: z.object({
@@ -75,7 +101,6 @@ function run() {
75
101
  }),
76
102
  },
77
103
  (req, res, next) => {
78
- // price/name come from body for POST
79
104
  req.body.data.data.price
80
105
  req.body.data.data.name
81
106
  res.json({
@@ -88,15 +113,6 @@ function run() {
88
113
  },
89
114
  )
90
115
 
91
- // api
92
- // .get("/api/v2/hotel/users/id", {
93
- // data: {
94
- // name: "tin",
95
- // age: 30,
96
- // },
97
- // })
98
- // .catch((error) => {})
99
-
100
116
  app.listen(3002, async () => {
101
117
  await ggapp.generateAPIFiles()
102
118
  console.log("done")
@@ -110,15 +126,24 @@ function run() {
110
126
  // price: 20,
111
127
  // },
112
128
  // })
129
+ // .catch((error) => {
130
+ // console.log(error)
131
+ // })
132
+ // api
133
+ // .get("/api/v2/hotel/users/id", {
134
+ // data: {
135
+ // name: "tin",
136
+ // age: 31,
137
+ // },
138
+ // })
113
139
  // .catch((error) => {})
114
- api
115
- .get("/api/v2/hotel/users/id", {
116
- data: {
117
- name: "tin",
118
- age: 30,
119
- },
120
- })
121
- .catch((error) => {})
140
+ // api
141
+ // .get("/api/v2/hotel/users/empty", {
142
+ // data: {},
143
+ // })
144
+ // .catch((error) => {
145
+ // console.log(error)
146
+ // })
122
147
  })
123
148
  }
124
149
  run()