gg-express 1.0.43 → 1.0.51

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/.gitlab-ci.yml ADDED
@@ -0,0 +1,13 @@
1
+ image: node:16
2
+
3
+ stages:
4
+ - publish
5
+
6
+ publish:
7
+ stage: publish
8
+ script:
9
+ - npm install
10
+ - npm install typescript --save-dev
11
+ - npm run build
12
+ - echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
13
+ - npm publish
package/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "name": "gg-express",
3
- "version": "1.0.43",
3
+ "version": "1.0.51",
4
4
  "description": "",
5
5
  "main": "dist/GGExpress.js",
6
6
  "scripts": {
7
- "build": "npm version patch && tsc",
7
+ "build": "tsc && npm version patch --no-git-tag-version",
8
8
  "test": "echo \"Error: no test specified\" && exit 1"
9
9
  },
10
- "author": "",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://gitlab.com/tin.sittiyot/gg-express.git"
13
+ },
14
+ "author": "goomgum",
11
15
  "license": "ISC",
12
16
  "dependencies": {
13
17
  "@types/axios": "^0.14.0",
@@ -19,6 +23,17 @@
19
23
  "path": "^0.12.7"
20
24
  },
21
25
  "devDependencies": {
22
- "@types/express": "^5.0.0"
23
- }
26
+ "@types/express": "^5.0.0",
27
+ "typescript": "^5.6.3"
28
+ },
29
+ "keywords": [
30
+ "express",
31
+ "typescript",
32
+ "api",
33
+ "wrapper",
34
+ "type-safe",
35
+ "backend",
36
+ "frontend",
37
+ "api-connector"
38
+ ]
24
39
  }
package/readme.md CHANGED
@@ -1,39 +1,37 @@
1
- # GG-Express
1
+ # 🚀 GG-Express
2
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.
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
4
 
5
- ## Features
5
+ ## Features
6
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.
7
+ - **Strict Type Enforcement**: Ensures that `req.query` and `req.body` follow the declared types.
8
+ - **Auto-Generated API Connector**: Automatically creates a static API class for strongly-typed parameters.
9
+ - **Seamless Front-End Integration**: Simplifies the connection between the back-end and front-end by enforcing consistency.
10
10
 
11
+ ## 🔑 Key Points
11
12
 
12
- ## Key Points
13
+ - **Strict Parameter Enforcement**: GG-Express ensures only the declared parameters are accessible in `req.body` or `req.query`.
14
+ - **Type-Safe Responses**: Ensures that the backend responds with a structure that matches front-end expectations.
15
+ - **API Connector Generation**: It automatically generates a type-safe API connector class file for the front-end.
13
16
 
14
- - Strict Parameter Enforcement: GG-Express ensures that only the declared parameters are accessible in your req.body or req.query.
17
+ ## 🛠 Installation
15
18
 
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
19
  ```bash
22
20
  npm install gg-express
23
21
  ```
24
22
 
25
- use case
26
- ```javascript
23
+ 🧑‍💻 Use Case in Backend
27
24
 
28
- const express = require('express');
29
- const GGExpress = require('gg-express'); // Import GG-Express
30
- const app = express();
25
+ ```typescript
26
+ import express from "express"
27
+ import GGExpress from "gg-express"
28
+ const app = express()
31
29
 
32
30
  // Initialize GG-Express with backend and frontend file paths
33
31
  const ggapp = new GGExpress(app, [
34
- './server/location-for-generating-file', // Path to generate backend file
35
- './myapp/location-for-generating-file', // Path to generate frontend file
36
- ]);
32
+ "./server/output-path-for-apiConnector.ts", // Path to generate apiConnector.ts backend file
33
+ "./myapp/output-path-for-apiConnector.ts", // Path to generate apiConnector.ts frontend file
34
+ ])
37
35
 
38
36
  // Example of a POST request with enforced parameters
39
37
  ggapp.post(
@@ -58,22 +56,10 @@ ggapp.post(
58
56
  },
59
57
  },
60
58
  (req, res, next) => {
61
- // You can only access the required parameters declared above
62
- const data = req.body.data;
63
-
64
- // Expected data structure:
65
- // data: {
66
- // parameter: {
67
- // lotNumber: number;
68
- // };
69
- // data: [{
70
- // id: number;
71
- // name: string;
72
- // price: number;
73
- // }];
74
- // }
75
-
76
- // Response structure also follows the predefined format
59
+ // Access only the required parameters declared above
60
+ const data = req.body.data
61
+
62
+ // Response structure follows the predefined format
77
63
  return res.json({
78
64
  message: "",
79
65
  status: "SUCCESS",
@@ -82,15 +68,49 @@ ggapp.post(
82
68
  itemName: "",
83
69
  numberOfPeople: 2,
84
70
  },
85
- });
71
+ })
86
72
  }
87
- );
73
+ )
88
74
 
89
75
  // Start the server and generate the API files
90
76
  app.listen(3000, () => {
91
- ggapp.generateAPIFiles(); // Generates the apiConnector class for front-end use
92
- });
77
+ ggapp.generateAPIFiles() // Generates the apiConnector class for front-end use
78
+ })
79
+ ```
93
80
 
81
+ 📲 Use Case in Frontend
82
+ • The apiConnector.ts file will be automatically generated by the GGExpress class you configured in the backend
94
83
 
84
+ ```typescript
85
+ import GGApi from "apiConnector.ts"
86
+
87
+ const api = new GGApi()
88
+ const response = await api.post("/api/item", {
89
+ parameter: {
90
+ lotNumber: 2,
91
+ },
92
+ data: [
93
+ {
94
+ id: 1032,
95
+ name: "machete",
96
+ price: 4599,
97
+ },
98
+ ],
99
+ })
100
+ console.log(response.data)
101
+ // Expected data structure:
102
+ // parameter: { numberOfPeople: number, itemName: string },
103
+ // structure: {
104
+ // id: number,
105
+ // name: string,
106
+ // }[],
107
+ // }
95
108
  ```
96
109
 
110
+ ## 🔑 Keywords
111
+
112
+ - Express.js
113
+ - TypeScript
114
+ - API wrapper
115
+ - Type-safe API
116
+ - Backend-frontend integration
@@ -1,33 +0,0 @@
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
- }