gg-express 1.0.27 → 1.0.30
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.js +8 -6
- package/dist/GGExpress.js +13 -3
- package/dist/parseQueryJSON.d.ts +2 -0
- package/dist/parseQueryJSON.js +33 -0
- package/dist/staticRouteInterface.d.ts +8 -3
- package/dist/test.js +5 -5
- package/package.json +2 -1
- package/src/GGApi.ts +12 -7
- package/src/GGExpress.ts +21 -3
- package/src/parseQueryJSON.ts +33 -0
- package/src/staticRouteInterface.ts +3 -3
- package/src/test.ts +6 -5
package/dist/GGApi.js
CHANGED
|
@@ -19,7 +19,7 @@ class GGApi {
|
|
|
19
19
|
get(url, requireParams) {
|
|
20
20
|
return new Promise((resolve, reject) => {
|
|
21
21
|
axios_1.default
|
|
22
|
-
.get(url, { params: requireParams })
|
|
22
|
+
.get(url, { params: { data: requireParams } })
|
|
23
23
|
.then((response) => {
|
|
24
24
|
resolve(response.data);
|
|
25
25
|
})
|
|
@@ -69,11 +69,13 @@ exports.GGApi = GGApi;
|
|
|
69
69
|
function run() {
|
|
70
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
71
71
|
const api = new GGApi();
|
|
72
|
-
const data =
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
// const data = await api.post("/api/item", {
|
|
73
|
+
// parameter: { lotNumber: 1223 },
|
|
74
|
+
// data: [],
|
|
75
|
+
// })
|
|
76
|
+
const data2 = yield api.get(("http://localhost:3002" + "/api/users/id"), {
|
|
77
|
+
data: [{ id: 2 }, { id: 3 }, { id: 20 }],
|
|
75
78
|
});
|
|
76
|
-
const data2 = yield api.get("/api/users", {});
|
|
77
|
-
data.data;
|
|
78
79
|
});
|
|
79
80
|
}
|
|
81
|
+
run();
|
package/dist/GGExpress.js
CHANGED
|
@@ -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 chalk_1 = __importDefault(require("chalk"));
|
|
17
18
|
const myExpressRouteList = [];
|
|
18
19
|
class GGExpress {
|
|
19
20
|
constructor(app, outputPath) {
|
|
@@ -112,7 +113,16 @@ class GGExpress {
|
|
|
112
113
|
let rawString = ``;
|
|
113
114
|
if (data.length === 0)
|
|
114
115
|
return "";
|
|
115
|
-
|
|
116
|
+
const filterDuplicateUrlOut = [];
|
|
117
|
+
for (let row of data) {
|
|
118
|
+
const temp = filterDuplicateUrlOut.filter((fRow) => fRow.method === row.method && fRow.url === row.url);
|
|
119
|
+
if (temp.length <= 0)
|
|
120
|
+
filterDuplicateUrlOut.push(row);
|
|
121
|
+
else {
|
|
122
|
+
console.log(chalk_1.default.yellow(`!!! found duplicate url ${row.method} : ${row.url} -- system will keep only first one it found.`));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
rawString = filterDuplicateUrlOut
|
|
116
126
|
.map((row) => {
|
|
117
127
|
const parameterCode = genParamCodeParameter(row.requireParams);
|
|
118
128
|
const parameterDataCode = genDataParamCode(row.requireParams);
|
|
@@ -162,7 +172,7 @@ class GGExpress {
|
|
|
162
172
|
return __awaiter(this, void 0, void 0, function* () {
|
|
163
173
|
// const code = await fs.readFileSync("./GGApi.ts")
|
|
164
174
|
const code = `
|
|
165
|
-
|
|
175
|
+
import axios from "axios"
|
|
166
176
|
import { staticRouteInterface } from "./staticRouteInterface"
|
|
167
177
|
|
|
168
178
|
export class GGApi {
|
|
@@ -174,7 +184,7 @@ export class GGApi {
|
|
|
174
184
|
): Promise<staticRouteInterface["get"][T]["responseStructure"]> {
|
|
175
185
|
return new Promise((resolve, reject) => {
|
|
176
186
|
axios
|
|
177
|
-
.get(url as string, { params: requireParams })
|
|
187
|
+
.get(url as string, { params: { data: requireParams } })
|
|
178
188
|
.then((response) => {
|
|
179
189
|
resolve(response.data)
|
|
180
190
|
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseQueryJSON = parseQueryJSON;
|
|
4
|
+
function parseQueryJSON(req, res, next) {
|
|
5
|
+
// Check if there's any query parameter to parse
|
|
6
|
+
if (req.query) {
|
|
7
|
+
for (const key in req.query) {
|
|
8
|
+
if (Array.isArray(req.query[key])) {
|
|
9
|
+
// If the query param is an array of stringified JSON, parse each item
|
|
10
|
+
req.query[key] = req.query[key].map((item) => {
|
|
11
|
+
try {
|
|
12
|
+
return JSON.parse(item);
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
console.error(`Failed to parse query parameter: ${item}`);
|
|
16
|
+
return item; // Return the original if parsing fails
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
else if (typeof req.query[key] === "string") {
|
|
21
|
+
// If the query param is a stringified JSON, attempt to parse it
|
|
22
|
+
try {
|
|
23
|
+
req.query[key] = JSON.parse(req.query[key]);
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
console.error(`Failed to parse query parameter: ${req.query[key]}`);
|
|
27
|
+
// Keep the original string if parsing fails
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
next(); // Call next middleware in the stack
|
|
33
|
+
}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export interface staticRouteInterface {
|
|
2
2
|
get: {
|
|
3
|
-
"/api/users": {
|
|
4
|
-
requireParams: {
|
|
3
|
+
"/api/users/id": {
|
|
4
|
+
requireParams: {
|
|
5
|
+
data: {
|
|
6
|
+
id: number;
|
|
7
|
+
}[];
|
|
8
|
+
};
|
|
5
9
|
responseStructure: {
|
|
6
10
|
status: "SUCCESS" | "ERROR";
|
|
7
11
|
message: string;
|
|
@@ -20,11 +24,12 @@ export interface staticRouteInterface {
|
|
|
20
24
|
"/api/item": {
|
|
21
25
|
requireParams: {
|
|
22
26
|
parameter: {
|
|
23
|
-
|
|
27
|
+
lotNumber: number;
|
|
24
28
|
};
|
|
25
29
|
data: {
|
|
26
30
|
id: number;
|
|
27
31
|
name: string;
|
|
32
|
+
price: number;
|
|
28
33
|
}[];
|
|
29
34
|
};
|
|
30
35
|
responseStructure: {
|
package/dist/test.js
CHANGED
|
@@ -8,11 +8,11 @@ const express_1 = __importDefault(require("express"));
|
|
|
8
8
|
function run() {
|
|
9
9
|
const app = (0, express_1.default)();
|
|
10
10
|
const ggapp = new GGExpress_1.default(app, ["./"]);
|
|
11
|
-
ggapp.get("/api/users", {
|
|
11
|
+
ggapp.get("/api/users/id", {
|
|
12
12
|
requireParams: {
|
|
13
13
|
parameter: {},
|
|
14
|
-
dataType: "
|
|
15
|
-
structure: {},
|
|
14
|
+
dataType: "arrayObject",
|
|
15
|
+
structure: { id: "number" },
|
|
16
16
|
},
|
|
17
17
|
responseStructure: {
|
|
18
18
|
parameter: { numberOfPeople: "number", itemName: "string" },
|
|
@@ -23,7 +23,7 @@ function run() {
|
|
|
23
23
|
},
|
|
24
24
|
},
|
|
25
25
|
}, (req, res, next) => {
|
|
26
|
-
|
|
26
|
+
console.log(req.query);
|
|
27
27
|
return res.json({
|
|
28
28
|
message: "",
|
|
29
29
|
status: "SUCCESS",
|
|
@@ -80,7 +80,7 @@ function run() {
|
|
|
80
80
|
},
|
|
81
81
|
});
|
|
82
82
|
});
|
|
83
|
-
app.listen(
|
|
83
|
+
app.listen(3002, () => {
|
|
84
84
|
ggapp.generateAPIFiles();
|
|
85
85
|
});
|
|
86
86
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gg-express",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.30",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/GGExpress.js",
|
|
6
6
|
"scripts": {
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"@types/axios": "^0.14.0",
|
|
14
14
|
"app-root-path": "^3.1.0",
|
|
15
15
|
"axios": "^1.7.7",
|
|
16
|
+
"chalk": "^4.1.2",
|
|
16
17
|
"express": "^4.21.1",
|
|
17
18
|
"fs": "^0.0.1-security",
|
|
18
19
|
"path": "^0.12.7"
|
package/src/GGApi.ts
CHANGED
|
@@ -10,7 +10,7 @@ export class GGApi {
|
|
|
10
10
|
): Promise<staticRouteInterface["get"][T]["responseStructure"]> {
|
|
11
11
|
return new Promise((resolve, reject) => {
|
|
12
12
|
axios
|
|
13
|
-
.get(url as string, { params: requireParams })
|
|
13
|
+
.get(url as string, { params: { data: requireParams } })
|
|
14
14
|
.then((response) => {
|
|
15
15
|
resolve(response.data)
|
|
16
16
|
})
|
|
@@ -69,11 +69,16 @@ export class GGApi {
|
|
|
69
69
|
|
|
70
70
|
async function run() {
|
|
71
71
|
const api = new GGApi()
|
|
72
|
-
const data = await api.post("/api/item", {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
})
|
|
72
|
+
// const data = await api.post("/api/item", {
|
|
73
|
+
// parameter: { lotNumber: 1223 },
|
|
74
|
+
// data: [],
|
|
75
|
+
// })
|
|
76
76
|
|
|
77
|
-
const data2 = await api.get(
|
|
78
|
-
|
|
77
|
+
const data2 = await api.get(
|
|
78
|
+
("http://localhost:3002" + "/api/users/id") as any,
|
|
79
|
+
{
|
|
80
|
+
data: [{ id: 2 }, { id: 3 }, { id: 20 }],
|
|
81
|
+
}
|
|
82
|
+
)
|
|
79
83
|
}
|
|
84
|
+
run()
|
package/src/GGExpress.ts
CHANGED
|
@@ -3,6 +3,8 @@ import { NextFunction, Request, Response } from "express"
|
|
|
3
3
|
import Express from "express-serve-static-core"
|
|
4
4
|
import fs from "fs"
|
|
5
5
|
import path from "path"
|
|
6
|
+
import chalk from "chalk"
|
|
7
|
+
|
|
6
8
|
// export { GGApi } from "./GGApi"
|
|
7
9
|
type Unarray<T> = T extends (infer U)[] ? U : T
|
|
8
10
|
type AsConstArray<T extends readonly any[]> = [...T]
|
|
@@ -302,7 +304,23 @@ export default class GGExpress {
|
|
|
302
304
|
const genInterfaceString = (data: typeof myExpressRouteList) => {
|
|
303
305
|
let rawString = ``
|
|
304
306
|
if (data.length === 0) return ""
|
|
305
|
-
|
|
307
|
+
|
|
308
|
+
const filterDuplicateUrlOut: typeof data = []
|
|
309
|
+
for (let row of data) {
|
|
310
|
+
const temp = filterDuplicateUrlOut.filter(
|
|
311
|
+
(fRow) => fRow.method === row.method && fRow.url === row.url
|
|
312
|
+
)
|
|
313
|
+
if (temp.length <= 0) filterDuplicateUrlOut.push(row)
|
|
314
|
+
else {
|
|
315
|
+
console.log(
|
|
316
|
+
chalk.yellow(
|
|
317
|
+
`!!! found duplicate url ${row.method} : ${row.url} -- system will keep only first one it found.`
|
|
318
|
+
)
|
|
319
|
+
)
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
rawString = filterDuplicateUrlOut
|
|
306
324
|
.map((row) => {
|
|
307
325
|
const parameterCode = genParamCodeParameter(row.requireParams)
|
|
308
326
|
const parameterDataCode = genDataParamCode(row.requireParams)
|
|
@@ -357,7 +375,7 @@ export default class GGExpress {
|
|
|
357
375
|
private async generateGGApi(filePathWithFileName: string) {
|
|
358
376
|
// const code = await fs.readFileSync("./GGApi.ts")
|
|
359
377
|
const code = `
|
|
360
|
-
|
|
378
|
+
import axios from "axios"
|
|
361
379
|
import { staticRouteInterface } from "./staticRouteInterface"
|
|
362
380
|
|
|
363
381
|
export class GGApi {
|
|
@@ -369,7 +387,7 @@ export class GGApi {
|
|
|
369
387
|
): Promise<staticRouteInterface["get"][T]["responseStructure"]> {
|
|
370
388
|
return new Promise((resolve, reject) => {
|
|
371
389
|
axios
|
|
372
|
-
.get(url as string, { params: requireParams })
|
|
390
|
+
.get(url as string, { params: { data: requireParams } })
|
|
373
391
|
.then((response) => {
|
|
374
392
|
resolve(response.data)
|
|
375
393
|
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from "express"
|
|
2
|
+
|
|
3
|
+
export function parseQueryJSON(
|
|
4
|
+
req: Request,
|
|
5
|
+
res: Response,
|
|
6
|
+
next: NextFunction
|
|
7
|
+
) {
|
|
8
|
+
// Check if there's any query parameter to parse
|
|
9
|
+
if (req.query) {
|
|
10
|
+
for (const key in req.query) {
|
|
11
|
+
if (Array.isArray(req.query[key])) {
|
|
12
|
+
// If the query param is an array of stringified JSON, parse each item
|
|
13
|
+
req.query[key] = req.query[key].map((item) => {
|
|
14
|
+
try {
|
|
15
|
+
return JSON.parse(item as any)
|
|
16
|
+
} catch (err) {
|
|
17
|
+
console.error(`Failed to parse query parameter: ${item}`)
|
|
18
|
+
return item // Return the original if parsing fails
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
} else if (typeof req.query[key] === "string") {
|
|
22
|
+
// If the query param is a stringified JSON, attempt to parse it
|
|
23
|
+
try {
|
|
24
|
+
req.query[key] = JSON.parse(req.query[key])
|
|
25
|
+
} catch (err) {
|
|
26
|
+
console.error(`Failed to parse query parameter: ${req.query[key]}`)
|
|
27
|
+
// Keep the original string if parsing fails
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
next() // Call next middleware in the stack
|
|
33
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
export interface staticRouteInterface {
|
|
2
|
-
get : { "/api/users" : {
|
|
3
|
-
requireParams : {},
|
|
2
|
+
get : { "/api/users/id" : {
|
|
3
|
+
requireParams : { data:{ id : number}[]},
|
|
4
4
|
responseStructure : {
|
|
5
5
|
status : "SUCCESS" | "ERROR",
|
|
6
6
|
message : string,
|
|
7
7
|
parameter : { numberOfPeople : number, itemName : string}, data : { id : number, name : string } []}
|
|
8
8
|
} },
|
|
9
9
|
post : { "/api/item" : {
|
|
10
|
-
requireParams : { parameter:{
|
|
10
|
+
requireParams : { parameter:{ lotNumber : number} , data:{ id : number, name : string, price : number}[]},
|
|
11
11
|
responseStructure : {
|
|
12
12
|
status : "SUCCESS" | "ERROR",
|
|
13
13
|
message : string,
|
package/src/test.ts
CHANGED
|
@@ -4,12 +4,12 @@ function run() {
|
|
|
4
4
|
const app = express()
|
|
5
5
|
const ggapp = new GGExpress(app, ["./"])
|
|
6
6
|
ggapp.get(
|
|
7
|
-
"/api/users",
|
|
7
|
+
"/api/users/id",
|
|
8
8
|
{
|
|
9
9
|
requireParams: {
|
|
10
10
|
parameter: {},
|
|
11
|
-
dataType: "
|
|
12
|
-
structure: {},
|
|
11
|
+
dataType: "arrayObject",
|
|
12
|
+
structure: { id: "number" },
|
|
13
13
|
},
|
|
14
14
|
responseStructure: {
|
|
15
15
|
parameter: { numberOfPeople: "number", itemName: "string" },
|
|
@@ -21,7 +21,7 @@ function run() {
|
|
|
21
21
|
},
|
|
22
22
|
},
|
|
23
23
|
(req, res, next) => {
|
|
24
|
-
|
|
24
|
+
console.log(req.query)
|
|
25
25
|
return res.json({
|
|
26
26
|
message: "",
|
|
27
27
|
status: "SUCCESS",
|
|
@@ -33,6 +33,7 @@ function run() {
|
|
|
33
33
|
})
|
|
34
34
|
}
|
|
35
35
|
)
|
|
36
|
+
|
|
36
37
|
ggapp.post(
|
|
37
38
|
"/api/item",
|
|
38
39
|
{
|
|
@@ -84,7 +85,7 @@ function run() {
|
|
|
84
85
|
})
|
|
85
86
|
}
|
|
86
87
|
)
|
|
87
|
-
app.listen(
|
|
88
|
+
app.listen(3002, () => {
|
|
88
89
|
ggapp.generateAPIFiles()
|
|
89
90
|
})
|
|
90
91
|
}
|