gg-express 1.0.96 → 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");
@@ -1,12 +1,13 @@
1
1
  import { TypeResolve } from "./typeResolver";
2
2
  export declare const ReturnOfCalculateEachDateConst: {
3
- readonly itemDetailData: [{
3
+ readonly itemDetailData: {
4
4
  readonly itemID: "number";
5
5
  readonly zoneID: "number";
6
- }];
6
+ }[];
7
7
  readonly itemDetailData_v2: [{
8
8
  readonly id: "number";
9
9
  }];
10
+ readonly bool: "boolean";
10
11
  readonly requisitionQuantity: "number";
11
12
  readonly usedQuantity: "number";
12
13
  readonly date: "string";
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ReturnOfCalculateEachDateConst = void 0;
4
+ const typeResolver_1 = require("./typeResolver");
4
5
  const itemDetailDataConst = {
5
6
  itemID: "number",
6
7
  zoneID: "number",
@@ -9,14 +10,13 @@ const QuantityTypeConst = {
9
10
  requisitionQuantity: "number",
10
11
  usedQuantity: "number",
11
12
  };
12
- exports.ReturnOfCalculateEachDateConst = Object.assign(Object.assign({ date: "string", itemID: "number" }, QuantityTypeConst), { itemDetailData: [itemDetailDataConst], itemDetailData_v2: [{ id: "number" }] });
13
+ exports.ReturnOfCalculateEachDateConst = Object.assign(Object.assign({ date: "string", itemID: "number" }, QuantityTypeConst), { itemDetailData: (0, typeResolver_1.toArray)(itemDetailDataConst), itemDetailData_v2: [{ id: "number" }], bool: "boolean" });
13
14
  const temp = {
14
- itemDetailData: [
15
- {
16
- itemID: 0,
17
- zoneID: 0,
18
- },
19
- ],
15
+ bool: true,
16
+ itemDetailData: (0, typeResolver_1.toArray)({
17
+ itemID: 0,
18
+ zoneID: 0,
19
+ }),
20
20
  itemDetailData_v2: [
21
21
  {
22
22
  id: 1,
@@ -1,12 +1,12 @@
1
1
  type Unarray<T> = T extends (infer U)[] ? U : T;
2
- type BaseType = "string" | "number" | "string[]" | "number[]";
2
+ type BaseType = "string" | "number" | "boolean" | "string[]" | "number[]" | "boolean[]";
3
3
  type CustomType = BaseType | `${BaseType}?` | `${BaseType}~` | `${BaseType}?~`;
4
4
  type InputEnum = readonly string[] | readonly number[];
5
5
  export type ConstSchemaType = {
6
6
  [key in string]: CustomType | ConstSchemaType | ConstSchemaType[] | InputEnum;
7
7
  };
8
- type ResolveBase<T extends BaseType> = T extends "string" ? string : T extends "string[]" ? string[] : T extends "number" ? number : T extends "number[]" ? number[] : never;
9
- export declare const toArray: <T extends ConstSchemaType>(input: T) => T[];
8
+ type ResolveBase<T extends BaseType> = T extends "string" ? string : T extends "string[]" ? string[] : T extends "number" ? number : T extends "number[]" ? number[] : T extends "boolean" ? boolean : T extends "boolean[]" ? boolean[] : never;
9
+ export declare const toArray: <T>(input: T) => T[];
10
10
  type ResolveExtraType<T> = T extends `${infer B}?~` ? ResolveBase<B & BaseType> | null | undefined : T extends `${infer B}?` ? ResolveBase<B & BaseType> | undefined : T extends `${infer B}~` ? ResolveBase<B & BaseType> | null : ResolveBase<T & BaseType>;
11
11
  type chooseType<T> = T extends InputEnum ? T[number] : T extends any[] ? chooseType<Unarray<T>>[] : T extends object ? TypeResolve<T> : ResolveExtraType<T>;
12
12
  export type TypeResolve<T> = {
@@ -5,54 +5,3 @@ const toArray = (input) => {
5
5
  return [input];
6
6
  };
7
7
  exports.toArray = toArray;
8
- // let req2 = {
9
- // booking: {
10
- // id: "number",
11
- // bookingGroupID: "number",
12
- // checkInDate: "string",
13
- // checkOutDate: "string",
14
- // checkInTime: "string",
15
- // checkOutTime: "string",
16
- // roomRate: "number",
17
- // extraBedRate: "number",
18
- // roomNumber: "string",
19
- // editTime: "string",
20
- // createTime: "string",
21
- // shiftDate: "string",
22
- // shiftName: "string",
23
- // userName: "string",
24
- // bookingStatus: ["ACTIVE", "CLOSE", "CANCEL", "NO_SHOW"],
25
- // is_specificRoom: "number",
26
- // guestCount: "number",
27
- // reasonToCancel: "string",
28
- // note: "string",
29
- // },
30
- // status: ["ACTIVE", "DISABLE"],
31
- // rooms: toArray({
32
- // id: "number",
33
- // status: ["ON", "OFF"],
34
- // roomNumber: "string",
35
- // log: toArray({
36
- // time: "string",
37
- // logger: [{ info: "string" }],
38
- // }),
39
- // }),
40
- // data: {
41
- // level_1: {
42
- // title: "string",
43
- // level_2: {
44
- // id: "number",
45
- // name: "string",
46
- // },
47
- // },
48
- // },
49
- // } as const satisfies InputParent
50
- // const data = {} as convertRoot<typeof req2>
51
- // data.booking.bookingStatus
52
- // data.status
53
- // const id = data.booking.id
54
- // const roomNumber = data.booking.bookingStatus === data.booking.bookingStatus
55
- // data.rooms.map((row) => row.status === "OFF")
56
- // data.data.level_1.level_2.id
57
- // data.booking.createTime
58
- // req2.data.level_1.level_2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-express",
3
- "version": "1.0.96",
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
  }
@@ -1,5 +1,6 @@
1
1
  import express from "express"
2
2
  import GGExpressV2 from "./GGExpressV2"
3
+ import { toArray } from "./typeResolver"
3
4
 
4
5
  export const hotel_model_const_temp = {
5
6
  booking: {
@@ -138,6 +139,28 @@ function run() {
138
139
  }
139
140
  )
140
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
+
141
164
  app.listen(3002, async () => {
142
165
  await ggapp.generateAPIFiles()
143
166
  console.log("done")
@@ -1,4 +1,4 @@
1
- import { TypeResolve, ConstSchemaType } from "./typeResolver"
1
+ import { TypeResolve, ConstSchemaType, toArray } from "./typeResolver"
2
2
 
3
3
  const itemDetailDataConst = {
4
4
  itemID: "number",
@@ -13,8 +13,9 @@ export const ReturnOfCalculateEachDateConst = {
13
13
  date: "string",
14
14
  itemID: "number",
15
15
  ...QuantityTypeConst,
16
- itemDetailData: [itemDetailDataConst],
16
+ itemDetailData: toArray(itemDetailDataConst),
17
17
  itemDetailData_v2: [{ id: "number" }],
18
+ bool: "boolean",
18
19
  } as const satisfies ConstSchemaType
19
20
 
20
21
  export type ReturnOfCalculateEachDate = TypeResolve<
@@ -22,12 +23,11 @@ export type ReturnOfCalculateEachDate = TypeResolve<
22
23
  >
23
24
 
24
25
  const temp: ReturnOfCalculateEachDate = {
25
- itemDetailData: [
26
- {
27
- itemID: 0,
28
- zoneID: 0,
29
- },
30
- ],
26
+ bool: true,
27
+ itemDetailData: toArray({
28
+ itemID: 0,
29
+ zoneID: 0,
30
+ }),
31
31
  itemDetailData_v2: [
32
32
  {
33
33
  id: 1,
@@ -1,9 +1,16 @@
1
1
  type Unarray<T> = T extends (infer U)[] ? U : T
2
2
 
3
- type BaseType = "string" | "number" | "string[]" | "number[]"
4
- type CustomType = BaseType | `${BaseType}?` | `${BaseType}~` | `${BaseType}?~`
3
+ type BaseType =
4
+ | "string"
5
+ | "number"
6
+ | "boolean"
7
+ | "string[]"
8
+ | "number[]"
9
+ | "boolean[]"
5
10
 
11
+ type CustomType = BaseType | `${BaseType}?` | `${BaseType}~` | `${BaseType}?~`
6
12
  type InputEnum = readonly string[] | readonly number[]
13
+
7
14
  export type ConstSchemaType = {
8
15
  [key in string]: CustomType | ConstSchemaType | ConstSchemaType[] | InputEnum
9
16
  }
@@ -15,10 +22,14 @@ type ResolveBase<T extends BaseType> = T extends "string"
15
22
  ? number
16
23
  : T extends "number[]"
17
24
  ? number[]
25
+ : T extends "boolean"
26
+ ? boolean
27
+ : T extends "boolean[]"
28
+ ? boolean[]
18
29
  : never
19
30
 
20
- export const toArray = <T extends ConstSchemaType>(input: T) => {
21
- return [input]
31
+ export const toArray = <T>(input: T): T[] => {
32
+ return [input] as T[]
22
33
  }
23
34
 
24
35
  type ResolveExtraType<T> =
@@ -34,8 +45,6 @@ type ResolveExtraType<T> =
34
45
  : // plain primitive
35
46
  ResolveBase<T & BaseType>
36
47
 
37
- // type checkEnum<T> = T extends InputEnum ? T[number] : ResolveExtraType<T>
38
-
39
48
  type chooseType<T> = T extends InputEnum
40
49
  ? T[number]
41
50
  : T extends any[]
@@ -43,62 +52,7 @@ type chooseType<T> = T extends InputEnum
43
52
  : T extends object
44
53
  ? TypeResolve<T>
45
54
  : ResolveExtraType<T>
46
- // : checkEnum<T>
47
55
 
48
56
  export type TypeResolve<T> = {
49
57
  [K in keyof T]: chooseType<T[K]>
50
58
  }
51
-
52
- // let req2 = {
53
- // booking: {
54
- // id: "number",
55
- // bookingGroupID: "number",
56
- // checkInDate: "string",
57
- // checkOutDate: "string",
58
- // checkInTime: "string",
59
- // checkOutTime: "string",
60
- // roomRate: "number",
61
- // extraBedRate: "number",
62
- // roomNumber: "string",
63
- // editTime: "string",
64
- // createTime: "string",
65
- // shiftDate: "string",
66
- // shiftName: "string",
67
- // userName: "string",
68
- // bookingStatus: ["ACTIVE", "CLOSE", "CANCEL", "NO_SHOW"],
69
- // is_specificRoom: "number",
70
- // guestCount: "number",
71
- // reasonToCancel: "string",
72
- // note: "string",
73
- // },
74
- // status: ["ACTIVE", "DISABLE"],
75
- // rooms: toArray({
76
- // id: "number",
77
- // status: ["ON", "OFF"],
78
- // roomNumber: "string",
79
- // log: toArray({
80
- // time: "string",
81
- // logger: [{ info: "string" }],
82
- // }),
83
- // }),
84
-
85
- // data: {
86
- // level_1: {
87
- // title: "string",
88
- // level_2: {
89
- // id: "number",
90
- // name: "string",
91
- // },
92
- // },
93
- // },
94
- // } as const satisfies InputParent
95
- // const data = {} as convertRoot<typeof req2>
96
-
97
- // data.booking.bookingStatus
98
- // data.status
99
- // const id = data.booking.id
100
- // const roomNumber = data.booking.bookingStatus === data.booking.bookingStatus
101
- // data.rooms.map((row) => row.status === "OFF")
102
- // data.data.level_1.level_2.id
103
- // data.booking.createTime
104
- // req2.data.level_1.level_2