gg-express 1.0.10 → 1.0.17

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 CHANGED
@@ -22,7 +22,7 @@ class GGApi {
22
22
  post(url, requireParams) {
23
23
  return new Promise((resolve, reject) => {
24
24
  axios_1.default
25
- .post(url, requireParams)
25
+ .post(url, { data: requireParams })
26
26
  .then((response) => {
27
27
  resolve(response.data);
28
28
  })
@@ -34,7 +34,7 @@ class GGApi {
34
34
  put(url, requireParams) {
35
35
  return new Promise((resolve, reject) => {
36
36
  axios_1.default
37
- .put(url, requireParams)
37
+ .put(url, { data: requireParams })
38
38
  .then((response) => {
39
39
  resolve(response.data);
40
40
  })
@@ -46,7 +46,7 @@ class GGApi {
46
46
  delete(url, requireParams) {
47
47
  return new Promise((resolve, reject) => {
48
48
  axios_1.default
49
- .delete(url, requireParams)
49
+ .delete(url, { data: requireParams })
50
50
  .then((response) => {
51
51
  resolve(response.data);
52
52
  })
@@ -57,3 +57,10 @@ class GGApi {
57
57
  }
58
58
  }
59
59
  exports.GGApi = GGApi;
60
+ const api = new GGApi();
61
+ api.post("/api/item", {
62
+ parameter: {
63
+ bankID: 1,
64
+ },
65
+ data: [{ id: 2, name: "2" }],
66
+ });
@@ -4,28 +4,40 @@ type Unarray<T> = T extends (infer U)[] ? U : T;
4
4
  type AsConstArray<T extends readonly any[]> = [...T];
5
5
  type paramType = "number" | "string" | AsConstArray<Array<string>> | AsConstArray<Array<number>>;
6
6
  interface responseStructure {
7
+ parameter: {
8
+ [key: string]: "number" | "string";
9
+ };
7
10
  dataType: "singleObject" | "arrayObject";
8
11
  structure: {
9
12
  [key: string]: paramType;
10
13
  };
11
14
  }
12
15
  interface requireParamsStructure {
16
+ parameter: {
17
+ [key: string]: "number" | "string";
18
+ };
13
19
  dataType: "singleObject" | "arrayObject";
14
20
  structure: {
15
21
  [key: string]: paramType;
16
22
  };
17
23
  }
18
24
  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" ? {
25
+ type singleOrArrayObject<DT extends requireParamsStructure["dataType"], T extends requireParamsStructure["structure"], K extends keyof T, P extends requireParamsStructure["parameter"]> = DT extends "arrayObject" ? {
26
+ parameter: {
27
+ [Key in keyof P]: numberOrString<P[Key]>;
28
+ };
20
29
  data: {
21
30
  [Key in K]: numberOrString<T[Key]>;
22
31
  }[];
23
32
  } : {
33
+ parameter: {
34
+ [Key in keyof P]: numberOrString<P[Key]>;
35
+ };
24
36
  data: {
25
37
  [Key in K]: numberOrString<T[Key]>;
26
38
  };
27
39
  };
28
- type MyRequest<DT extends requireParamsStructure["dataType"], T extends requireParamsStructure["structure"], K extends keyof T> = Request<{}, {}, singleOrArrayObject<DT, T, K>, singleOrArrayObject<DT, T, K>, {}>;
40
+ type MyRequest<DT extends requireParamsStructure["dataType"], T extends requireParamsStructure["structure"], K extends keyof T, P extends requireParamsStructure["parameter"]> = Request<{}, {}, singleOrArrayObject<DT, T, K, P>, singleOrArrayObject<DT, T, K, P>, {}>;
29
41
  type MyResponse<DT extends responseStructure["dataType"], T extends responseStructure["structure"], K extends keyof T> = Response<{
30
42
  status: "SUCCESS" | "ERROR";
31
43
  message: string;
@@ -40,22 +52,22 @@ export default class GGExpress {
40
52
  private outputPath;
41
53
  constructor(app: Express.Express, outputPath: string[]);
42
54
  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: {
55
+ get<M extends requireParamsStructure, T extends M["structure"], K extends keyof T, R extends responseStructure, RS extends R["structure"], KR extends keyof RS, P extends M["parameter"]>(url: `/api/${string}`, options: {
44
56
  requireParams: M;
45
57
  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: {
58
+ }, ...middlewares: Array<(req: MyRequest<M["dataType"], T, K, P>, res: MyResponse<R["dataType"], RS, KR>, next: NextFunction) => any>): Express.Express;
59
+ post<M extends requireParamsStructure, T extends M["structure"], K extends keyof T, R extends responseStructure, RS extends R["structure"], KR extends keyof RS, P extends M["parameter"]>(url: `/api/${string}`, options: {
48
60
  requireParams: M;
49
61
  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: {
62
+ }, ...middlewares: Array<(req: MyRequest<M["dataType"], T, K, P>, res: MyResponse<R["dataType"], RS, KR>, next: NextFunction) => any>): Express.Express;
63
+ put<M extends requireParamsStructure, T extends M["structure"], K extends keyof T, R extends responseStructure, RS extends R["structure"], KR extends keyof RS, P extends M["parameter"]>(url: `/api/${string}`, options: {
52
64
  requireParams: M;
53
65
  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: {
66
+ }, ...middlewares: Array<(req: MyRequest<M["dataType"], T, K, P>, res: MyResponse<R["dataType"], RS, KR>, next: NextFunction) => any>): Express.Express;
67
+ delete<M extends requireParamsStructure, T extends M["structure"], K extends keyof T, R extends responseStructure, RS extends R["structure"], KR extends keyof RS, P extends M["parameter"]>(url: `/api/${string}`, options: {
56
68
  requireParams: M;
57
69
  responseStructure: R;
58
- }, ...middlewares: Array<(req: MyRequest<M["dataType"], T, K>, res: MyResponse<R["dataType"], RS, KR>, next: NextFunction) => any>): Express.Express;
70
+ }, ...middlewares: Array<(req: MyRequest<M["dataType"], T, K, P>, res: MyResponse<R["dataType"], RS, KR>, next: NextFunction) => any>): Express.Express;
59
71
  generateAPIFiles(): void;
60
72
  private generateStaticRouteFile;
61
73
  private generateGGApi;
package/dist/GGExpress.js CHANGED
@@ -38,6 +38,32 @@ class GGExpress {
38
38
  this.generateStaticRouteFile();
39
39
  }
40
40
  generateStaticRouteFile() {
41
+ const genParamCodeParameter = (data) => {
42
+ let result;
43
+ const resultArray = Object.entries(data["parameter"]).map(([key, value]) => ({
44
+ name: key,
45
+ value: value,
46
+ }));
47
+ if (resultArray.length === 0)
48
+ return "";
49
+ result = resultArray
50
+ .map((row) => {
51
+ if (Array.isArray(row.value)) {
52
+ return ` ${row.name} : ${row.value
53
+ .map((vRow) => {
54
+ if (typeof vRow === "string")
55
+ return `"${vRow}"`;
56
+ else if (typeof vRow === "number")
57
+ return `${vRow}`;
58
+ })
59
+ .join(" | ")}`;
60
+ }
61
+ else
62
+ return ` ${row.name} : ${row.value}`;
63
+ })
64
+ .join(",");
65
+ return result;
66
+ };
41
67
  const genParamCode = (data) => {
42
68
  let result;
43
69
  const resultArray = Object.entries(data["structure"]).map(([key, value]) => ({
@@ -79,9 +105,8 @@ class GGExpress {
79
105
  rawString = data
80
106
  .map((row) => {
81
107
  return ` "${row.url}" : {
82
- requireParams : { ${genParamCode(row.requireParams)} }${isArray(row.requireParams)},
83
- responseStructure : { ${genParamCode(row.responseStructure)} }${isArray(row.responseStructure)},
84
- responseModelInterfaceName : undefined
108
+ requireParams : { parameter : {${genParamCodeParameter(row.requireParams)}}, data : { ${genParamCode(row.requireParams)} }${isArray(row.requireParams)}},
109
+ responseStructure : { parameter : {${genParamCodeParameter(row.responseStructure)}}, data : { ${genParamCode(row.responseStructure)} } ${isArray(row.responseStructure)}}
85
110
  } `;
86
111
  })
87
112
  .join(",");
@@ -98,30 +123,14 @@ class GGExpress {
98
123
  delete : { ${genInterfaceString(deleteDelete)} },
99
124
  }`;
100
125
  for (let row of this.outputPath) {
101
- fs_1.default.writeFileSync(path_1.default.join(row, 'staticRouteInterface.ts'), content);
102
- this.generateGGApi(path_1.default.join(row, 'apiConnector.ts'));
126
+ fs_1.default.writeFileSync(path_1.default.join(row, "staticRouteInterface.ts"), content);
127
+ this.generateGGApi(path_1.default.join(row, "apiConnector.ts"));
103
128
  }
104
- // const serverFilePath = path.join(
105
- // appRootPath.toString(),
106
- // "../",
107
- // "my-app",
108
- // "src",
109
- // "interfaces",
110
- // "staticRouteInterface.ts"
111
- // )
112
- // const myAppFilePath = path.join(
113
- // appRootPath.toString(),
114
- // "src",
115
- // "interfaces",
116
- // "staticRouteInterface.ts"
117
- // )
118
- // fs.writeFileSync(serverFilePath, content)
119
- // fs.writeFileSync(myAppFilePath, content)
120
129
  }
121
130
  generateGGApi(filePathWithFileName) {
122
131
  let code = `
123
- import { staticRouteInterface } from "./staticRouteInterface"
124
- import axios from "axios"
132
+ import axios from "axios"
133
+ import { staticRouteInterface } from "./staticRouteInterface"
125
134
 
126
135
  export class GGApi {
127
136
  constructor() {}
@@ -148,7 +157,7 @@ export class GGApi {
148
157
  ): Promise<staticRouteInterface["post"][T]["responseStructure"]> {
149
158
  return new Promise((resolve, reject) => {
150
159
  axios
151
- .post(url, requireParams)
160
+ .post(url, { data: requireParams })
152
161
  .then((response) => {
153
162
  resolve(response.data)
154
163
  })
@@ -163,7 +172,7 @@ export class GGApi {
163
172
  ): Promise<staticRouteInterface["put"][T]["responseStructure"]> {
164
173
  return new Promise((resolve, reject) => {
165
174
  axios
166
- .put(url, requireParams)
175
+ .put(url, { data: requireParams })
167
176
  .then((response) => {
168
177
  resolve(response.data)
169
178
  })
@@ -178,7 +187,7 @@ export class GGApi {
178
187
  ): Promise<staticRouteInterface["delete"][T]["responseStructure"]> {
179
188
  return new Promise((resolve, reject) => {
180
189
  axios
181
- .delete(url, requireParams)
190
+ .delete(url, { data: requireParams })
182
191
  .then((response) => {
183
192
  resolve(response.data)
184
193
  })
@@ -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
+ }
@@ -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, { data: 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, { data: 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, { data: requireParams })
50
+ .then((response) => {
51
+ resolve(response.data);
52
+ })
53
+ .catch((error) => {
54
+ reject(error);
55
+ });
56
+ });
57
+ }
58
+ }
59
+ exports.GGApi = GGApi;
@@ -1,16 +1,22 @@
1
1
  export interface staticRouteInterface {
2
- get: {
3
- "/api/user": {
2
+ get: {};
3
+ post: {
4
+ "/api/item": {
4
5
  requireParams: {
5
- date: string;
6
+ parameter: {
7
+ bankID: number;
8
+ };
9
+ data: {
10
+ id: number;
11
+ name: string;
12
+ }[];
6
13
  };
7
14
  responseStructure: {
8
- amount: number;
15
+ parameter: {};
16
+ data: {};
9
17
  };
10
- responseModelInterfaceName: undefined;
11
18
  };
12
19
  };
13
- post: {};
14
20
  put: {};
15
21
  delete: {};
16
22
  }
package/dist/test.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/test.js ADDED
@@ -0,0 +1,34 @@
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 GGExpress_1 = __importDefault(require("./GGExpress"));
7
+ const express_1 = __importDefault(require("express"));
8
+ function run() {
9
+ const app = (0, express_1.default)();
10
+ const ggapp = new GGExpress_1.default(app, ["./"]);
11
+ ggapp.post("/api/item", {
12
+ requireParams: {
13
+ parameter: {
14
+ bankID: "number",
15
+ },
16
+ dataType: "arrayObject",
17
+ structure: {
18
+ id: "number",
19
+ name: "string",
20
+ },
21
+ },
22
+ responseStructure: {
23
+ parameter: {},
24
+ dataType: "arrayObject",
25
+ structure: {},
26
+ },
27
+ }, (req, res, next) => {
28
+ req.body.parameter;
29
+ });
30
+ app.listen(3000, () => {
31
+ ggapp.generateAPIFiles();
32
+ });
33
+ }
34
+ run();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-express",
3
- "version": "1.0.10",
3
+ "version": "1.0.17",
4
4
  "description": "",
5
5
  "main": "dist/GGExpress.js",
6
6
  "scripts": {
package/src/GGApi.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { staticRouteInterface } from "./staticRouteInterface"
2
1
  import axios from "axios"
2
+ import { staticRouteInterface } from "./staticRouteInterface"
3
3
 
4
4
  export class GGApi {
5
5
  constructor() {}
@@ -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, { params: requireParams })
13
+ .get(url as string, { params: requireParams })
14
14
  .then((response) => {
15
15
  resolve(response.data)
16
16
  })
@@ -26,7 +26,7 @@ export class GGApi {
26
26
  ): Promise<staticRouteInterface["post"][T]["responseStructure"]> {
27
27
  return new Promise((resolve, reject) => {
28
28
  axios
29
- .post(url, requireParams)
29
+ .post(url as string, { data: requireParams })
30
30
  .then((response) => {
31
31
  resolve(response.data)
32
32
  })
@@ -41,7 +41,7 @@ export class GGApi {
41
41
  ): Promise<staticRouteInterface["put"][T]["responseStructure"]> {
42
42
  return new Promise((resolve, reject) => {
43
43
  axios
44
- .put(url, requireParams)
44
+ .put(url as string, { data: requireParams })
45
45
  .then((response) => {
46
46
  resolve(response.data)
47
47
  })
@@ -56,7 +56,7 @@ export class GGApi {
56
56
  ): Promise<staticRouteInterface["delete"][T]["responseStructure"]> {
57
57
  return new Promise((resolve, reject) => {
58
58
  axios
59
- .delete(url, requireParams)
59
+ .delete(url as string, { data: requireParams })
60
60
  .then((response) => {
61
61
  resolve(response.data)
62
62
  })
@@ -66,3 +66,11 @@ export class GGApi {
66
66
  })
67
67
  }
68
68
  }
69
+
70
+ const api = new GGApi()
71
+ api.post("/api/item", {
72
+ parameter: {
73
+ bankID: 1,
74
+ },
75
+ data: [{ id: 2, name: "2" }],
76
+ })
package/src/GGExpress.ts CHANGED
@@ -4,7 +4,7 @@ import Express from "express-serve-static-core"
4
4
  import fs from "fs"
5
5
  import path from "path"
6
6
  // export { GGApi } from "./GGApi"
7
- type Unarray<T> = T extends (infer U)[] ? U : T;
7
+ type Unarray<T> = T extends (infer U)[] ? U : T
8
8
  type AsConstArray<T extends readonly any[]> = [...T]
9
9
  type paramType =
10
10
  | "number"
@@ -12,6 +12,7 @@ type paramType =
12
12
  | AsConstArray<Array<string>>
13
13
  | AsConstArray<Array<number>>
14
14
  interface responseStructure {
15
+ parameter: { [key: string]: "number" | "string" }
15
16
  dataType: "singleObject" | "arrayObject"
16
17
  structure: {
17
18
  [key: string]: paramType
@@ -19,6 +20,7 @@ interface responseStructure {
19
20
  }
20
21
 
21
22
  interface requireParamsStructure {
23
+ parameter: { [key: string]: "number" | "string" }
22
24
  dataType: "singleObject" | "arrayObject"
23
25
  structure: {
24
26
  [key: string]: paramType
@@ -37,20 +39,28 @@ type numberOrString<T> = T extends "number"
37
39
  type singleOrArrayObject<
38
40
  DT extends requireParamsStructure["dataType"],
39
41
  T extends requireParamsStructure["structure"],
40
- K extends keyof T
42
+ K extends keyof T,
43
+ P extends requireParamsStructure["parameter"]
41
44
  > = DT extends "arrayObject"
42
- ? { data: { [Key in K]: numberOrString<T[Key]> }[] }
43
- : { data: { [Key in K]: numberOrString<T[Key]> } }
45
+ ? {
46
+ parameter: { [Key in keyof P]: numberOrString<P[Key]> }
47
+ data: { [Key in K]: numberOrString<T[Key]> }[]
48
+ }
49
+ : {
50
+ parameter: { [Key in keyof P]: numberOrString<P[Key]> }
51
+ data: { [Key in K]: numberOrString<T[Key]> }
52
+ }
44
53
 
45
54
  type MyRequest<
46
55
  DT extends requireParamsStructure["dataType"],
47
56
  T extends requireParamsStructure["structure"],
48
- K extends keyof T
57
+ K extends keyof T,
58
+ P extends requireParamsStructure["parameter"]
49
59
  > = Request<
50
60
  {},
51
61
  {},
52
- singleOrArrayObject<DT, T, K>,
53
- singleOrArrayObject<DT, T, K>,
62
+ singleOrArrayObject<DT, T, K, P>,
63
+ singleOrArrayObject<DT, T, K, P>,
54
64
  {}
55
65
  >
56
66
 
@@ -87,7 +97,8 @@ export default class GGExpress {
87
97
  K extends keyof T,
88
98
  R extends responseStructure,
89
99
  RS extends R["structure"],
90
- KR extends keyof RS
100
+ KR extends keyof RS,
101
+ P extends M["parameter"]
91
102
  >(
92
103
  method: "get" | "post" | "put" | "delete",
93
104
  url: string,
@@ -97,7 +108,7 @@ export default class GGExpress {
97
108
  },
98
109
  ...middlewares: Array<
99
110
  (
100
- req: MyRequest<M["dataType"], T, K>,
111
+ req: MyRequest<M["dataType"], T, K, P>,
101
112
  res: MyResponse<R["dataType"], RS, KR>,
102
113
  next: NextFunction
103
114
  ) => any
@@ -113,7 +124,7 @@ export default class GGExpress {
113
124
  return this.express[method](
114
125
  url,
115
126
  (
116
- req: MyRequest<M["dataType"], T, K>,
127
+ req: MyRequest<M["dataType"], T, K, P>,
117
128
  res: Response,
118
129
  next: NextFunction
119
130
  ) => {
@@ -128,7 +139,8 @@ export default class GGExpress {
128
139
  K extends keyof T,
129
140
  R extends responseStructure,
130
141
  RS extends R["structure"],
131
- KR extends keyof RS
142
+ KR extends keyof RS,
143
+ P extends M["parameter"]
132
144
  >(
133
145
  url: `/api/${string}`,
134
146
  options: {
@@ -137,7 +149,7 @@ export default class GGExpress {
137
149
  },
138
150
  ...middlewares: Array<
139
151
  (
140
- req: MyRequest<M["dataType"], T, K>,
152
+ req: MyRequest<M["dataType"], T, K, P>,
141
153
  res: MyResponse<R["dataType"], RS, KR>,
142
154
  next: NextFunction
143
155
  ) => any
@@ -152,7 +164,8 @@ export default class GGExpress {
152
164
  K extends keyof T,
153
165
  R extends responseStructure,
154
166
  RS extends R["structure"],
155
- KR extends keyof RS
167
+ KR extends keyof RS,
168
+ P extends M["parameter"]
156
169
  >(
157
170
  url: `/api/${string}`,
158
171
  options: {
@@ -161,7 +174,7 @@ export default class GGExpress {
161
174
  },
162
175
  ...middlewares: Array<
163
176
  (
164
- req: MyRequest<M["dataType"], T, K>,
177
+ req: MyRequest<M["dataType"], T, K, P>,
165
178
  res: MyResponse<R["dataType"], RS, KR>,
166
179
  next: NextFunction
167
180
  ) => any
@@ -175,7 +188,8 @@ export default class GGExpress {
175
188
  K extends keyof T,
176
189
  R extends responseStructure,
177
190
  RS extends R["structure"],
178
- KR extends keyof RS
191
+ KR extends keyof RS,
192
+ P extends M["parameter"]
179
193
  >(
180
194
  url: `/api/${string}`,
181
195
  options: {
@@ -184,7 +198,7 @@ export default class GGExpress {
184
198
  },
185
199
  ...middlewares: Array<
186
200
  (
187
- req: MyRequest<M["dataType"], T, K>,
201
+ req: MyRequest<M["dataType"], T, K, P>,
188
202
  res: MyResponse<R["dataType"], RS, KR>,
189
203
  next: NextFunction
190
204
  ) => any
@@ -198,7 +212,8 @@ export default class GGExpress {
198
212
  K extends keyof T,
199
213
  R extends responseStructure,
200
214
  RS extends R["structure"],
201
- KR extends keyof RS
215
+ KR extends keyof RS,
216
+ P extends M["parameter"]
202
217
  >(
203
218
  url: `/api/${string}`,
204
219
  options: {
@@ -207,7 +222,7 @@ export default class GGExpress {
207
222
  },
208
223
  ...middlewares: Array<
209
224
  (
210
- req: MyRequest<M["dataType"], T, K>,
225
+ req: MyRequest<M["dataType"], T, K, P>,
211
226
  res: MyResponse<R["dataType"], RS, KR>,
212
227
  next: NextFunction
213
228
  ) => any
@@ -219,6 +234,30 @@ export default class GGExpress {
219
234
  this.generateStaticRouteFile()
220
235
  }
221
236
  private generateStaticRouteFile() {
237
+ const genParamCodeParameter = (data: requireParamsStructure) => {
238
+ let result: string
239
+ const resultArray = Object.entries(data["parameter"]).map(
240
+ ([key, value]) => ({
241
+ name: key,
242
+ value: value,
243
+ })
244
+ )
245
+ if (resultArray.length === 0) return ""
246
+
247
+ result = resultArray
248
+ .map((row) => {
249
+ if (Array.isArray(row.value)) {
250
+ return ` ${row.name} : ${row.value
251
+ .map((vRow) => {
252
+ if (typeof vRow === "string") return `"${vRow}"`
253
+ else if (typeof vRow === "number") return `${vRow}`
254
+ })
255
+ .join(" | ")}`
256
+ } else return ` ${row.name} : ${row.value}`
257
+ })
258
+ .join(",")
259
+ return result
260
+ }
222
261
  const genParamCode = (data: requireParamsStructure) => {
223
262
  let result: string
224
263
  const resultArray = Object.entries(data["structure"]).map(
@@ -241,9 +280,9 @@ export default class GGExpress {
241
280
  } else return ` ${row.name} : ${row.value}`
242
281
  })
243
282
  .join(",")
244
-
245
283
  return result
246
284
  }
285
+
247
286
  const isArray = (data: requireParamsStructure) => {
248
287
  if (Object.entries(data["structure"]).length === 0) return ""
249
288
  else if (data["dataType"] === "arrayObject") return "[]"
@@ -255,13 +294,16 @@ export default class GGExpress {
255
294
  rawString = data
256
295
  .map((row) => {
257
296
  return ` "${row.url}" : {
258
- requireParams : { ${genParamCode(
297
+ requireParams : { parameter : {${genParamCodeParameter(
259
298
  row.requireParams
260
- )} }${isArray(row.requireParams)},
261
- responseStructure : { ${genParamCode(
299
+ )}}, data : { ${genParamCode(row.requireParams)} }${isArray(
300
+ row.requireParams
301
+ )}},
302
+ responseStructure : { parameter : {${genParamCodeParameter(
303
+ row.responseStructure
304
+ )}}, data : { ${genParamCode(row.responseStructure)} } ${isArray(
262
305
  row.responseStructure
263
- )} }${isArray(row.responseStructure)},
264
- responseModelInterfaceName : undefined
306
+ )}}
265
307
  } `
266
308
  })
267
309
  .join(",")
@@ -282,32 +324,14 @@ export default class GGExpress {
282
324
  }`
283
325
 
284
326
  for (let row of this.outputPath) {
285
- fs.writeFileSync(path.join(row,'staticRouteInterface.ts'), content)
286
- this.generateGGApi(path.join(row,'apiConnector.ts'))
327
+ fs.writeFileSync(path.join(row, "staticRouteInterface.ts"), content)
328
+ this.generateGGApi(path.join(row, "apiConnector.ts"))
287
329
  }
288
-
289
- // const serverFilePath = path.join(
290
- // appRootPath.toString(),
291
- // "../",
292
- // "my-app",
293
- // "src",
294
- // "interfaces",
295
- // "staticRouteInterface.ts"
296
- // )
297
- // const myAppFilePath = path.join(
298
- // appRootPath.toString(),
299
- // "src",
300
- // "interfaces",
301
- // "staticRouteInterface.ts"
302
- // )
303
-
304
- // fs.writeFileSync(serverFilePath, content)
305
- // fs.writeFileSync(myAppFilePath, content)
306
330
  }
307
- private generateGGApi(filePathWithFileName : string) {
331
+ private generateGGApi(filePathWithFileName: string) {
308
332
  let code = `
309
- import { staticRouteInterface } from "./staticRouteInterface"
310
- import axios from "axios"
333
+ import axios from "axios"
334
+ import { staticRouteInterface } from "./staticRouteInterface"
311
335
 
312
336
  export class GGApi {
313
337
  constructor() {}
@@ -334,7 +358,7 @@ export class GGApi {
334
358
  ): Promise<staticRouteInterface["post"][T]["responseStructure"]> {
335
359
  return new Promise((resolve, reject) => {
336
360
  axios
337
- .post(url, requireParams)
361
+ .post(url, { data: requireParams })
338
362
  .then((response) => {
339
363
  resolve(response.data)
340
364
  })
@@ -349,7 +373,7 @@ export class GGApi {
349
373
  ): Promise<staticRouteInterface["put"][T]["responseStructure"]> {
350
374
  return new Promise((resolve, reject) => {
351
375
  axios
352
- .put(url, requireParams)
376
+ .put(url, { data: requireParams })
353
377
  .then((response) => {
354
378
  resolve(response.data)
355
379
  })
@@ -364,7 +388,7 @@ export class GGApi {
364
388
  ): Promise<staticRouteInterface["delete"][T]["responseStructure"]> {
365
389
  return new Promise((resolve, reject) => {
366
390
  axios
367
- .delete(url, requireParams)
391
+ .delete(url, { data: requireParams })
368
392
  .then((response) => {
369
393
  resolve(response.data)
370
394
  })
@@ -376,6 +400,6 @@ export class GGApi {
376
400
  }
377
401
 
378
402
  `
379
- fs.writeFileSync(filePathWithFileName,code)
403
+ fs.writeFileSync(filePathWithFileName, code)
380
404
  }
381
405
  }
@@ -0,0 +1,68 @@
1
+ import axios from "axios"
2
+ import { staticRouteInterface } from "./staticRouteInterface"
3
+
4
+ export class GGApi {
5
+ constructor() {}
6
+
7
+ get<T extends keyof staticRouteInterface["get"]>(
8
+ url: T,
9
+ requireParams: staticRouteInterface["get"][T]["requireParams"]
10
+ ): Promise<staticRouteInterface["get"][T]["responseStructure"]> {
11
+ return new Promise((resolve, reject) => {
12
+ axios
13
+ .get(url, { params: requireParams })
14
+ .then((response) => {
15
+ resolve(response.data)
16
+ })
17
+ .catch((error) => {
18
+ reject(error)
19
+ })
20
+ })
21
+ }
22
+
23
+ post<T extends keyof staticRouteInterface["post"]>(
24
+ url: T,
25
+ requireParams: staticRouteInterface["post"][T]["requireParams"]
26
+ ): Promise<staticRouteInterface["post"][T]["responseStructure"]> {
27
+ return new Promise((resolve, reject) => {
28
+ axios
29
+ .post(url, { data: requireParams })
30
+ .then((response) => {
31
+ resolve(response.data)
32
+ })
33
+ .catch((error) => {
34
+ reject(error)
35
+ })
36
+ })
37
+ }
38
+ put<T extends keyof staticRouteInterface["put"]>(
39
+ url: T,
40
+ requireParams: staticRouteInterface["put"][T]["requireParams"]
41
+ ): Promise<staticRouteInterface["put"][T]["responseStructure"]> {
42
+ return new Promise((resolve, reject) => {
43
+ axios
44
+ .put(url, { data: requireParams })
45
+ .then((response) => {
46
+ resolve(response.data)
47
+ })
48
+ .catch((error) => {
49
+ reject(error)
50
+ })
51
+ })
52
+ }
53
+ delete<T extends keyof staticRouteInterface["delete"]>(
54
+ url: T,
55
+ requireParams: staticRouteInterface["delete"][T]["requireParams"]
56
+ ): Promise<staticRouteInterface["delete"][T]["responseStructure"]> {
57
+ return new Promise((resolve, reject) => {
58
+ axios
59
+ .delete(url, { data: requireParams })
60
+ .then((response) => {
61
+ resolve(response.data)
62
+ })
63
+ .catch((error) => {
64
+ reject(error)
65
+ })
66
+ })
67
+ }
68
+ }
@@ -1,10 +1,9 @@
1
1
  export interface staticRouteInterface {
2
- get : { "/api/user" : {
3
- requireParams : { date : string },
4
- responseStructure : { amount : number },
5
- responseModelInterfaceName : undefined
2
+ get : { },
3
+ post : { "/api/item" : {
4
+ requireParams : { parameter : { bankID : number}, data : { id : number, name : string }[]},
5
+ responseStructure : { parameter : {}, data : { } }
6
6
  } },
7
- post : { },
8
7
  put : { },
9
8
  delete : { },
10
9
  }
package/src/test.ts ADDED
@@ -0,0 +1,33 @@
1
+ import GGExpress from "./GGExpress"
2
+ import express, { Express } from "express"
3
+ function run() {
4
+ const app = express()
5
+ const ggapp = new GGExpress(app, ["./"])
6
+ ggapp.post(
7
+ "/api/item",
8
+ {
9
+ requireParams: {
10
+ parameter: {
11
+ bankID: "number",
12
+ },
13
+ dataType: "arrayObject",
14
+ structure: {
15
+ id: "number",
16
+ name: "string",
17
+ },
18
+ },
19
+ responseStructure: {
20
+ parameter: {},
21
+ dataType: "arrayObject",
22
+ structure: {},
23
+ },
24
+ },
25
+ (req, res, next) => {
26
+ req.body.parameter
27
+ }
28
+ )
29
+ app.listen(3000, () => {
30
+ ggapp.generateAPIFiles()
31
+ })
32
+ }
33
+ run()
@@ -1,32 +0,0 @@
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
- // }