gg-express 1.0.25 → 1.0.27
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 +1 -3
- package/dist/test.js +17 -2
- package/package.json +1 -1
- package/readme.md +109 -0
- package/src/GGApi.ts +1 -3
- package/src/test.ts +18 -2
package/dist/GGApi.js
CHANGED
|
@@ -70,9 +70,7 @@ 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
|
});
|
|
78
76
|
const data2 = yield api.get("/api/users", {});
|
package/dist/test.js
CHANGED
|
@@ -37,12 +37,13 @@ function run() {
|
|
|
37
37
|
ggapp.post("/api/item", {
|
|
38
38
|
requireParams: {
|
|
39
39
|
parameter: {
|
|
40
|
-
|
|
40
|
+
lotNumber: "number",
|
|
41
41
|
},
|
|
42
42
|
dataType: "arrayObject",
|
|
43
43
|
structure: {
|
|
44
44
|
id: "number",
|
|
45
45
|
name: "string",
|
|
46
|
+
price: "number",
|
|
46
47
|
},
|
|
47
48
|
},
|
|
48
49
|
responseStructure: {
|
|
@@ -54,7 +55,21 @@ function run() {
|
|
|
54
55
|
},
|
|
55
56
|
},
|
|
56
57
|
}, (req, res, next) => {
|
|
57
|
-
|
|
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
|
|
58
73
|
return res.json({
|
|
59
74
|
message: "",
|
|
60
75
|
status: "SUCCESS",
|
package/package.json
CHANGED
package/readme.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# GG-Express
|
|
2
|
+
|
|
3
|
+
**GG-Express** is an Express.js wrapper that enforces strong type requirements for `GET`, `POST`, `PUT`, and `DELETE` methods. It ensures that `req.query` and `req.body` follow strict structure definitions and automatically generates an `apiConnector` class file for easy use in front-end development.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Enforces required request (`req.query` and `req.body`) and response parameter types for `GET`, `POST`, `PUT`, and `DELETE` methods in Express.
|
|
8
|
+
- Automatically generates a static API URL class that strongly types parameters and auto-casts return types to match the backend's response structure.
|
|
9
|
+
- Simplifies integrating strong typing between back-end and front-end.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Key Points
|
|
13
|
+
|
|
14
|
+
- Strict Parameter Enforcement: GG-Express ensures that only the declared parameters are accessible in your req.body or req.query.
|
|
15
|
+
|
|
16
|
+
- Type-Safe Responses: You can only return data that follows the predefined responseStructure, improving consistency between backend and frontend.
|
|
17
|
+
|
|
18
|
+
- API Connector Generation: It automatically generates an API connector class file for your front-end, making API calls more reliable and type-safe.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Install the package using either `npm` or `yarn`:
|
|
23
|
+
|
|
24
|
+
### Using npm:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install gg-express
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
You can install this package using npm or yarn:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
# Using npm
|
|
35
|
+
npm install gg-express
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
use case
|
|
39
|
+
```javascript
|
|
40
|
+
|
|
41
|
+
const express = require('express');
|
|
42
|
+
const GGExpress = require('gg-express'); // Import GG-Express
|
|
43
|
+
const app = express();
|
|
44
|
+
|
|
45
|
+
// Initialize GG-Express with backend and frontend file paths
|
|
46
|
+
const ggapp = new GGExpress(app, [
|
|
47
|
+
'./server/location-for-generating-file', // Path to generate backend file
|
|
48
|
+
'./myapp/location-for-generating-file', // Path to generate frontend file
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
// Example of a POST request with enforced parameters
|
|
52
|
+
ggapp.post(
|
|
53
|
+
"/api/item",
|
|
54
|
+
{
|
|
55
|
+
requireParams: {
|
|
56
|
+
parameter: { lotNumber: "number" },
|
|
57
|
+
dataType: "arrayObject",
|
|
58
|
+
structure: {
|
|
59
|
+
id: "number",
|
|
60
|
+
name: "string",
|
|
61
|
+
price: "number",
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
responseStructure: {
|
|
65
|
+
parameter: { numberOfPeople: "number", itemName: "string" },
|
|
66
|
+
dataType: "arrayObject",
|
|
67
|
+
structure: {
|
|
68
|
+
id: "number",
|
|
69
|
+
name: "string",
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
(req, res, next) => {
|
|
74
|
+
// You can only access the required parameters declared above
|
|
75
|
+
const data = req.body.data;
|
|
76
|
+
|
|
77
|
+
// Expected data structure:
|
|
78
|
+
// data: {
|
|
79
|
+
// parameter: {
|
|
80
|
+
// lotNumber: number;
|
|
81
|
+
// };
|
|
82
|
+
// data: [{
|
|
83
|
+
// id: number;
|
|
84
|
+
// name: string;
|
|
85
|
+
// price: number;
|
|
86
|
+
// }];
|
|
87
|
+
// }
|
|
88
|
+
|
|
89
|
+
// Response structure also follows the predefined format
|
|
90
|
+
return res.json({
|
|
91
|
+
message: "",
|
|
92
|
+
status: "SUCCESS",
|
|
93
|
+
data: [],
|
|
94
|
+
parameter: {
|
|
95
|
+
itemName: "",
|
|
96
|
+
numberOfPeople: 2,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
// Start the server and generate the API files
|
|
103
|
+
app.listen(3000, () => {
|
|
104
|
+
ggapp.generateAPIFiles(); // Generates the apiConnector class for front-end use
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
|
package/src/GGApi.ts
CHANGED
package/src/test.ts
CHANGED
|
@@ -38,12 +38,13 @@ function run() {
|
|
|
38
38
|
{
|
|
39
39
|
requireParams: {
|
|
40
40
|
parameter: {
|
|
41
|
-
|
|
41
|
+
lotNumber: "number",
|
|
42
42
|
},
|
|
43
43
|
dataType: "arrayObject",
|
|
44
44
|
structure: {
|
|
45
45
|
id: "number",
|
|
46
46
|
name: "string",
|
|
47
|
+
price: "number",
|
|
47
48
|
},
|
|
48
49
|
},
|
|
49
50
|
responseStructure: {
|
|
@@ -56,7 +57,22 @@ function run() {
|
|
|
56
57
|
},
|
|
57
58
|
},
|
|
58
59
|
(req, res, next) => {
|
|
59
|
-
|
|
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
|
|
60
76
|
return res.json({
|
|
61
77
|
message: "",
|
|
62
78
|
status: "SUCCESS",
|