gg-express 1.0.1
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/GGApi.d.ts +8 -0
- package/dist/GGApi.js +59 -0
- package/dist/GGExpress.d.ts +61 -0
- package/dist/GGExpress.js +117 -0
- package/dist/dynamicRoute.d.ts +0 -0
- package/dist/dynamicRoute.js +30 -0
- package/dist/staticRouteInterface.d.ts +16 -0
- package/dist/staticRouteInterface.js +2 -0
- package/package.json +23 -0
- package/src/GGApi.ts +67 -0
- package/src/GGExpress.ts +302 -0
- package/src/dynamicRoute.ts +32 -0
- package/src/staticRouteInterface.ts +10 -0
- package/tsconfig.json +14 -0
package/dist/GGApi.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { staticRouteInterface } from "./staticRouteInterface";
|
|
2
|
+
export declare class GGApi {
|
|
3
|
+
constructor();
|
|
4
|
+
get<T extends keyof staticRouteInterface["get"]>(url: T, requireParams: staticRouteInterface["get"][T]["requireParams"]): Promise<staticRouteInterface["get"][T]["responseStructure"]>;
|
|
5
|
+
post<T extends keyof staticRouteInterface["post"]>(url: T, requireParams: staticRouteInterface["post"][T]["requireParams"]): Promise<staticRouteInterface["post"][T]["responseStructure"]>;
|
|
6
|
+
put<T extends keyof staticRouteInterface["put"]>(url: T, requireParams: staticRouteInterface["put"][T]["requireParams"]): Promise<staticRouteInterface["put"][T]["responseStructure"]>;
|
|
7
|
+
delete<T extends keyof staticRouteInterface["delete"]>(url: T, requireParams: staticRouteInterface["delete"][T]["requireParams"]): Promise<staticRouteInterface["delete"][T]["responseStructure"]>;
|
|
8
|
+
}
|
package/dist/GGApi.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.GGApi = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
class GGApi {
|
|
9
|
+
constructor() { }
|
|
10
|
+
get(url, requireParams) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
axios_1.default
|
|
13
|
+
.get(url, { params: requireParams })
|
|
14
|
+
.then((response) => {
|
|
15
|
+
resolve(response.data);
|
|
16
|
+
})
|
|
17
|
+
.catch((error) => {
|
|
18
|
+
reject(error);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
post(url, requireParams) {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
axios_1.default
|
|
25
|
+
.post(url, requireParams)
|
|
26
|
+
.then((response) => {
|
|
27
|
+
resolve(response.data);
|
|
28
|
+
})
|
|
29
|
+
.catch((error) => {
|
|
30
|
+
reject(error);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
put(url, requireParams) {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
axios_1.default
|
|
37
|
+
.put(url, requireParams)
|
|
38
|
+
.then((response) => {
|
|
39
|
+
resolve(response.data);
|
|
40
|
+
})
|
|
41
|
+
.catch((error) => {
|
|
42
|
+
reject(error);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
delete(url, requireParams) {
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
axios_1.default
|
|
49
|
+
.delete(url, requireParams)
|
|
50
|
+
.then((response) => {
|
|
51
|
+
resolve(response.data);
|
|
52
|
+
})
|
|
53
|
+
.catch((error) => {
|
|
54
|
+
reject(error);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.GGApi = GGApi;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from "express";
|
|
2
|
+
import Express from "express-serve-static-core";
|
|
3
|
+
type Unarray<T> = T extends (infer U)[] ? U : T;
|
|
4
|
+
type AsConstArray<T extends readonly any[]> = [...T];
|
|
5
|
+
type paramType = "number" | "string" | AsConstArray<Array<string>> | AsConstArray<Array<number>>;
|
|
6
|
+
interface responseStructure {
|
|
7
|
+
dataType: "singleObject" | "arrayObject";
|
|
8
|
+
structure: {
|
|
9
|
+
[key: string]: paramType;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
interface requireParamsStructure {
|
|
13
|
+
dataType: "singleObject" | "arrayObject";
|
|
14
|
+
structure: {
|
|
15
|
+
[key: string]: paramType;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
type numberOrString<T> = T extends "number" ? number : T extends "string" ? string : T extends string[] ? Unarray<T> : T extends number[] ? Unarray<T> : "error-type";
|
|
19
|
+
type singleOrArrayObject<DT extends requireParamsStructure["dataType"], T extends requireParamsStructure["structure"], K extends keyof T> = DT extends "arrayObject" ? {
|
|
20
|
+
data: {
|
|
21
|
+
[Key in K]: numberOrString<T[Key]>;
|
|
22
|
+
}[];
|
|
23
|
+
} : {
|
|
24
|
+
data: {
|
|
25
|
+
[Key in K]: numberOrString<T[Key]>;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
type MyRequest<DT extends requireParamsStructure["dataType"], T extends requireParamsStructure["structure"], K extends keyof T> = Request<{}, {}, singleOrArrayObject<DT, T, K>, singleOrArrayObject<DT, T, K>, {}>;
|
|
29
|
+
type MyResponse<DT extends responseStructure["dataType"], T extends responseStructure["structure"], K extends keyof T> = Response<{
|
|
30
|
+
status: "SUCCESS" | "ERROR";
|
|
31
|
+
message: string;
|
|
32
|
+
data: DT extends "arrayObject" ? Array<{
|
|
33
|
+
[key in K]: numberOrString<T[key]>;
|
|
34
|
+
}> : {
|
|
35
|
+
[key in K]: numberOrString<T[key]>;
|
|
36
|
+
};
|
|
37
|
+
}>;
|
|
38
|
+
export default class MyExpress {
|
|
39
|
+
express: Express.Express;
|
|
40
|
+
private outputPath;
|
|
41
|
+
constructor(app: Express.Express, outputPath: string[]);
|
|
42
|
+
private rootMethod;
|
|
43
|
+
get<M extends requireParamsStructure, T extends M["structure"], K extends keyof T, R extends responseStructure, RS extends R["structure"], KR extends keyof RS>(url: `/api/${string}`, options: {
|
|
44
|
+
requireParams: M;
|
|
45
|
+
responseStructure: R;
|
|
46
|
+
}, ...middlewares: Array<(req: MyRequest<M["dataType"], T, K>, res: MyResponse<R["dataType"], RS, KR>, next: NextFunction) => any>): Express.Express;
|
|
47
|
+
post<M extends requireParamsStructure, T extends M["structure"], K extends keyof T, R extends responseStructure, RS extends R["structure"], KR extends keyof RS>(url: `/api/${string}`, options: {
|
|
48
|
+
requireParams: M;
|
|
49
|
+
responseStructure: R;
|
|
50
|
+
}, ...middlewares: Array<(req: MyRequest<M["dataType"], T, K>, res: MyResponse<R["dataType"], RS, KR>, next: NextFunction) => any>): Express.Express;
|
|
51
|
+
put<M extends requireParamsStructure, T extends M["structure"], K extends keyof T, R extends responseStructure, RS extends R["structure"], KR extends keyof RS>(url: `/api/${string}`, options: {
|
|
52
|
+
requireParams: M;
|
|
53
|
+
responseStructure: R;
|
|
54
|
+
}, ...middlewares: Array<(req: MyRequest<M["dataType"], T, K>, res: MyResponse<R["dataType"], RS, KR>, next: NextFunction) => any>): Express.Express;
|
|
55
|
+
delete<M extends requireParamsStructure, T extends M["structure"], K extends keyof T, R extends responseStructure, RS extends R["structure"], KR extends keyof RS>(url: `/api/${string}`, options: {
|
|
56
|
+
requireParams: M;
|
|
57
|
+
responseStructure: R;
|
|
58
|
+
}, ...middlewares: Array<(req: MyRequest<M["dataType"], T, K>, res: MyResponse<R["dataType"], RS, KR>, next: NextFunction) => any>): Express.Express;
|
|
59
|
+
generateStaticRouteFile(): void;
|
|
60
|
+
}
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fs_1 = __importDefault(require("fs"));
|
|
7
|
+
const myExpressRouteList = [];
|
|
8
|
+
class MyExpress {
|
|
9
|
+
constructor(app, outputPath) {
|
|
10
|
+
this.express = app;
|
|
11
|
+
this.outputPath = outputPath;
|
|
12
|
+
}
|
|
13
|
+
rootMethod(method, url, options, ...middlewares) {
|
|
14
|
+
myExpressRouteList.push({
|
|
15
|
+
method: method,
|
|
16
|
+
url: url,
|
|
17
|
+
requireParams: options.requireParams,
|
|
18
|
+
responseStructure: options.responseStructure,
|
|
19
|
+
});
|
|
20
|
+
return this.express[method](url, (req, res, next) => {
|
|
21
|
+
return next();
|
|
22
|
+
}, ...middlewares);
|
|
23
|
+
}
|
|
24
|
+
get(url, options, ...middlewares) {
|
|
25
|
+
return this.rootMethod("get", url, options, ...middlewares);
|
|
26
|
+
}
|
|
27
|
+
post(url, options, ...middlewares) {
|
|
28
|
+
return this.rootMethod("post", url, options, ...middlewares);
|
|
29
|
+
}
|
|
30
|
+
put(url, options, ...middlewares) {
|
|
31
|
+
return this.rootMethod("put", url, options, ...middlewares);
|
|
32
|
+
}
|
|
33
|
+
delete(url, options, ...middlewares) {
|
|
34
|
+
return this.rootMethod("delete", url, options, ...middlewares);
|
|
35
|
+
}
|
|
36
|
+
generateStaticRouteFile() {
|
|
37
|
+
const genRequireParamCode = (data) => {
|
|
38
|
+
let result;
|
|
39
|
+
const resultArray = Object.entries(data["structure"]).map(([key, value]) => ({
|
|
40
|
+
name: key,
|
|
41
|
+
value: value,
|
|
42
|
+
}));
|
|
43
|
+
if (resultArray.length === 0)
|
|
44
|
+
return "";
|
|
45
|
+
result = resultArray
|
|
46
|
+
.map((row) => {
|
|
47
|
+
if (Array.isArray(row.value)) {
|
|
48
|
+
return ` ${row.name} : ${row.value
|
|
49
|
+
.map((vRow) => {
|
|
50
|
+
if (typeof vRow === "string")
|
|
51
|
+
return `"${vRow}"`;
|
|
52
|
+
else if (typeof vRow === "number")
|
|
53
|
+
return `${vRow}`;
|
|
54
|
+
})
|
|
55
|
+
.join(" | ")}`;
|
|
56
|
+
}
|
|
57
|
+
else
|
|
58
|
+
return ` ${row.name} : ${row.value}`;
|
|
59
|
+
})
|
|
60
|
+
.join(",");
|
|
61
|
+
return result;
|
|
62
|
+
};
|
|
63
|
+
const isArray = (data) => {
|
|
64
|
+
if (Object.entries(data["structure"]).length === 0)
|
|
65
|
+
return "";
|
|
66
|
+
else if (data["dataType"] === "arrayObject")
|
|
67
|
+
return "[]";
|
|
68
|
+
else
|
|
69
|
+
return "";
|
|
70
|
+
};
|
|
71
|
+
const genInterfaceString = (data) => {
|
|
72
|
+
let rawString = ``;
|
|
73
|
+
if (data.length === 0)
|
|
74
|
+
return "";
|
|
75
|
+
rawString = data
|
|
76
|
+
.map((row) => {
|
|
77
|
+
return ` "${row.url}" : {
|
|
78
|
+
requireParams : { ${genRequireParamCode(row.requireParams)} }${isArray(row.requireParams)},
|
|
79
|
+
responseStructure : { ${genRequireParamCode(row.responseStructure)} }${isArray(row.requireParams)},
|
|
80
|
+
responseModelInterfaceName : undefined
|
|
81
|
+
} `;
|
|
82
|
+
})
|
|
83
|
+
.join(",");
|
|
84
|
+
return rawString;
|
|
85
|
+
};
|
|
86
|
+
const getRoute = myExpressRouteList.filter((row) => row.method === "get");
|
|
87
|
+
const postRoute = myExpressRouteList.filter((row) => row.method === "post");
|
|
88
|
+
const putRoute = myExpressRouteList.filter((row) => row.method === "put");
|
|
89
|
+
const deleteDelete = myExpressRouteList.filter((row) => row.method === "delete");
|
|
90
|
+
let content = `export interface staticRouteInterface {
|
|
91
|
+
get : { ${genInterfaceString(getRoute)} },
|
|
92
|
+
post : { ${genInterfaceString(postRoute)} },
|
|
93
|
+
put : { ${genInterfaceString(putRoute)} },
|
|
94
|
+
delete : { ${genInterfaceString(deleteDelete)} },
|
|
95
|
+
}`;
|
|
96
|
+
for (let row of this.outputPath) {
|
|
97
|
+
fs_1.default.writeFileSync(row, content);
|
|
98
|
+
}
|
|
99
|
+
// const serverFilePath = path.join(
|
|
100
|
+
// appRootPath.toString(),
|
|
101
|
+
// "../",
|
|
102
|
+
// "my-app",
|
|
103
|
+
// "src",
|
|
104
|
+
// "interfaces",
|
|
105
|
+
// "staticRouteInterface.ts"
|
|
106
|
+
// )
|
|
107
|
+
// const myAppFilePath = path.join(
|
|
108
|
+
// appRootPath.toString(),
|
|
109
|
+
// "src",
|
|
110
|
+
// "interfaces",
|
|
111
|
+
// "staticRouteInterface.ts"
|
|
112
|
+
// )
|
|
113
|
+
// fs.writeFileSync(serverFilePath, content)
|
|
114
|
+
// fs.writeFileSync(myAppFilePath, content)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.default = MyExpress;
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// import hotel_INF from "../../interfaces/hotel_DBInstance"
|
|
3
|
+
// import { hotel_model } from "../../myModel/hotel_model"
|
|
4
|
+
// import MyExpress from "./MyExpress"
|
|
5
|
+
// export function dynamicRoute(myApp: MyExpress, tableName: keyof hotel_INF) {
|
|
6
|
+
// const model = hotel_model.find((row) => row.tableName === tableName)
|
|
7
|
+
// if (model) {
|
|
8
|
+
// const data: { [key: string]: "number" | "string" } = {}
|
|
9
|
+
// for (const row of model.columns) {
|
|
10
|
+
// if (
|
|
11
|
+
// row.DATA_TYPE === "int" ||
|
|
12
|
+
// row.DATA_TYPE === "tinyint" ||
|
|
13
|
+
// row.DATA_TYPE === "float" ||
|
|
14
|
+
// row.DATA_TYPE === "double"
|
|
15
|
+
// )
|
|
16
|
+
// data[row.COLUMN_NAME] = "number"
|
|
17
|
+
// else data[row.COLUMN_NAME] = "string"
|
|
18
|
+
// }
|
|
19
|
+
// myApp.get(`/api/${tableName}`, {
|
|
20
|
+
// requireParams: {
|
|
21
|
+
// dataType: "singleObject",
|
|
22
|
+
// structure: {},
|
|
23
|
+
// },
|
|
24
|
+
// responseStructure: {
|
|
25
|
+
// dataType: "arrayObject",
|
|
26
|
+
// structure: data,
|
|
27
|
+
// },
|
|
28
|
+
// })
|
|
29
|
+
// }
|
|
30
|
+
// }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface staticRouteInterface {
|
|
2
|
+
get: {
|
|
3
|
+
"/api/user": {
|
|
4
|
+
requireParams: {
|
|
5
|
+
date: string;
|
|
6
|
+
};
|
|
7
|
+
responseStructure: {
|
|
8
|
+
amount: number;
|
|
9
|
+
};
|
|
10
|
+
responseModelInterfaceName: undefined;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
post: {};
|
|
14
|
+
put: {};
|
|
15
|
+
delete: {};
|
|
16
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "gg-express",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "npm version patch && tsc",
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@types/axios": "^0.14.0",
|
|
14
|
+
"app-root-path": "^3.1.0",
|
|
15
|
+
"axios": "^1.7.7",
|
|
16
|
+
"express": "^4.21.1",
|
|
17
|
+
"fs": "^0.0.1-security",
|
|
18
|
+
"path": "^0.12.7"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/express": "^5.0.0"
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/GGApi.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { staticRouteInterface } from "./staticRouteInterface"
|
|
2
|
+
import axios from "axios"
|
|
3
|
+
export class GGApi {
|
|
4
|
+
constructor() {}
|
|
5
|
+
|
|
6
|
+
get<T extends keyof staticRouteInterface["get"]>(
|
|
7
|
+
url: T,
|
|
8
|
+
requireParams: staticRouteInterface["get"][T]["requireParams"]
|
|
9
|
+
): Promise<staticRouteInterface["get"][T]["responseStructure"]> {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
axios
|
|
12
|
+
.get(url, { params: requireParams })
|
|
13
|
+
.then((response) => {
|
|
14
|
+
resolve(response.data)
|
|
15
|
+
})
|
|
16
|
+
.catch((error) => {
|
|
17
|
+
reject(error)
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
post<T extends keyof staticRouteInterface["post"]>(
|
|
23
|
+
url: T,
|
|
24
|
+
requireParams: staticRouteInterface["post"][T]["requireParams"]
|
|
25
|
+
): Promise<staticRouteInterface["post"][T]["responseStructure"]> {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
axios
|
|
28
|
+
.post(url, requireParams)
|
|
29
|
+
.then((response) => {
|
|
30
|
+
resolve(response.data)
|
|
31
|
+
})
|
|
32
|
+
.catch((error) => {
|
|
33
|
+
reject(error)
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
put<T extends keyof staticRouteInterface["put"]>(
|
|
38
|
+
url: T,
|
|
39
|
+
requireParams: staticRouteInterface["put"][T]["requireParams"]
|
|
40
|
+
): Promise<staticRouteInterface["put"][T]["responseStructure"]> {
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
axios
|
|
43
|
+
.put(url, requireParams)
|
|
44
|
+
.then((response) => {
|
|
45
|
+
resolve(response.data)
|
|
46
|
+
})
|
|
47
|
+
.catch((error) => {
|
|
48
|
+
reject(error)
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
delete<T extends keyof staticRouteInterface["delete"]>(
|
|
53
|
+
url: T,
|
|
54
|
+
requireParams: staticRouteInterface["delete"][T]["requireParams"]
|
|
55
|
+
): Promise<staticRouteInterface["delete"][T]["responseStructure"]> {
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
axios
|
|
58
|
+
.delete(url, requireParams)
|
|
59
|
+
.then((response) => {
|
|
60
|
+
resolve(response.data)
|
|
61
|
+
})
|
|
62
|
+
.catch((error) => {
|
|
63
|
+
reject(error)
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
}
|
package/src/GGExpress.ts
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import appRootPath from "app-root-path"
|
|
2
|
+
import { NextFunction, Request, Response } from "express"
|
|
3
|
+
import Express from "express-serve-static-core"
|
|
4
|
+
import fs from "fs"
|
|
5
|
+
import path from "path"
|
|
6
|
+
type Unarray<T> = T extends (infer U)[] ? U : T;
|
|
7
|
+
type AsConstArray<T extends readonly any[]> = [...T]
|
|
8
|
+
type paramType =
|
|
9
|
+
| "number"
|
|
10
|
+
| "string"
|
|
11
|
+
| AsConstArray<Array<string>>
|
|
12
|
+
| AsConstArray<Array<number>>
|
|
13
|
+
interface responseStructure {
|
|
14
|
+
dataType: "singleObject" | "arrayObject"
|
|
15
|
+
structure: {
|
|
16
|
+
[key: string]: paramType
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface requireParamsStructure {
|
|
21
|
+
dataType: "singleObject" | "arrayObject"
|
|
22
|
+
structure: {
|
|
23
|
+
[key: string]: paramType
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
type numberOrString<T> = T extends "number"
|
|
27
|
+
? number
|
|
28
|
+
: T extends "string"
|
|
29
|
+
? string
|
|
30
|
+
: T extends string[]
|
|
31
|
+
? Unarray<T>
|
|
32
|
+
: T extends number[]
|
|
33
|
+
? Unarray<T>
|
|
34
|
+
: "error-type"
|
|
35
|
+
|
|
36
|
+
type singleOrArrayObject<
|
|
37
|
+
DT extends requireParamsStructure["dataType"],
|
|
38
|
+
T extends requireParamsStructure["structure"],
|
|
39
|
+
K extends keyof T
|
|
40
|
+
> = DT extends "arrayObject"
|
|
41
|
+
? { data: { [Key in K]: numberOrString<T[Key]> }[] }
|
|
42
|
+
: { data: { [Key in K]: numberOrString<T[Key]> } }
|
|
43
|
+
|
|
44
|
+
type MyRequest<
|
|
45
|
+
DT extends requireParamsStructure["dataType"],
|
|
46
|
+
T extends requireParamsStructure["structure"],
|
|
47
|
+
K extends keyof T
|
|
48
|
+
> = Request<
|
|
49
|
+
{},
|
|
50
|
+
{},
|
|
51
|
+
singleOrArrayObject<DT, T, K>,
|
|
52
|
+
singleOrArrayObject<DT, T, K>,
|
|
53
|
+
{}
|
|
54
|
+
>
|
|
55
|
+
|
|
56
|
+
type MyResponse<
|
|
57
|
+
DT extends responseStructure["dataType"],
|
|
58
|
+
T extends responseStructure["structure"],
|
|
59
|
+
K extends keyof T
|
|
60
|
+
> = Response<{
|
|
61
|
+
status: "SUCCESS" | "ERROR"
|
|
62
|
+
message: string
|
|
63
|
+
data: DT extends "arrayObject"
|
|
64
|
+
? Array<{ [key in K]: numberOrString<T[key]> }>
|
|
65
|
+
: { [key in K]: numberOrString<T[key]> }
|
|
66
|
+
}>
|
|
67
|
+
|
|
68
|
+
const myExpressRouteList: {
|
|
69
|
+
method: "get" | "post" | "put" | "delete"
|
|
70
|
+
url: string
|
|
71
|
+
requireParams: requireParamsStructure
|
|
72
|
+
responseStructure: responseStructure
|
|
73
|
+
}[] = []
|
|
74
|
+
|
|
75
|
+
export default class MyExpress {
|
|
76
|
+
public express: Express.Express
|
|
77
|
+
private outputPath: string[]
|
|
78
|
+
constructor(app: Express.Express, outputPath: string[]) {
|
|
79
|
+
this.express = app
|
|
80
|
+
this.outputPath = outputPath
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private rootMethod<
|
|
84
|
+
M extends requireParamsStructure,
|
|
85
|
+
T extends M["structure"],
|
|
86
|
+
K extends keyof T,
|
|
87
|
+
R extends responseStructure,
|
|
88
|
+
RS extends R["structure"],
|
|
89
|
+
KR extends keyof RS
|
|
90
|
+
>(
|
|
91
|
+
method: "get" | "post" | "put" | "delete",
|
|
92
|
+
url: string,
|
|
93
|
+
options: {
|
|
94
|
+
requireParams: M
|
|
95
|
+
responseStructure: R
|
|
96
|
+
},
|
|
97
|
+
...middlewares: Array<
|
|
98
|
+
(
|
|
99
|
+
req: MyRequest<M["dataType"], T, K>,
|
|
100
|
+
res: MyResponse<R["dataType"], RS, KR>,
|
|
101
|
+
next: NextFunction
|
|
102
|
+
) => any
|
|
103
|
+
>
|
|
104
|
+
) {
|
|
105
|
+
myExpressRouteList.push({
|
|
106
|
+
method: method,
|
|
107
|
+
url: url,
|
|
108
|
+
requireParams: options.requireParams,
|
|
109
|
+
responseStructure: options.responseStructure,
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
return this.express[method](
|
|
113
|
+
url,
|
|
114
|
+
(
|
|
115
|
+
req: MyRequest<M["dataType"], T, K>,
|
|
116
|
+
res: Response,
|
|
117
|
+
next: NextFunction
|
|
118
|
+
) => {
|
|
119
|
+
return next()
|
|
120
|
+
},
|
|
121
|
+
...middlewares
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
get<
|
|
125
|
+
M extends requireParamsStructure,
|
|
126
|
+
T extends M["structure"],
|
|
127
|
+
K extends keyof T,
|
|
128
|
+
R extends responseStructure,
|
|
129
|
+
RS extends R["structure"],
|
|
130
|
+
KR extends keyof RS
|
|
131
|
+
>(
|
|
132
|
+
url: `/api/${string}`,
|
|
133
|
+
options: {
|
|
134
|
+
requireParams: M
|
|
135
|
+
responseStructure: R
|
|
136
|
+
},
|
|
137
|
+
...middlewares: Array<
|
|
138
|
+
(
|
|
139
|
+
req: MyRequest<M["dataType"], T, K>,
|
|
140
|
+
res: MyResponse<R["dataType"], RS, KR>,
|
|
141
|
+
next: NextFunction
|
|
142
|
+
) => any
|
|
143
|
+
>
|
|
144
|
+
) {
|
|
145
|
+
return this.rootMethod("get", url, options, ...middlewares)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
post<
|
|
149
|
+
M extends requireParamsStructure,
|
|
150
|
+
T extends M["structure"],
|
|
151
|
+
K extends keyof T,
|
|
152
|
+
R extends responseStructure,
|
|
153
|
+
RS extends R["structure"],
|
|
154
|
+
KR extends keyof RS
|
|
155
|
+
>(
|
|
156
|
+
url: `/api/${string}`,
|
|
157
|
+
options: {
|
|
158
|
+
requireParams: M
|
|
159
|
+
responseStructure: R
|
|
160
|
+
},
|
|
161
|
+
...middlewares: Array<
|
|
162
|
+
(
|
|
163
|
+
req: MyRequest<M["dataType"], T, K>,
|
|
164
|
+
res: MyResponse<R["dataType"], RS, KR>,
|
|
165
|
+
next: NextFunction
|
|
166
|
+
) => any
|
|
167
|
+
>
|
|
168
|
+
) {
|
|
169
|
+
return this.rootMethod("post", url, options, ...middlewares)
|
|
170
|
+
}
|
|
171
|
+
put<
|
|
172
|
+
M extends requireParamsStructure,
|
|
173
|
+
T extends M["structure"],
|
|
174
|
+
K extends keyof T,
|
|
175
|
+
R extends responseStructure,
|
|
176
|
+
RS extends R["structure"],
|
|
177
|
+
KR extends keyof RS
|
|
178
|
+
>(
|
|
179
|
+
url: `/api/${string}`,
|
|
180
|
+
options: {
|
|
181
|
+
requireParams: M
|
|
182
|
+
responseStructure: R
|
|
183
|
+
},
|
|
184
|
+
...middlewares: Array<
|
|
185
|
+
(
|
|
186
|
+
req: MyRequest<M["dataType"], T, K>,
|
|
187
|
+
res: MyResponse<R["dataType"], RS, KR>,
|
|
188
|
+
next: NextFunction
|
|
189
|
+
) => any
|
|
190
|
+
>
|
|
191
|
+
) {
|
|
192
|
+
return this.rootMethod("put", url, options, ...middlewares)
|
|
193
|
+
}
|
|
194
|
+
delete<
|
|
195
|
+
M extends requireParamsStructure,
|
|
196
|
+
T extends M["structure"],
|
|
197
|
+
K extends keyof T,
|
|
198
|
+
R extends responseStructure,
|
|
199
|
+
RS extends R["structure"],
|
|
200
|
+
KR extends keyof RS
|
|
201
|
+
>(
|
|
202
|
+
url: `/api/${string}`,
|
|
203
|
+
options: {
|
|
204
|
+
requireParams: M
|
|
205
|
+
responseStructure: R
|
|
206
|
+
},
|
|
207
|
+
...middlewares: Array<
|
|
208
|
+
(
|
|
209
|
+
req: MyRequest<M["dataType"], T, K>,
|
|
210
|
+
res: MyResponse<R["dataType"], RS, KR>,
|
|
211
|
+
next: NextFunction
|
|
212
|
+
) => any
|
|
213
|
+
>
|
|
214
|
+
) {
|
|
215
|
+
return this.rootMethod("delete", url, options, ...middlewares)
|
|
216
|
+
}
|
|
217
|
+
generateStaticRouteFile() {
|
|
218
|
+
const genRequireParamCode = (data: requireParamsStructure) => {
|
|
219
|
+
let result: string
|
|
220
|
+
const resultArray = Object.entries(data["structure"]).map(
|
|
221
|
+
([key, value]) => ({
|
|
222
|
+
name: key,
|
|
223
|
+
value: value,
|
|
224
|
+
})
|
|
225
|
+
)
|
|
226
|
+
if (resultArray.length === 0) return ""
|
|
227
|
+
|
|
228
|
+
result = resultArray
|
|
229
|
+
.map((row) => {
|
|
230
|
+
if (Array.isArray(row.value)) {
|
|
231
|
+
return ` ${row.name} : ${row.value
|
|
232
|
+
.map((vRow) => {
|
|
233
|
+
if (typeof vRow === "string") return `"${vRow}"`
|
|
234
|
+
else if (typeof vRow === "number") return `${vRow}`
|
|
235
|
+
})
|
|
236
|
+
.join(" | ")}`
|
|
237
|
+
} else return ` ${row.name} : ${row.value}`
|
|
238
|
+
})
|
|
239
|
+
.join(",")
|
|
240
|
+
|
|
241
|
+
return result
|
|
242
|
+
}
|
|
243
|
+
const isArray = (data: requireParamsStructure) => {
|
|
244
|
+
if (Object.entries(data["structure"]).length === 0) return ""
|
|
245
|
+
else if (data["dataType"] === "arrayObject") return "[]"
|
|
246
|
+
else return ""
|
|
247
|
+
}
|
|
248
|
+
const genInterfaceString = (data: typeof myExpressRouteList) => {
|
|
249
|
+
let rawString = ``
|
|
250
|
+
if (data.length === 0) return ""
|
|
251
|
+
rawString = data
|
|
252
|
+
.map((row) => {
|
|
253
|
+
return ` "${row.url}" : {
|
|
254
|
+
requireParams : { ${genRequireParamCode(
|
|
255
|
+
row.requireParams
|
|
256
|
+
)} }${isArray(row.requireParams)},
|
|
257
|
+
responseStructure : { ${genRequireParamCode(
|
|
258
|
+
row.responseStructure
|
|
259
|
+
)} }${isArray(row.requireParams)},
|
|
260
|
+
responseModelInterfaceName : undefined
|
|
261
|
+
} `
|
|
262
|
+
})
|
|
263
|
+
.join(",")
|
|
264
|
+
return rawString
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const getRoute = myExpressRouteList.filter((row) => row.method === "get")
|
|
268
|
+
const postRoute = myExpressRouteList.filter((row) => row.method === "post")
|
|
269
|
+
const putRoute = myExpressRouteList.filter((row) => row.method === "put")
|
|
270
|
+
const deleteDelete = myExpressRouteList.filter(
|
|
271
|
+
(row) => row.method === "delete"
|
|
272
|
+
)
|
|
273
|
+
let content = `export interface staticRouteInterface {
|
|
274
|
+
get : { ${genInterfaceString(getRoute)} },
|
|
275
|
+
post : { ${genInterfaceString(postRoute)} },
|
|
276
|
+
put : { ${genInterfaceString(putRoute)} },
|
|
277
|
+
delete : { ${genInterfaceString(deleteDelete)} },
|
|
278
|
+
}`
|
|
279
|
+
|
|
280
|
+
for (let row of this.outputPath) {
|
|
281
|
+
fs.writeFileSync(row, content)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// const serverFilePath = path.join(
|
|
285
|
+
// appRootPath.toString(),
|
|
286
|
+
// "../",
|
|
287
|
+
// "my-app",
|
|
288
|
+
// "src",
|
|
289
|
+
// "interfaces",
|
|
290
|
+
// "staticRouteInterface.ts"
|
|
291
|
+
// )
|
|
292
|
+
// const myAppFilePath = path.join(
|
|
293
|
+
// appRootPath.toString(),
|
|
294
|
+
// "src",
|
|
295
|
+
// "interfaces",
|
|
296
|
+
// "staticRouteInterface.ts"
|
|
297
|
+
// )
|
|
298
|
+
|
|
299
|
+
// fs.writeFileSync(serverFilePath, content)
|
|
300
|
+
// fs.writeFileSync(myAppFilePath, content)
|
|
301
|
+
}
|
|
302
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// import hotel_INF from "../../interfaces/hotel_DBInstance"
|
|
2
|
+
// import { hotel_model } from "../../myModel/hotel_model"
|
|
3
|
+
// import MyExpress from "./MyExpress"
|
|
4
|
+
|
|
5
|
+
// export function dynamicRoute(myApp: MyExpress, tableName: keyof hotel_INF) {
|
|
6
|
+
// const model = hotel_model.find((row) => row.tableName === tableName)
|
|
7
|
+
// if (model) {
|
|
8
|
+
// const data: { [key: string]: "number" | "string" } = {}
|
|
9
|
+
|
|
10
|
+
// for (const row of model.columns) {
|
|
11
|
+
// if (
|
|
12
|
+
// row.DATA_TYPE === "int" ||
|
|
13
|
+
// row.DATA_TYPE === "tinyint" ||
|
|
14
|
+
// row.DATA_TYPE === "float" ||
|
|
15
|
+
// row.DATA_TYPE === "double"
|
|
16
|
+
// )
|
|
17
|
+
// data[row.COLUMN_NAME] = "number"
|
|
18
|
+
// else data[row.COLUMN_NAME] = "string"
|
|
19
|
+
// }
|
|
20
|
+
|
|
21
|
+
// myApp.get(`/api/${tableName}`, {
|
|
22
|
+
// requireParams: {
|
|
23
|
+
// dataType: "singleObject",
|
|
24
|
+
// structure: {},
|
|
25
|
+
// },
|
|
26
|
+
// responseStructure: {
|
|
27
|
+
// dataType: "arrayObject",
|
|
28
|
+
// structure: data,
|
|
29
|
+
// },
|
|
30
|
+
// })
|
|
31
|
+
// }
|
|
32
|
+
// }
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES6", // Specify ECMAScript target version
|
|
4
|
+
"module": "commonjs", // Use CommonJS module system for Node.js
|
|
5
|
+
"declaration": true, // Generates corresponding '.d.ts' files
|
|
6
|
+
"outDir": "./dist", // Redirect output structure to 'dist' folder
|
|
7
|
+
"strict": true, // Enable strict type-checking options
|
|
8
|
+
"esModuleInterop": true, // Ensures ES6 module compatibility
|
|
9
|
+
"skipLibCheck": true, // Skips type checking of declaration files
|
|
10
|
+
"forceConsistentCasingInFileNames": true
|
|
11
|
+
},
|
|
12
|
+
"include": ["src/**/*"], // Include all TypeScript files in 'src'
|
|
13
|
+
"exclude": ["node_modules", "dist"]
|
|
14
|
+
}
|