gg-express 1.0.97 → 1.0.98

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.
@@ -5,11 +5,16 @@ export type Method = "get" | "post" | "put" | "delete";
5
5
  type MyRequest<M extends Method, RQ extends ConstSchemaType> = M extends "get" ? TypeResolve<MyRequestQuery<RQ>> : TypeResolve<MyRequestBody<RQ>>;
6
6
  type MyRequestQuery<T extends ConstSchemaType> = Request<{}, {}, {}, T, {}>;
7
7
  type MyRequestBody<T extends ConstSchemaType> = Request<{}, {}, T, {}, {}>;
8
- type MyResponse<T extends ConstSchemaType> = Response<{
9
- status: "SUCCESS" | "ERROR" | "SERVER_ERROR";
8
+ type SuccessResponse<T extends ConstSchemaType> = {
9
+ status: "SUCCESS";
10
10
  message: string;
11
11
  data: TypeResolve<T>;
12
- }>;
12
+ };
13
+ type ErrorResponse = {
14
+ status: "ERROR" | "SERVER_ERROR";
15
+ message: string;
16
+ };
17
+ type MyResponse<T extends ConstSchemaType> = Response<SuccessResponse<T> | ErrorResponse>;
13
18
  export default class GGExpressV2<appName extends string> {
14
19
  express: Express.Express;
15
20
  private outputPath;
@@ -43,7 +43,7 @@ class GGExpressV2 {
43
43
  // Express handler ต้องกว้าง
44
44
  (req, res, next) => next(),
45
45
  // แต่ middleware ของคุณจะถูก wrap ทีละตัว
46
- ...middlewares.map((mw) => ((req, res, next) => mw(req, res, next))));
46
+ ...middlewares.map((mw) => (req, res, next) => mw(req, res, next)));
47
47
  }
48
48
  get(url, options, ...middlewares) {
49
49
  return this.rootMethod("get", url, options, ...middlewares);
@@ -142,6 +142,23 @@ function run() {
142
142
  },
143
143
  });
144
144
  });
145
+ ggapp.post("/api/hotel/booking/id", {
146
+ requireParams: {
147
+ id: "number",
148
+ },
149
+ responseStructure: {
150
+ bookingData: {
151
+ id: "number",
152
+ roomNumber: "string",
153
+ },
154
+ },
155
+ }, (req, res, next) => {
156
+ req.body.id;
157
+ return res.json({
158
+ status: "ERROR",
159
+ message: "",
160
+ });
161
+ });
145
162
  app.listen(3002, () => __awaiter(this, void 0, void 0, function* () {
146
163
  yield ggapp.generateAPIFiles();
147
164
  console.log("done");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-express",
3
- "version": "1.0.97",
3
+ "version": "1.0.98",
4
4
  "description": "",
5
5
  "main": "dist/main.js",
6
6
  "scripts": {
@@ -6,7 +6,6 @@ import { generateStaticRouteFileV2 } from "./generateStaticRouteFileV2"
6
6
  import path from "path"
7
7
  import { generateGGApi_v2 } from "./generateGGApi_v2"
8
8
 
9
- type Unarray<T> = T extends (infer U)[] ? U : T
10
9
  export type Method = "get" | "post" | "put" | "delete"
11
10
 
12
11
  type MyRequest<M extends Method, RQ extends ConstSchemaType> = M extends "get"
@@ -18,12 +17,22 @@ type MyRequest<M extends Method, RQ extends ConstSchemaType> = M extends "get"
18
17
  type MyRequestQuery<T extends ConstSchemaType> = Request<{}, {}, {}, T, {}>
19
18
  type MyRequestBody<T extends ConstSchemaType> = Request<{}, {}, T, {}, {}>
20
19
 
21
- type MyResponse<T extends ConstSchemaType> = Response<{
22
- status: "SUCCESS" | "ERROR" | "SERVER_ERROR"
20
+ type SuccessResponse<T extends ConstSchemaType> = {
21
+ status: "SUCCESS"
23
22
  message: string
24
23
  data: TypeResolve<T>
25
- }>
24
+ }
26
25
 
26
+ type ErrorResponse = {
27
+ status: "ERROR" | "SERVER_ERROR"
28
+ message: string
29
+ }
30
+ type MyResponse<T extends ConstSchemaType> = Response<
31
+ SuccessResponse<T> | ErrorResponse
32
+ >
33
+ type GGHandlerReturn<RS extends ConstSchemaType> = Response<
34
+ MyResponse<RS>
35
+ > | void // สำหรับ next()
27
36
  const myExpressRouteList: {
28
37
  method: "get" | "post" | "put" | "delete"
29
38
  url: string
@@ -84,13 +93,8 @@ export default class GGExpressV2<appName extends string> {
84
93
 
85
94
  // แต่ middleware ของคุณจะถูก wrap ทีละตัว
86
95
  ...middlewares.map(
87
- (mw) =>
88
- ((req: Request, res: Response, next: NextFunction) =>
89
- mw(
90
- req as unknown as MyRequest<M, RQ>,
91
- res as MyResponse<RS>,
92
- next
93
- )) as Express.RequestHandler
96
+ (mw) => (req: Request, res: Response, next: NextFunction) =>
97
+ mw(req as unknown as MyRequest<M, RQ>, res as MyResponse<RS>, next)
94
98
  )
95
99
  )
96
100
  }
@@ -139,6 +139,28 @@ function run() {
139
139
  }
140
140
  )
141
141
 
142
+ ggapp.post(
143
+ "/api/hotel/booking/id",
144
+ {
145
+ requireParams: {
146
+ id: "number",
147
+ },
148
+ responseStructure: {
149
+ bookingData: {
150
+ id: "number",
151
+ roomNumber: "string",
152
+ },
153
+ },
154
+ },
155
+ (req, res, next) => {
156
+ req.body.id
157
+ return res.json({
158
+ status: "ERROR",
159
+ message: "",
160
+ })
161
+ }
162
+ )
163
+
142
164
  app.listen(3002, async () => {
143
165
  await ggapp.generateAPIFiles()
144
166
  console.log("done")
@@ -10,6 +10,7 @@ type BaseType =
10
10
 
11
11
  type CustomType = BaseType | `${BaseType}?` | `${BaseType}~` | `${BaseType}?~`
12
12
  type InputEnum = readonly string[] | readonly number[]
13
+
13
14
  export type ConstSchemaType = {
14
15
  [key in string]: CustomType | ConstSchemaType | ConstSchemaType[] | InputEnum
15
16
  }