gg-express 1.0.24 → 1.0.26
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 +3 -3
- package/dist/GGExpress.js +22 -5
- package/dist/apiConnector.js +0 -21
- package/dist/staticRouteInterface.d.ts +17 -1
- package/dist/test.js +43 -2
- package/package.json +1 -1
- package/readme.md +91 -0
- package/src/GGApi.ts +4 -3
- package/src/GGExpress.ts +21 -9
- package/src/apiConnector.ts +4 -11
- package/src/staticRouteInterface.ts +8 -2
- package/src/test.ts +48 -2
package/dist/GGApi.js
CHANGED
|
@@ -70,10 +70,10 @@ function run() {
|
|
|
70
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
71
71
|
const api = new GGApi();
|
|
72
72
|
const data = yield api.post("/api/item", {
|
|
73
|
-
parameter: {
|
|
74
|
-
bankID: 1,
|
|
75
|
-
},
|
|
73
|
+
parameter: { bankID: 2 },
|
|
76
74
|
data: [{ id: 2, name: "2" }],
|
|
77
75
|
});
|
|
76
|
+
const data2 = yield api.get("/api/users", {});
|
|
77
|
+
data.data;
|
|
78
78
|
});
|
|
79
79
|
}
|
package/dist/GGExpress.js
CHANGED
|
@@ -49,7 +49,7 @@ class GGExpress {
|
|
|
49
49
|
generateStaticRouteFile() {
|
|
50
50
|
return __awaiter(this, void 0, void 0, function* () {
|
|
51
51
|
const genParamCodeParameter = (data) => {
|
|
52
|
-
let result;
|
|
52
|
+
let result = "";
|
|
53
53
|
const resultArray = Object.entries(data["parameter"]).map(([key, value]) => ({
|
|
54
54
|
name: key,
|
|
55
55
|
value: value,
|
|
@@ -74,8 +74,8 @@ class GGExpress {
|
|
|
74
74
|
.join(",");
|
|
75
75
|
return result;
|
|
76
76
|
};
|
|
77
|
-
const
|
|
78
|
-
let result;
|
|
77
|
+
const genDataParamCode = (data) => {
|
|
78
|
+
let result = "";
|
|
79
79
|
const resultArray = Object.entries(data["structure"]).map(([key, value]) => ({
|
|
80
80
|
name: key,
|
|
81
81
|
value: value,
|
|
@@ -114,12 +114,29 @@ class GGExpress {
|
|
|
114
114
|
return "";
|
|
115
115
|
rawString = data
|
|
116
116
|
.map((row) => {
|
|
117
|
+
const parameterCode = genParamCodeParameter(row.requireParams);
|
|
118
|
+
const parameterDataCode = genDataParamCode(row.requireParams);
|
|
119
|
+
const requireParameterCode = () => {
|
|
120
|
+
if (parameterCode === "")
|
|
121
|
+
return "";
|
|
122
|
+
else
|
|
123
|
+
return ` parameter:{${parameterCode}} `;
|
|
124
|
+
};
|
|
125
|
+
const requireDataCode = () => {
|
|
126
|
+
if (parameterDataCode === "")
|
|
127
|
+
return "";
|
|
128
|
+
else
|
|
129
|
+
return ` data:{${parameterDataCode}}${isArray(row.requireParams)}`;
|
|
130
|
+
};
|
|
131
|
+
let requireParamCode = `{${[requireParameterCode(), requireDataCode()]
|
|
132
|
+
.filter((row) => row.length > 0)
|
|
133
|
+
.join(",")}}`;
|
|
117
134
|
return ` "${row.url}" : {
|
|
118
|
-
requireParams :
|
|
135
|
+
requireParams : ${requireParamCode},
|
|
119
136
|
responseStructure : {
|
|
120
137
|
status : "SUCCESS" | "ERROR",
|
|
121
138
|
message : string,
|
|
122
|
-
parameter : {${genParamCodeParameter(row.responseStructure)}}, data : { ${
|
|
139
|
+
parameter : {${genParamCodeParameter(row.responseStructure)}}, data : { ${genDataParamCode(row.responseStructure)} } ${isArray(row.responseStructure)}}
|
|
123
140
|
} `;
|
|
124
141
|
})
|
|
125
142
|
.join(",");
|
package/dist/apiConnector.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -66,15 +57,3 @@ class GGApi {
|
|
|
66
57
|
}
|
|
67
58
|
}
|
|
68
59
|
exports.GGApi = GGApi;
|
|
69
|
-
function run() {
|
|
70
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
-
const api = new GGApi();
|
|
72
|
-
const data = yield api.post("/api/item", {
|
|
73
|
-
parameter: {
|
|
74
|
-
bankID: 1,
|
|
75
|
-
},
|
|
76
|
-
data: [{ id: 2, name: "2" }],
|
|
77
|
-
});
|
|
78
|
-
data.data;
|
|
79
|
-
});
|
|
80
|
-
}
|
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
export interface staticRouteInterface {
|
|
2
|
-
get: {
|
|
2
|
+
get: {
|
|
3
|
+
"/api/users": {
|
|
4
|
+
requireParams: {};
|
|
5
|
+
responseStructure: {
|
|
6
|
+
status: "SUCCESS" | "ERROR";
|
|
7
|
+
message: string;
|
|
8
|
+
parameter: {
|
|
9
|
+
numberOfPeople: number;
|
|
10
|
+
itemName: string;
|
|
11
|
+
};
|
|
12
|
+
data: {
|
|
13
|
+
id: number;
|
|
14
|
+
name: string;
|
|
15
|
+
}[];
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
};
|
|
3
19
|
post: {
|
|
4
20
|
"/api/item": {
|
|
5
21
|
requireParams: {
|
package/dist/test.js
CHANGED
|
@@ -8,15 +8,42 @@ 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", {
|
|
12
|
+
requireParams: {
|
|
13
|
+
parameter: {},
|
|
14
|
+
dataType: "singleObject",
|
|
15
|
+
structure: {},
|
|
16
|
+
},
|
|
17
|
+
responseStructure: {
|
|
18
|
+
parameter: { numberOfPeople: "number", itemName: "string" },
|
|
19
|
+
dataType: "arrayObject",
|
|
20
|
+
structure: {
|
|
21
|
+
id: "number",
|
|
22
|
+
name: "string",
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
}, (req, res, next) => {
|
|
26
|
+
const data = req.body.data.parameter;
|
|
27
|
+
return res.json({
|
|
28
|
+
message: "",
|
|
29
|
+
status: "SUCCESS",
|
|
30
|
+
data: [],
|
|
31
|
+
parameter: {
|
|
32
|
+
itemName: "",
|
|
33
|
+
numberOfPeople: 2,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
});
|
|
11
37
|
ggapp.post("/api/item", {
|
|
12
38
|
requireParams: {
|
|
13
39
|
parameter: {
|
|
14
|
-
|
|
40
|
+
lotNumber: "number",
|
|
15
41
|
},
|
|
16
42
|
dataType: "arrayObject",
|
|
17
43
|
structure: {
|
|
18
44
|
id: "number",
|
|
19
45
|
name: "string",
|
|
46
|
+
price: "number",
|
|
20
47
|
},
|
|
21
48
|
},
|
|
22
49
|
responseStructure: {
|
|
@@ -28,7 +55,21 @@ function run() {
|
|
|
28
55
|
},
|
|
29
56
|
},
|
|
30
57
|
}, (req, res, next) => {
|
|
31
|
-
|
|
58
|
+
// in body or query, you can access only requireParams that you declare at start
|
|
59
|
+
const data = req.body.data;
|
|
60
|
+
/* data structure
|
|
61
|
+
data: {
|
|
62
|
+
parameter: {
|
|
63
|
+
lotNumber: number;
|
|
64
|
+
};
|
|
65
|
+
data: {
|
|
66
|
+
id: number;
|
|
67
|
+
name: string;
|
|
68
|
+
price: number;
|
|
69
|
+
}[];
|
|
70
|
+
}
|
|
71
|
+
*/
|
|
72
|
+
// also res.json(), you can only return structure like responseStructure that you declare at start
|
|
32
73
|
return res.json({
|
|
33
74
|
message: "",
|
|
34
75
|
status: "SUCCESS",
|
package/package.json
CHANGED
package/readme.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# GG-Express
|
|
2
|
+
|
|
3
|
+
this is express wrapper. that force method get,post,put,delete to have strong require req.query and req.body structure and generate apiConnector class file for using in any front end
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
- force express method get,post,put,delete to have strong require and response parameter
|
|
10
|
+
- generate strong static api url class combo with force require parameter and auto cast return type same as backend
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
You can install this package using npm or yarn:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# Using npm
|
|
19
|
+
npm install gg-express
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
use case
|
|
23
|
+
```javascript
|
|
24
|
+
|
|
25
|
+
const app = express()
|
|
26
|
+
|
|
27
|
+
const ggapp = new GGExpress(app,
|
|
28
|
+
[
|
|
29
|
+
'./server/location-for-generating-file', # example path to backend
|
|
30
|
+
'./myapp/location-for-generating-file', # example path to frontend
|
|
31
|
+
])
|
|
32
|
+
|
|
33
|
+
// example force parameter
|
|
34
|
+
ggapp.post(
|
|
35
|
+
"/api/item",
|
|
36
|
+
{
|
|
37
|
+
requireParams: {
|
|
38
|
+
parameter: {
|
|
39
|
+
lotNumber: "number",
|
|
40
|
+
},
|
|
41
|
+
dataType: "arrayObject",
|
|
42
|
+
structure: {
|
|
43
|
+
id: "number",
|
|
44
|
+
name: "string",
|
|
45
|
+
price: "number",
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
responseStructure: {
|
|
49
|
+
parameter: { numberOfPeople: "number", itemName: "string" },
|
|
50
|
+
dataType: "arrayObject",
|
|
51
|
+
structure: {
|
|
52
|
+
id: "number",
|
|
53
|
+
name: "string",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
(req, res, next) => {
|
|
58
|
+
// in body or query, you can access only requireParams that you declare at start
|
|
59
|
+
const data = req.body.data
|
|
60
|
+
//data structure
|
|
61
|
+
/*
|
|
62
|
+
data: {
|
|
63
|
+
parameter: {
|
|
64
|
+
lotNumber: number;
|
|
65
|
+
};
|
|
66
|
+
data: {
|
|
67
|
+
id: number;
|
|
68
|
+
name: string;
|
|
69
|
+
price: number;
|
|
70
|
+
}[];
|
|
71
|
+
}
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
// also res.json, you can access only return like responseStructure that you declare at start
|
|
75
|
+
return res.json({
|
|
76
|
+
message: "",
|
|
77
|
+
status: "SUCCESS",
|
|
78
|
+
data: [],
|
|
79
|
+
parameter: {
|
|
80
|
+
itemName: "",
|
|
81
|
+
numberOfPeople: 2,
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
app.listen(3000, () => {
|
|
88
|
+
ggapp.generateAPIFiles() // this will generate apiConnector class file that you can use in front end
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
```
|
package/src/GGApi.ts
CHANGED
|
@@ -70,9 +70,10 @@ export class GGApi {
|
|
|
70
70
|
async function run() {
|
|
71
71
|
const api = new GGApi()
|
|
72
72
|
const data = await api.post("/api/item", {
|
|
73
|
-
parameter: {
|
|
74
|
-
bankID: 1,
|
|
75
|
-
},
|
|
73
|
+
parameter: { bankID: 2 },
|
|
76
74
|
data: [{ id: 2, name: "2" }],
|
|
77
75
|
})
|
|
76
|
+
|
|
77
|
+
const data2 = await api.get("/api/users", {})
|
|
78
|
+
data.data
|
|
78
79
|
}
|
package/src/GGExpress.ts
CHANGED
|
@@ -246,7 +246,7 @@ export default class GGExpress {
|
|
|
246
246
|
}
|
|
247
247
|
private async generateStaticRouteFile() {
|
|
248
248
|
const genParamCodeParameter = (data: requireParamsStructure) => {
|
|
249
|
-
let result: string
|
|
249
|
+
let result: string = ""
|
|
250
250
|
const resultArray = Object.entries(data["parameter"]).map(
|
|
251
251
|
([key, value]) => ({
|
|
252
252
|
name: key,
|
|
@@ -269,8 +269,8 @@ export default class GGExpress {
|
|
|
269
269
|
.join(",")
|
|
270
270
|
return result
|
|
271
271
|
}
|
|
272
|
-
const
|
|
273
|
-
let result: string
|
|
272
|
+
const genDataParamCode = (data: requireParamsStructure) => {
|
|
273
|
+
let result: string = ""
|
|
274
274
|
const resultArray = Object.entries(data["structure"]).map(
|
|
275
275
|
([key, value]) => ({
|
|
276
276
|
name: key,
|
|
@@ -304,18 +304,30 @@ export default class GGExpress {
|
|
|
304
304
|
if (data.length === 0) return ""
|
|
305
305
|
rawString = data
|
|
306
306
|
.map((row) => {
|
|
307
|
+
const parameterCode = genParamCodeParameter(row.requireParams)
|
|
308
|
+
const parameterDataCode = genDataParamCode(row.requireParams)
|
|
309
|
+
|
|
310
|
+
const requireParameterCode = () => {
|
|
311
|
+
if (parameterCode === "") return ""
|
|
312
|
+
else return ` parameter:{${parameterCode}} `
|
|
313
|
+
}
|
|
314
|
+
const requireDataCode = () => {
|
|
315
|
+
if (parameterDataCode === "") return ""
|
|
316
|
+
else
|
|
317
|
+
return ` data:{${parameterDataCode}}${isArray(row.requireParams)}`
|
|
318
|
+
}
|
|
319
|
+
let requireParamCode = `{${[requireParameterCode(), requireDataCode()]
|
|
320
|
+
.filter((row) => row.length > 0)
|
|
321
|
+
.join(",")}}`
|
|
322
|
+
|
|
307
323
|
return ` "${row.url}" : {
|
|
308
|
-
requireParams :
|
|
309
|
-
row.requireParams
|
|
310
|
-
)}}, data : { ${genParamCode(row.requireParams)} }${isArray(
|
|
311
|
-
row.requireParams
|
|
312
|
-
)}},
|
|
324
|
+
requireParams : ${requireParamCode},
|
|
313
325
|
responseStructure : {
|
|
314
326
|
status : "SUCCESS" | "ERROR",
|
|
315
327
|
message : string,
|
|
316
328
|
parameter : {${genParamCodeParameter(
|
|
317
329
|
row.responseStructure
|
|
318
|
-
)}}, data : { ${
|
|
330
|
+
)}}, data : { ${genDataParamCode(row.responseStructure)} } ${isArray(
|
|
319
331
|
row.responseStructure
|
|
320
332
|
)}}
|
|
321
333
|
} `
|
package/src/apiConnector.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
import axios from "axios"
|
|
2
3
|
import { staticRouteInterface } from "./staticRouteInterface"
|
|
3
4
|
|
|
4
5
|
export class GGApi {
|
|
@@ -67,13 +68,5 @@ export class GGApi {
|
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
70
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const data = await api.post("/api/item", {
|
|
73
|
-
parameter: {
|
|
74
|
-
bankID: 1,
|
|
75
|
-
},
|
|
76
|
-
data: [{ id: 2, name: "2" }],
|
|
77
|
-
})
|
|
78
|
-
data.data
|
|
79
|
-
}
|
|
71
|
+
|
|
72
|
+
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
export interface staticRouteInterface {
|
|
2
|
-
get : {
|
|
2
|
+
get : { "/api/users" : {
|
|
3
|
+
requireParams : {},
|
|
4
|
+
responseStructure : {
|
|
5
|
+
status : "SUCCESS" | "ERROR",
|
|
6
|
+
message : string,
|
|
7
|
+
parameter : { numberOfPeople : number, itemName : string}, data : { id : number, name : string } []}
|
|
8
|
+
} },
|
|
3
9
|
post : { "/api/item" : {
|
|
4
|
-
requireParams : { parameter
|
|
10
|
+
requireParams : { parameter:{ bankID : number} , data:{ id : number, name : string}[]},
|
|
5
11
|
responseStructure : {
|
|
6
12
|
status : "SUCCESS" | "ERROR",
|
|
7
13
|
message : string,
|
package/src/test.ts
CHANGED
|
@@ -3,17 +3,48 @@ import express, { Express } from "express"
|
|
|
3
3
|
function run() {
|
|
4
4
|
const app = express()
|
|
5
5
|
const ggapp = new GGExpress(app, ["./"])
|
|
6
|
+
ggapp.get(
|
|
7
|
+
"/api/users",
|
|
8
|
+
{
|
|
9
|
+
requireParams: {
|
|
10
|
+
parameter: {},
|
|
11
|
+
dataType: "singleObject",
|
|
12
|
+
structure: {},
|
|
13
|
+
},
|
|
14
|
+
responseStructure: {
|
|
15
|
+
parameter: { numberOfPeople: "number", itemName: "string" },
|
|
16
|
+
dataType: "arrayObject",
|
|
17
|
+
structure: {
|
|
18
|
+
id: "number",
|
|
19
|
+
name: "string",
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
(req, res, next) => {
|
|
24
|
+
const data = req.body.data.parameter
|
|
25
|
+
return res.json({
|
|
26
|
+
message: "",
|
|
27
|
+
status: "SUCCESS",
|
|
28
|
+
data: [],
|
|
29
|
+
parameter: {
|
|
30
|
+
itemName: "",
|
|
31
|
+
numberOfPeople: 2,
|
|
32
|
+
},
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
)
|
|
6
36
|
ggapp.post(
|
|
7
37
|
"/api/item",
|
|
8
38
|
{
|
|
9
39
|
requireParams: {
|
|
10
40
|
parameter: {
|
|
11
|
-
|
|
41
|
+
lotNumber: "number",
|
|
12
42
|
},
|
|
13
43
|
dataType: "arrayObject",
|
|
14
44
|
structure: {
|
|
15
45
|
id: "number",
|
|
16
46
|
name: "string",
|
|
47
|
+
price: "number",
|
|
17
48
|
},
|
|
18
49
|
},
|
|
19
50
|
responseStructure: {
|
|
@@ -26,7 +57,22 @@ function run() {
|
|
|
26
57
|
},
|
|
27
58
|
},
|
|
28
59
|
(req, res, next) => {
|
|
29
|
-
|
|
60
|
+
// in body or query, you can access only requireParams that you declare at start
|
|
61
|
+
const data = req.body.data
|
|
62
|
+
/* data structure
|
|
63
|
+
data: {
|
|
64
|
+
parameter: {
|
|
65
|
+
lotNumber: number;
|
|
66
|
+
};
|
|
67
|
+
data: {
|
|
68
|
+
id: number;
|
|
69
|
+
name: string;
|
|
70
|
+
price: number;
|
|
71
|
+
}[];
|
|
72
|
+
}
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
// also res.json(), you can only return structure like responseStructure that you declare at start
|
|
30
76
|
return res.json({
|
|
31
77
|
message: "",
|
|
32
78
|
status: "SUCCESS",
|