gg-express 1.0.99 → 1.0.101
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/v2/GGExpressV2.d.ts +22 -16
- package/dist/v2/GGExpressV2.js +3 -1
- package/dist/v2/generateStaticRouteFileV2.d.ts +5 -3
- package/dist/v2/generateStaticRouteFileV2.js +15 -10
- package/dist/v2/output/staticRouteInterface_hotel_v2.d.ts +18 -2
- package/dist/v2/run_v2.test.js +14 -10
- package/dist/v2/typeResolver.js +3 -0
- package/package.json +1 -1
- package/src/v2/GGExpressV2.ts +97 -34
- package/src/v2/generateStaticRouteFileV2.ts +29 -19
- package/src/v2/output/staticRouteInterface_hotel_v2.ts +13 -2
- package/src/v2/run_v2.test.ts +15 -11
- package/src/v2/typeResolver.ts +4 -0
package/dist/v2/GGExpressV2.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { NextFunction
|
|
1
|
+
import { NextFunction } from "express";
|
|
2
|
+
import { Request, Response } from "express-serve-static-core";
|
|
2
3
|
import Express from "express-serve-static-core";
|
|
3
4
|
import { TypeResolve, ConstSchemaType } from "./typeResolver";
|
|
4
5
|
export type Method = "get" | "post" | "put" | "delete";
|
|
5
|
-
type MyRequest<M extends Method, RQ extends ConstSchemaType> = M extends "get" ?
|
|
6
|
-
type MyRequestQuery<T extends ConstSchemaType> = Request<{}, {}, {}, T, {}>;
|
|
7
|
-
type MyRequestBody<T extends ConstSchemaType> = Request<{}, {}, T, {}, {}>;
|
|
6
|
+
type MyRequest<M extends Method, RQ extends ConstSchemaType, P extends boolean = false> = M extends "get" ? MyRequestQuery<RQ, P> : MyRequestBody<RQ, P>;
|
|
7
|
+
type MyRequestQuery<T extends ConstSchemaType, P extends boolean = false> = P extends true ? Request<{}, {}, {}, Partial<TypeResolve<T>>, {}> : Request<{}, {}, {}, TypeResolve<T>, {}>;
|
|
8
|
+
type MyRequestBody<T extends ConstSchemaType, P extends boolean = false> = P extends true ? Request<{}, {}, Partial<TypeResolve<T>>, {}, {}> : Request<{}, {}, TypeResolve<T>, {}, {}>;
|
|
8
9
|
type SuccessResponse<T extends ConstSchemaType> = {
|
|
9
10
|
status: "SUCCESS";
|
|
10
11
|
message: string;
|
|
@@ -15,28 +16,33 @@ type ErrorResponse = {
|
|
|
15
16
|
message: string;
|
|
16
17
|
};
|
|
17
18
|
type MyResponse<T extends ConstSchemaType> = Response<SuccessResponse<T> | ErrorResponse>;
|
|
19
|
+
type RequestParamWrapper<RQ extends ConstSchemaType, P extends boolean = false> = {
|
|
20
|
+
isPartial?: P;
|
|
21
|
+
data: RQ;
|
|
22
|
+
};
|
|
23
|
+
type RequestQueryBodyWrapper<M extends Method, RQ extends ConstSchemaType, P extends boolean = false> = MyRequest<M, RQ, P>;
|
|
18
24
|
export default class GGExpressV2<appName extends string> {
|
|
19
25
|
express: Express.Express;
|
|
20
26
|
private outputPath;
|
|
21
27
|
appName: string;
|
|
22
28
|
constructor(app: Express.Express, appName: appName, outputPath: string[]);
|
|
23
29
|
private rootMethod;
|
|
24
|
-
get<RQ extends ConstSchemaType, RS extends ConstSchemaType>(url: `/api/${appName | "seed"}/${string}` | `/api/${appName | "seed"}/${string}`[], options: {
|
|
25
|
-
requireParams: RQ
|
|
30
|
+
get<RQ extends ConstSchemaType, RS extends ConstSchemaType, P extends boolean = false>(url: `/api/${appName | "seed"}/${string}` | `/api/${appName | "seed"}/${string}`[], options: {
|
|
31
|
+
requireParams: RequestParamWrapper<RQ, P>;
|
|
26
32
|
responseStructure: RS;
|
|
27
|
-
}, ...middlewares: Array<(req:
|
|
28
|
-
post<RQ extends ConstSchemaType, RS extends ConstSchemaType>(url: `/api/${appName | "seed"}/${string}` | `/api/${appName | "seed"}/${string}`[], options: {
|
|
29
|
-
requireParams: RQ
|
|
33
|
+
}, ...middlewares: Array<(req: RequestQueryBodyWrapper<"get", RQ, P>, res: MyResponse<RS>, next: NextFunction) => any>): Express.Express;
|
|
34
|
+
post<RQ extends ConstSchemaType, RS extends ConstSchemaType, P extends boolean = false>(url: `/api/${appName | "seed"}/${string}` | `/api/${appName | "seed"}/${string}`[], options: {
|
|
35
|
+
requireParams: RequestParamWrapper<RQ, P>;
|
|
30
36
|
responseStructure: RS;
|
|
31
|
-
}, ...middlewares: Array<(req:
|
|
32
|
-
put<RQ extends ConstSchemaType, RS extends ConstSchemaType>(url: `/api/${appName | "seed"}/${string}` | `/api/${appName | "seed"}/${string}`[], options: {
|
|
33
|
-
requireParams: RQ
|
|
37
|
+
}, ...middlewares: Array<(req: RequestQueryBodyWrapper<"post", RQ, P>, res: MyResponse<RS>, next: NextFunction) => any>): Express.Express;
|
|
38
|
+
put<RQ extends ConstSchemaType, RS extends ConstSchemaType, P extends boolean = false>(url: `/api/${appName | "seed"}/${string}` | `/api/${appName | "seed"}/${string}`[], options: {
|
|
39
|
+
requireParams: RequestParamWrapper<RQ, P>;
|
|
34
40
|
responseStructure: RS;
|
|
35
|
-
}, ...middlewares: Array<(req:
|
|
36
|
-
delete<RQ extends ConstSchemaType, RS extends ConstSchemaType>(url: `/api/${appName | "seed"}/${string}` | `/api/${appName | "seed"}/${string}`[], options: {
|
|
37
|
-
requireParams: RQ
|
|
41
|
+
}, ...middlewares: Array<(req: RequestQueryBodyWrapper<"put", RQ, P>, res: MyResponse<RS>, next: NextFunction) => any>): Express.Express;
|
|
42
|
+
delete<RQ extends ConstSchemaType, RS extends ConstSchemaType, P extends boolean = false>(url: `/api/${appName | "seed"}/${string}` | `/api/${appName | "seed"}/${string}`[], options: {
|
|
43
|
+
requireParams: RequestParamWrapper<RQ, P>;
|
|
38
44
|
responseStructure: RS;
|
|
39
|
-
}, ...middlewares: Array<(req:
|
|
45
|
+
}, ...middlewares: Array<(req: RequestQueryBodyWrapper<"delete", RQ, P>, res: MyResponse<RS>, next: NextFunction) => any>): Express.Express;
|
|
40
46
|
generateAPIFiles(): Promise<void>;
|
|
41
47
|
private generateStaticRouteFile;
|
|
42
48
|
}
|
package/dist/v2/GGExpressV2.js
CHANGED
|
@@ -35,7 +35,8 @@ class GGExpressV2 {
|
|
|
35
35
|
myExpressRouteList.push({
|
|
36
36
|
method: method,
|
|
37
37
|
url: url,
|
|
38
|
-
|
|
38
|
+
isPartialRequireParam: options.requireParams.isPartial === true ? true : false,
|
|
39
|
+
requireParams: options.requireParams.data,
|
|
39
40
|
responseStructure: Object.assign({}, options.responseStructure),
|
|
40
41
|
});
|
|
41
42
|
});
|
|
@@ -69,6 +70,7 @@ class GGExpressV2 {
|
|
|
69
70
|
for (let row of this.outputPath) {
|
|
70
71
|
fs_1.default.writeFileSync(path_1.default.join(row, `staticRouteInterface_${this.appName}_v2.ts`), staticRouteCode);
|
|
71
72
|
yield (0, generateGGApi_v2_1.generateGGApi_v2)(this.appName, path_1.default.join(row, `apiConnector_${this.appName}_v2.ts`));
|
|
73
|
+
console.log(`generate -> ${`staticRouteInterface_${this.appName}_v2.ts`}`);
|
|
72
74
|
}
|
|
73
75
|
});
|
|
74
76
|
}
|
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import { Method } from "./GGExpressV2";
|
|
2
2
|
import { ConstSchemaType } from "./typeResolver";
|
|
3
|
-
type routeList = {
|
|
3
|
+
export type routeList = {
|
|
4
4
|
method: Method;
|
|
5
5
|
url: string;
|
|
6
|
+
isPartialRequireParam: boolean;
|
|
6
7
|
requireParams: ConstSchemaType;
|
|
7
8
|
responseStructure: ConstSchemaType;
|
|
8
9
|
};
|
|
9
|
-
type
|
|
10
|
+
type PrepareRouteStructure = {
|
|
10
11
|
[K in Method]: {
|
|
11
12
|
[key in string]: {
|
|
12
13
|
requireParams: string;
|
|
13
14
|
responseStructure: string;
|
|
15
|
+
isPartial: boolean;
|
|
14
16
|
};
|
|
15
17
|
};
|
|
16
18
|
};
|
|
17
|
-
export declare function prepareExportInterfaceData(data: routeList[]):
|
|
19
|
+
export declare function prepareExportInterfaceData(data: routeList[]): PrepareRouteStructure;
|
|
18
20
|
export declare function generateStaticRouteFileV2(appName: string, data: routeList[]): string;
|
|
19
21
|
export {};
|
|
@@ -15,14 +15,21 @@ function prepareExportInterfaceData(data) {
|
|
|
15
15
|
function extract(allData, prepareRoute) {
|
|
16
16
|
for (const route of allData) {
|
|
17
17
|
prepareRoute[route.method][route.url] = {
|
|
18
|
-
requireParams: parentInputToCode(route.requireParams),
|
|
19
|
-
responseStructure: parentInputToCode(route.responseStructure),
|
|
18
|
+
requireParams: parentInputToCode(route.requireParams, route.isPartialRequireParam),
|
|
19
|
+
responseStructure: parentInputToCode(route.responseStructure, false),
|
|
20
|
+
isPartial: route.isPartialRequireParam,
|
|
20
21
|
};
|
|
21
22
|
}
|
|
22
23
|
return prepareRoute;
|
|
23
24
|
}
|
|
24
|
-
function parentInputToCode(data) {
|
|
25
|
+
function parentInputToCode(data, isPartialRequireParam) {
|
|
25
26
|
const recursiveExtract = (target, parentKeyName) => {
|
|
27
|
+
const getParentKeyCode = (parentKeyName) => {
|
|
28
|
+
if (isPartialRequireParam === true)
|
|
29
|
+
return `${parentKeyName}?`;
|
|
30
|
+
else
|
|
31
|
+
return parentKeyName;
|
|
32
|
+
};
|
|
26
33
|
let result = "";
|
|
27
34
|
// check nest pimitive -------------------
|
|
28
35
|
if (typeof target === "string") {
|
|
@@ -39,7 +46,7 @@ function parentInputToCode(data) {
|
|
|
39
46
|
result = `(${result})[]`;
|
|
40
47
|
else
|
|
41
48
|
result;
|
|
42
|
-
return `${parentKeyName} : ${result}`;
|
|
49
|
+
return `${getParentKeyCode(parentKeyName)} : ${result}`;
|
|
43
50
|
}
|
|
44
51
|
// check object ---------------------------
|
|
45
52
|
else if (typeof target === "object" && Array.isArray(target) === false) {
|
|
@@ -53,7 +60,7 @@ function parentInputToCode(data) {
|
|
|
53
60
|
if (parentKeyName === "root")
|
|
54
61
|
result = ` { ${tempResult.join(",")} } `;
|
|
55
62
|
else
|
|
56
|
-
result = `${result} \n ${parentKeyName} : { ${tempResult.join(",")} }`;
|
|
63
|
+
result = `${result} \n ${getParentKeyCode(parentKeyName)} : { ${tempResult.join(",")} }`;
|
|
57
64
|
}
|
|
58
65
|
// check array ---------------------------
|
|
59
66
|
else if (Array.isArray(target)) {
|
|
@@ -62,10 +69,7 @@ function parentInputToCode(data) {
|
|
|
62
69
|
typeof target[0] === "string" ||
|
|
63
70
|
target[0] === null ||
|
|
64
71
|
target[0] === undefined)
|
|
65
|
-
|
|
66
|
-
// .map((word) => ` "${word}" `)
|
|
67
|
-
// .join(",")}] `
|
|
68
|
-
return `${parentKeyName} : ${target
|
|
72
|
+
return `${getParentKeyCode(parentKeyName)} : ${target
|
|
69
73
|
.map((word) => ` "${word}" `)
|
|
70
74
|
.join("|")} `;
|
|
71
75
|
// nest or input nest
|
|
@@ -90,7 +94,8 @@ function generateStaticRouteFileV2(appName, data) {
|
|
|
90
94
|
responseStructure : {
|
|
91
95
|
data : ${data[keyName].responseStructure},
|
|
92
96
|
status: "SUCCESS" | "ERROR",
|
|
93
|
-
message: string
|
|
97
|
+
message: string
|
|
98
|
+
}
|
|
94
99
|
}`)
|
|
95
100
|
.join(", ");
|
|
96
101
|
};
|
|
@@ -64,7 +64,9 @@ export interface staticRouteInterface_hotel_v2 {
|
|
|
64
64
|
post: {
|
|
65
65
|
"/api/hotel/booking/id": {
|
|
66
66
|
requireParams: {
|
|
67
|
-
id
|
|
67
|
+
id?: number;
|
|
68
|
+
id2?: number;
|
|
69
|
+
name?: string;
|
|
68
70
|
};
|
|
69
71
|
responseStructure: {
|
|
70
72
|
data: {
|
|
@@ -78,6 +80,20 @@ export interface staticRouteInterface_hotel_v2 {
|
|
|
78
80
|
};
|
|
79
81
|
};
|
|
80
82
|
};
|
|
81
|
-
put: {
|
|
83
|
+
put: {
|
|
84
|
+
"/api/hotel/testPut": {
|
|
85
|
+
requireParams: {};
|
|
86
|
+
responseStructure: {
|
|
87
|
+
data: {
|
|
88
|
+
bookingData: {
|
|
89
|
+
id: number;
|
|
90
|
+
roomNumber: string;
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
status: "SUCCESS" | "ERROR";
|
|
94
|
+
message: string;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
};
|
|
82
98
|
delete: {};
|
|
83
99
|
}
|
package/dist/v2/run_v2.test.js
CHANGED
|
@@ -43,10 +43,12 @@ function run() {
|
|
|
43
43
|
const ggapp = new GGExpressV2_1.default(app, "hotel", ["./output"]);
|
|
44
44
|
ggapp.get("/api/hotel/users/id", {
|
|
45
45
|
requireParams: {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
data: {
|
|
47
|
+
id: "number",
|
|
48
|
+
data: exports.hotel_model_const_temp["booking"],
|
|
49
|
+
name: "string",
|
|
50
|
+
status: ["on", "off"],
|
|
51
|
+
},
|
|
50
52
|
},
|
|
51
53
|
responseStructure: {
|
|
52
54
|
x: "string",
|
|
@@ -81,7 +83,6 @@ function run() {
|
|
|
81
83
|
],
|
|
82
84
|
},
|
|
83
85
|
}, (req, res, next) => {
|
|
84
|
-
req.query.status === "on";
|
|
85
86
|
return res.json({
|
|
86
87
|
message: "",
|
|
87
88
|
status: "SUCCESS",
|
|
@@ -121,7 +122,12 @@ function run() {
|
|
|
121
122
|
});
|
|
122
123
|
ggapp.post("/api/hotel/booking/id", {
|
|
123
124
|
requireParams: {
|
|
124
|
-
|
|
125
|
+
isPartial: true,
|
|
126
|
+
data: {
|
|
127
|
+
id: "number",
|
|
128
|
+
id2: "number",
|
|
129
|
+
name: "string",
|
|
130
|
+
},
|
|
125
131
|
},
|
|
126
132
|
responseStructure: {
|
|
127
133
|
bookingData: {
|
|
@@ -130,7 +136,6 @@ function run() {
|
|
|
130
136
|
},
|
|
131
137
|
},
|
|
132
138
|
}, (req, res, next) => {
|
|
133
|
-
req.body.id;
|
|
134
139
|
return res.json({
|
|
135
140
|
message: "",
|
|
136
141
|
status: "SUCCESS",
|
|
@@ -142,9 +147,9 @@ function run() {
|
|
|
142
147
|
},
|
|
143
148
|
});
|
|
144
149
|
});
|
|
145
|
-
ggapp.
|
|
150
|
+
ggapp.put("/api/hotel/testPut", {
|
|
146
151
|
requireParams: {
|
|
147
|
-
|
|
152
|
+
data: {},
|
|
148
153
|
},
|
|
149
154
|
responseStructure: {
|
|
150
155
|
bookingData: {
|
|
@@ -153,7 +158,6 @@ function run() {
|
|
|
153
158
|
},
|
|
154
159
|
},
|
|
155
160
|
}, (req, res, next) => {
|
|
156
|
-
req.body.id;
|
|
157
161
|
return res.json({
|
|
158
162
|
status: "ERROR",
|
|
159
163
|
message: "",
|
package/dist/v2/typeResolver.js
CHANGED
package/package.json
CHANGED
package/src/v2/GGExpressV2.ts
CHANGED
|
@@ -1,21 +1,53 @@
|
|
|
1
|
-
import { NextFunction
|
|
1
|
+
import { NextFunction } from "express"
|
|
2
|
+
import { Request, Response } from "express-serve-static-core"
|
|
2
3
|
import Express from "express-serve-static-core"
|
|
3
4
|
import fs from "fs"
|
|
4
5
|
import { TypeResolve, ConstSchemaType } from "./typeResolver"
|
|
5
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
generateStaticRouteFileV2,
|
|
8
|
+
routeList,
|
|
9
|
+
} from "./generateStaticRouteFileV2"
|
|
6
10
|
import path from "path"
|
|
7
11
|
import { generateGGApi_v2 } from "./generateGGApi_v2"
|
|
8
12
|
|
|
9
13
|
export type Method = "get" | "post" | "put" | "delete"
|
|
10
14
|
|
|
11
|
-
type MyRequest<
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
// type MyRequest<
|
|
16
|
+
// M extends Method,
|
|
17
|
+
// RQ extends ConstSchemaType,
|
|
18
|
+
// P extends boolean = false
|
|
19
|
+
// > = M extends "get"
|
|
20
|
+
// ? // get
|
|
21
|
+
// P extends true
|
|
22
|
+
// ? TypePartialResolve<MyRequestQuery<RQ>>
|
|
23
|
+
// : TypeResolve<MyRequestQuery<RQ>>
|
|
24
|
+
// : // post, put, delete
|
|
25
|
+
// P extends true
|
|
26
|
+
// ? TypePartialResolve<MyRequestBody<RQ>>
|
|
27
|
+
// : TypeResolve<MyRequestBody<RQ>>
|
|
28
|
+
type MyRequest<
|
|
29
|
+
M extends Method,
|
|
30
|
+
RQ extends ConstSchemaType,
|
|
31
|
+
P extends boolean = false
|
|
32
|
+
> = M extends "get"
|
|
33
|
+
? // get
|
|
34
|
+
MyRequestQuery<RQ, P>
|
|
14
35
|
: // post, put, delete
|
|
15
|
-
|
|
36
|
+
MyRequestBody<RQ, P>
|
|
16
37
|
|
|
17
|
-
type MyRequestQuery<
|
|
18
|
-
|
|
38
|
+
type MyRequestQuery<
|
|
39
|
+
T extends ConstSchemaType,
|
|
40
|
+
P extends boolean = false
|
|
41
|
+
> = P extends true
|
|
42
|
+
? Request<{}, {}, {}, Partial<TypeResolve<T>>, {}>
|
|
43
|
+
: Request<{}, {}, {}, TypeResolve<T>, {}>
|
|
44
|
+
|
|
45
|
+
type MyRequestBody<
|
|
46
|
+
T extends ConstSchemaType,
|
|
47
|
+
P extends boolean = false
|
|
48
|
+
> = P extends true
|
|
49
|
+
? Request<{}, {}, Partial<TypeResolve<T>>, {}, {}>
|
|
50
|
+
: Request<{}, {}, TypeResolve<T>, {}, {}>
|
|
19
51
|
|
|
20
52
|
type SuccessResponse<T extends ConstSchemaType> = {
|
|
21
53
|
status: "SUCCESS"
|
|
@@ -30,15 +62,22 @@ type ErrorResponse = {
|
|
|
30
62
|
type MyResponse<T extends ConstSchemaType> = Response<
|
|
31
63
|
SuccessResponse<T> | ErrorResponse
|
|
32
64
|
>
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
65
|
+
|
|
66
|
+
type RequestParamWrapper<
|
|
67
|
+
RQ extends ConstSchemaType,
|
|
68
|
+
P extends boolean = false
|
|
69
|
+
> = {
|
|
70
|
+
isPartial?: P
|
|
71
|
+
data: RQ
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
type RequestQueryBodyWrapper<
|
|
75
|
+
M extends Method,
|
|
76
|
+
RQ extends ConstSchemaType,
|
|
77
|
+
P extends boolean = false
|
|
78
|
+
> = MyRequest<M, RQ, P>
|
|
79
|
+
|
|
80
|
+
const myExpressRouteList: routeList[] = []
|
|
42
81
|
|
|
43
82
|
export default class GGExpressV2<appName extends string> {
|
|
44
83
|
public express: Express.Express
|
|
@@ -53,17 +92,18 @@ export default class GGExpressV2<appName extends string> {
|
|
|
53
92
|
private rootMethod<
|
|
54
93
|
M extends Method,
|
|
55
94
|
RQ extends ConstSchemaType,
|
|
56
|
-
RS extends ConstSchemaType
|
|
95
|
+
RS extends ConstSchemaType,
|
|
96
|
+
P extends boolean = false
|
|
57
97
|
>(
|
|
58
98
|
method: M,
|
|
59
99
|
url: string | string[],
|
|
60
100
|
options: {
|
|
61
|
-
requireParams: RQ
|
|
101
|
+
requireParams: RequestParamWrapper<RQ, P>
|
|
62
102
|
responseStructure: RS
|
|
63
103
|
},
|
|
64
104
|
...middlewares: Array<
|
|
65
105
|
(
|
|
66
|
-
req:
|
|
106
|
+
req: RequestQueryBodyWrapper<M, RQ, P>,
|
|
67
107
|
res: MyResponse<RS>,
|
|
68
108
|
next: NextFunction
|
|
69
109
|
) => void | Promise<void>
|
|
@@ -79,7 +119,9 @@ export default class GGExpressV2<appName extends string> {
|
|
|
79
119
|
myExpressRouteList.push({
|
|
80
120
|
method: method,
|
|
81
121
|
url: url,
|
|
82
|
-
|
|
122
|
+
isPartialRequireParam:
|
|
123
|
+
options.requireParams.isPartial === true ? true : false,
|
|
124
|
+
requireParams: options.requireParams.data,
|
|
83
125
|
responseStructure: {
|
|
84
126
|
...options.responseStructure,
|
|
85
127
|
},
|
|
@@ -94,21 +136,29 @@ export default class GGExpressV2<appName extends string> {
|
|
|
94
136
|
// แต่ middleware ของคุณจะถูก wrap ทีละตัว
|
|
95
137
|
...middlewares.map(
|
|
96
138
|
(mw) => (req: Request, res: Response, next: NextFunction) =>
|
|
97
|
-
mw(
|
|
139
|
+
mw(
|
|
140
|
+
req as RequestQueryBodyWrapper<M, RQ, P>,
|
|
141
|
+
res as MyResponse<RS>,
|
|
142
|
+
next
|
|
143
|
+
)
|
|
98
144
|
)
|
|
99
145
|
)
|
|
100
146
|
}
|
|
101
|
-
get<
|
|
147
|
+
get<
|
|
148
|
+
RQ extends ConstSchemaType,
|
|
149
|
+
RS extends ConstSchemaType,
|
|
150
|
+
P extends boolean = false
|
|
151
|
+
>(
|
|
102
152
|
url:
|
|
103
153
|
| `/api/${appName | "seed"}/${string}`
|
|
104
154
|
| `/api/${appName | "seed"}/${string}`[],
|
|
105
155
|
options: {
|
|
106
|
-
requireParams: RQ
|
|
156
|
+
requireParams: RequestParamWrapper<RQ, P>
|
|
107
157
|
responseStructure: RS
|
|
108
158
|
},
|
|
109
159
|
...middlewares: Array<
|
|
110
160
|
(
|
|
111
|
-
req:
|
|
161
|
+
req: RequestQueryBodyWrapper<"get", RQ, P>,
|
|
112
162
|
res: MyResponse<RS>,
|
|
113
163
|
next: NextFunction
|
|
114
164
|
) => any
|
|
@@ -117,17 +167,21 @@ export default class GGExpressV2<appName extends string> {
|
|
|
117
167
|
return this.rootMethod("get", url, options, ...middlewares)
|
|
118
168
|
}
|
|
119
169
|
|
|
120
|
-
post<
|
|
170
|
+
post<
|
|
171
|
+
RQ extends ConstSchemaType,
|
|
172
|
+
RS extends ConstSchemaType,
|
|
173
|
+
P extends boolean = false
|
|
174
|
+
>(
|
|
121
175
|
url:
|
|
122
176
|
| `/api/${appName | "seed"}/${string}`
|
|
123
177
|
| `/api/${appName | "seed"}/${string}`[],
|
|
124
178
|
options: {
|
|
125
|
-
requireParams: RQ
|
|
179
|
+
requireParams: RequestParamWrapper<RQ, P>
|
|
126
180
|
responseStructure: RS
|
|
127
181
|
},
|
|
128
182
|
...middlewares: Array<
|
|
129
183
|
(
|
|
130
|
-
req:
|
|
184
|
+
req: RequestQueryBodyWrapper<"post", RQ, P>,
|
|
131
185
|
res: MyResponse<RS>,
|
|
132
186
|
next: NextFunction
|
|
133
187
|
) => any
|
|
@@ -135,17 +189,21 @@ export default class GGExpressV2<appName extends string> {
|
|
|
135
189
|
) {
|
|
136
190
|
return this.rootMethod("post", url, options, ...middlewares)
|
|
137
191
|
}
|
|
138
|
-
put<
|
|
192
|
+
put<
|
|
193
|
+
RQ extends ConstSchemaType,
|
|
194
|
+
RS extends ConstSchemaType,
|
|
195
|
+
P extends boolean = false
|
|
196
|
+
>(
|
|
139
197
|
url:
|
|
140
198
|
| `/api/${appName | "seed"}/${string}`
|
|
141
199
|
| `/api/${appName | "seed"}/${string}`[],
|
|
142
200
|
options: {
|
|
143
|
-
requireParams: RQ
|
|
201
|
+
requireParams: RequestParamWrapper<RQ, P>
|
|
144
202
|
responseStructure: RS
|
|
145
203
|
},
|
|
146
204
|
...middlewares: Array<
|
|
147
205
|
(
|
|
148
|
-
req:
|
|
206
|
+
req: RequestQueryBodyWrapper<"put", RQ, P>,
|
|
149
207
|
res: MyResponse<RS>,
|
|
150
208
|
next: NextFunction
|
|
151
209
|
) => any
|
|
@@ -153,17 +211,21 @@ export default class GGExpressV2<appName extends string> {
|
|
|
153
211
|
) {
|
|
154
212
|
return this.rootMethod("put", url, options, ...middlewares)
|
|
155
213
|
}
|
|
156
|
-
delete<
|
|
214
|
+
delete<
|
|
215
|
+
RQ extends ConstSchemaType,
|
|
216
|
+
RS extends ConstSchemaType,
|
|
217
|
+
P extends boolean = false
|
|
218
|
+
>(
|
|
157
219
|
url:
|
|
158
220
|
| `/api/${appName | "seed"}/${string}`
|
|
159
221
|
| `/api/${appName | "seed"}/${string}`[],
|
|
160
222
|
options: {
|
|
161
|
-
requireParams: RQ
|
|
223
|
+
requireParams: RequestParamWrapper<RQ, P>
|
|
162
224
|
responseStructure: RS
|
|
163
225
|
},
|
|
164
226
|
...middlewares: Array<
|
|
165
227
|
(
|
|
166
|
-
req:
|
|
228
|
+
req: RequestQueryBodyWrapper<"delete", RQ, P>,
|
|
167
229
|
res: MyResponse<RS>,
|
|
168
230
|
next: NextFunction
|
|
169
231
|
) => any
|
|
@@ -191,6 +253,7 @@ export default class GGExpressV2<appName extends string> {
|
|
|
191
253
|
this.appName,
|
|
192
254
|
path.join(row, `apiConnector_${this.appName}_v2.ts`)
|
|
193
255
|
)
|
|
256
|
+
console.log(`generate -> ${`staticRouteInterface_${this.appName}_v2.ts`}`)
|
|
194
257
|
}
|
|
195
258
|
}
|
|
196
259
|
}
|
|
@@ -1,26 +1,25 @@
|
|
|
1
1
|
import { Method } from "./GGExpressV2"
|
|
2
2
|
import { ConstSchemaType } from "./typeResolver"
|
|
3
|
-
type routeList = {
|
|
3
|
+
export type routeList = {
|
|
4
4
|
method: Method
|
|
5
5
|
url: string
|
|
6
|
+
isPartialRequireParam: boolean
|
|
6
7
|
requireParams: ConstSchemaType
|
|
7
8
|
responseStructure: ConstSchemaType
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
type
|
|
11
|
+
type PrepareRouteStructure = {
|
|
11
12
|
[K in Method]: {
|
|
12
13
|
[key in string]: {
|
|
13
14
|
requireParams: string
|
|
14
15
|
responseStructure: string
|
|
16
|
+
isPartial: boolean
|
|
15
17
|
}
|
|
16
18
|
}
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
type MyResponse = {
|
|
20
|
-
[url in string]: {}
|
|
21
|
-
}
|
|
22
21
|
export function prepareExportInterfaceData(data: routeList[]) {
|
|
23
|
-
let prepareRoute:
|
|
22
|
+
let prepareRoute: PrepareRouteStructure = {
|
|
24
23
|
get: {},
|
|
25
24
|
post: {},
|
|
26
25
|
put: {},
|
|
@@ -31,20 +30,31 @@ export function prepareExportInterfaceData(data: routeList[]) {
|
|
|
31
30
|
}
|
|
32
31
|
function extract(
|
|
33
32
|
allData: routeList[],
|
|
34
|
-
prepareRoute:
|
|
35
|
-
):
|
|
33
|
+
prepareRoute: PrepareRouteStructure
|
|
34
|
+
): PrepareRouteStructure {
|
|
36
35
|
for (const route of allData) {
|
|
37
36
|
prepareRoute[route.method][route.url] = {
|
|
38
|
-
requireParams: parentInputToCode(
|
|
39
|
-
|
|
37
|
+
requireParams: parentInputToCode(
|
|
38
|
+
route.requireParams,
|
|
39
|
+
route.isPartialRequireParam
|
|
40
|
+
),
|
|
41
|
+
responseStructure: parentInputToCode(route.responseStructure, false),
|
|
42
|
+
isPartial: route.isPartialRequireParam,
|
|
40
43
|
}
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
return prepareRoute
|
|
44
47
|
}
|
|
45
48
|
|
|
46
|
-
function parentInputToCode(
|
|
49
|
+
function parentInputToCode(
|
|
50
|
+
data: ConstSchemaType,
|
|
51
|
+
isPartialRequireParam: boolean
|
|
52
|
+
) {
|
|
47
53
|
const recursiveExtract = (target: ConstSchemaType, parentKeyName: string) => {
|
|
54
|
+
const getParentKeyCode = (parentKeyName: string) => {
|
|
55
|
+
if (isPartialRequireParam === true) return `${parentKeyName}?`
|
|
56
|
+
else return parentKeyName
|
|
57
|
+
}
|
|
48
58
|
let result = ""
|
|
49
59
|
// check nest pimitive -------------------
|
|
50
60
|
if (typeof target === "string") {
|
|
@@ -59,7 +69,7 @@ function parentInputToCode(data: ConstSchemaType) {
|
|
|
59
69
|
.join(" | ")
|
|
60
70
|
if (isArray) result = `(${result})[]`
|
|
61
71
|
else result
|
|
62
|
-
return `${parentKeyName} : ${result}`
|
|
72
|
+
return `${getParentKeyCode(parentKeyName)} : ${result}`
|
|
63
73
|
}
|
|
64
74
|
|
|
65
75
|
// check object ---------------------------
|
|
@@ -73,7 +83,9 @@ function parentInputToCode(data: ConstSchemaType) {
|
|
|
73
83
|
}
|
|
74
84
|
if (parentKeyName === "root") result = ` { ${tempResult.join(",")} } `
|
|
75
85
|
else
|
|
76
|
-
result = `${result} \n ${
|
|
86
|
+
result = `${result} \n ${getParentKeyCode(
|
|
87
|
+
parentKeyName
|
|
88
|
+
)} : { ${tempResult.join(",")} }`
|
|
77
89
|
}
|
|
78
90
|
|
|
79
91
|
// check array ---------------------------
|
|
@@ -85,10 +97,7 @@ function parentInputToCode(data: ConstSchemaType) {
|
|
|
85
97
|
target[0] === null ||
|
|
86
98
|
target[0] === undefined
|
|
87
99
|
)
|
|
88
|
-
|
|
89
|
-
// .map((word) => ` "${word}" `)
|
|
90
|
-
// .join(",")}] `
|
|
91
|
-
return `${parentKeyName} : ${(target as string[])
|
|
100
|
+
return `${getParentKeyCode(parentKeyName)} : ${(target as string[])
|
|
92
101
|
.map((word) => ` "${word}" `)
|
|
93
102
|
.join("|")} `
|
|
94
103
|
// nest or input nest
|
|
@@ -104,7 +113,7 @@ function parentInputToCode(data: ConstSchemaType) {
|
|
|
104
113
|
}
|
|
105
114
|
export function generateStaticRouteFileV2(appName: string, data: routeList[]) {
|
|
106
115
|
const interfaceName = `staticRouteInterface_${appName}_v2`
|
|
107
|
-
const generateEachUrl = (data:
|
|
116
|
+
const generateEachUrl = (data: PrepareRouteStructure[Method]) => {
|
|
108
117
|
const keyName = Object.keys(data)
|
|
109
118
|
return keyName
|
|
110
119
|
.map(
|
|
@@ -114,7 +123,8 @@ export function generateStaticRouteFileV2(appName: string, data: routeList[]) {
|
|
|
114
123
|
responseStructure : {
|
|
115
124
|
data : ${data[keyName].responseStructure},
|
|
116
125
|
status: "SUCCESS" | "ERROR",
|
|
117
|
-
message: string
|
|
126
|
+
message: string
|
|
127
|
+
}
|
|
118
128
|
}`
|
|
119
129
|
)
|
|
120
130
|
.join(", ")
|
|
@@ -54,7 +54,19 @@ export interface staticRouteInterface_hotel_v2 {
|
|
|
54
54
|
}
|
|
55
55
|
post: {
|
|
56
56
|
"/api/hotel/booking/id": {
|
|
57
|
-
requireParams: { id
|
|
57
|
+
requireParams: { id?: number; id2?: number; name?: string }
|
|
58
|
+
responseStructure: {
|
|
59
|
+
data: {
|
|
60
|
+
bookingData: { id: number; roomNumber: string }
|
|
61
|
+
}
|
|
62
|
+
status: "SUCCESS" | "ERROR"
|
|
63
|
+
message: string
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
put: {
|
|
68
|
+
"/api/hotel/testPut": {
|
|
69
|
+
requireParams: {}
|
|
58
70
|
responseStructure: {
|
|
59
71
|
data: {
|
|
60
72
|
bookingData: { id: number; roomNumber: string }
|
|
@@ -64,6 +76,5 @@ export interface staticRouteInterface_hotel_v2 {
|
|
|
64
76
|
}
|
|
65
77
|
}
|
|
66
78
|
}
|
|
67
|
-
put: {}
|
|
68
79
|
delete: {}
|
|
69
80
|
}
|
package/src/v2/run_v2.test.ts
CHANGED
|
@@ -33,10 +33,12 @@ function run() {
|
|
|
33
33
|
"/api/hotel/users/id",
|
|
34
34
|
{
|
|
35
35
|
requireParams: {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
data: {
|
|
37
|
+
id: "number",
|
|
38
|
+
data: hotel_model_const_temp["booking"],
|
|
39
|
+
name: "string",
|
|
40
|
+
status: ["on", "off"],
|
|
41
|
+
},
|
|
40
42
|
},
|
|
41
43
|
responseStructure: {
|
|
42
44
|
x: "string",
|
|
@@ -72,7 +74,6 @@ function run() {
|
|
|
72
74
|
},
|
|
73
75
|
},
|
|
74
76
|
(req, res, next) => {
|
|
75
|
-
req.query.status === "on"
|
|
76
77
|
return res.json({
|
|
77
78
|
message: "",
|
|
78
79
|
status: "SUCCESS",
|
|
@@ -115,7 +116,12 @@ function run() {
|
|
|
115
116
|
"/api/hotel/booking/id",
|
|
116
117
|
{
|
|
117
118
|
requireParams: {
|
|
118
|
-
|
|
119
|
+
isPartial: true,
|
|
120
|
+
data: {
|
|
121
|
+
id: "number",
|
|
122
|
+
id2: "number",
|
|
123
|
+
name: "string",
|
|
124
|
+
},
|
|
119
125
|
},
|
|
120
126
|
responseStructure: {
|
|
121
127
|
bookingData: {
|
|
@@ -125,7 +131,6 @@ function run() {
|
|
|
125
131
|
},
|
|
126
132
|
},
|
|
127
133
|
(req, res, next) => {
|
|
128
|
-
req.body.id
|
|
129
134
|
return res.json({
|
|
130
135
|
message: "",
|
|
131
136
|
status: "SUCCESS",
|
|
@@ -139,11 +144,11 @@ function run() {
|
|
|
139
144
|
}
|
|
140
145
|
)
|
|
141
146
|
|
|
142
|
-
ggapp.
|
|
143
|
-
"/api/hotel/
|
|
147
|
+
ggapp.put(
|
|
148
|
+
"/api/hotel/testPut",
|
|
144
149
|
{
|
|
145
150
|
requireParams: {
|
|
146
|
-
|
|
151
|
+
data: {},
|
|
147
152
|
},
|
|
148
153
|
responseStructure: {
|
|
149
154
|
bookingData: {
|
|
@@ -153,7 +158,6 @@ function run() {
|
|
|
153
158
|
},
|
|
154
159
|
},
|
|
155
160
|
(req, res, next) => {
|
|
156
|
-
req.body.id
|
|
157
161
|
return res.json({
|
|
158
162
|
status: "ERROR",
|
|
159
163
|
message: "",
|
package/src/v2/typeResolver.ts
CHANGED
|
@@ -14,6 +14,7 @@ type InputEnum = readonly string[] | readonly number[]
|
|
|
14
14
|
export type ConstSchemaType = {
|
|
15
15
|
[key in string]: CustomType | ConstSchemaType | ConstSchemaType[] | InputEnum
|
|
16
16
|
}
|
|
17
|
+
|
|
17
18
|
type ResolveBase<T extends BaseType> = T extends "string"
|
|
18
19
|
? string
|
|
19
20
|
: T extends "string[]"
|
|
@@ -56,3 +57,6 @@ type chooseType<T> = T extends InputEnum
|
|
|
56
57
|
export type TypeResolve<T> = {
|
|
57
58
|
[K in keyof T]: chooseType<T[K]>
|
|
58
59
|
}
|
|
60
|
+
// export type PartialTypeResolve<T> = {
|
|
61
|
+
// [K in keyof T]: chooseType<T[K]>
|
|
62
|
+
// }
|