very-express 0.4.2 → 0.4.5

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.
@@ -31,7 +31,7 @@ export default class PassportGoogle {
31
31
  this.config.strategyConfig.options || {
32
32
  clientID: process.env.OAUTH_GOOGLE_CLIENTID || "",
33
33
  clientSecret: process.env.OAUTH_GOOGLE_CLIENTSECRET || "",
34
- callbackURL: `${process.env.APP_HOST}:${process.env.APP_PORT}/auth/google/callback`,
34
+ callbackURL: `${process.env.APP_HOST}/auth/google/callback`,
35
35
  },
36
36
  this.config.strategyConfig.verify
37
37
  ));
@@ -0,0 +1,70 @@
1
+ // {{headerComment}}
2
+
3
+ import mongoose from "mongoose";
4
+ import { Request, Response, NextFunction } from "express";
5
+ import vex from "./../_utils/index.gen";
6
+
7
+ export default class VexDbConnector {
8
+ private mongoUrl: string;
9
+ private sqlUrl: string;
10
+
11
+
12
+ constructor(options:{
13
+ mongoUrl?: string;
14
+ sqlUrl?: string;
15
+ }){
16
+ this.mongoUrl = options.mongoUrl || "";
17
+ this.sqlUrl = options.sqlUrl || "";
18
+ }
19
+
20
+ connectMongo() : mongoose.Connection | void {
21
+ if ( !this.mongoUrl ){
22
+ vex.log.error(`VexDbConnector : MongoUrl is not invalid "${this.mongoUrl}"`);
23
+ return;
24
+ }
25
+ /**
26
+ * @param retryTime time in second
27
+ */
28
+ const connectWithRetry = (retryTime: number = 10) : Promise<void | typeof mongoose> => {
29
+ // vex.log.infoMongo("Connection with retry");
30
+ return mongoose.connect(this.mongoUrl, {
31
+ autoCreate: true,
32
+ connectTimeoutMS: 5000,
33
+ })
34
+ .catch((err) => {
35
+ vex.log.errorMongo(`Failed to Connect DB, Retrying in ${retryTime} seconds.`, err);
36
+ setTimeout(connectWithRetry, retryTime*1000);
37
+ });
38
+ };
39
+ connectWithRetry(10);
40
+
41
+ mongoose.connection.on("open", () => {
42
+ vex.log.infoMongo("MongoDB Connection open : " + this.mongoUrl);
43
+ });
44
+
45
+ mongoose.connection.on("error", (err: any) => {
46
+ vex.log.errorMongo("MongoDB Connection error:", err.message, err);
47
+ });
48
+
49
+ return mongoose.connection;
50
+ }
51
+
52
+ middleware(req: Request, res: Response, next: NextFunction) {
53
+ if (mongoose.connection.readyState !== 1) {
54
+ vex.response.send(res, 503, "Database Connection Down");
55
+ }
56
+ else {
57
+ mongoose.connection.collection("accessLogs").insertOne({
58
+ timestamp: new Date().getTime(),
59
+ ipa: req.socket.remoteAddress || req.headers["x-forwarded-for"],
60
+ method: req.method,
61
+ url: req.url.split("?")[0],
62
+ headers: req.headers,
63
+ // body: req.body,
64
+ query: req.query,
65
+ });
66
+
67
+ next();
68
+ }
69
+ }
70
+ }
@@ -1,4 +1,4 @@
1
- enum responseMessage {
1
+ export enum responseMsg {
2
2
  permissionDeny = "Permission Denied",
3
3
  ok = "OK",
4
4
  notFound = "Not Found",
@@ -10,4 +10,11 @@ enum responseMessage {
10
10
  deleteFail = "Delete Fail",
11
11
  }
12
12
 
13
- export default responseMessage;
13
+ export interface responseObject<T>{
14
+ ret_code : number,
15
+ ret_msg : string,
16
+ ret_time?: number,
17
+ result: T,
18
+ }
19
+
20
+ export default responseMsg;
@@ -1,11 +1,11 @@
1
1
  import logger from "./logger.gen";
2
2
  import response from "./response.gen";
3
- import responseMessage from "../../system/_types/responseMsg.gen";
3
+ import { responseMsg } from "./../_types/responseMsg.gen";
4
4
  import common from "./common.gen";
5
5
 
6
6
  export default {
7
7
  log : logger,
8
8
  response,
9
- responseMsg : responseMessage,
9
+ responseMsg : responseMsg ,
10
10
  common
11
11
  };
@@ -19,10 +19,32 @@ export function error(msg: any, ...optionalParams: any[]): never {
19
19
  process.exit(1);
20
20
  }
21
21
 
22
+ // wrapper
23
+ export function infoMongo(msg: any, ...optionalParams: any[]): void {
24
+ info("[MONGO]", msg, optionalParams);
25
+ }
26
+
27
+ export function errorMongo(msg: any, ...optionalParams: any[]): never {
28
+ error("[MONGO]", msg, optionalParams);
29
+ }
30
+
31
+ export function infoSql(msg: any, ...optionalParams: any[]): void {
32
+ info("[SQL]", msg, optionalParams);
33
+ }
34
+
35
+ export function errorSql(msg: any, ...optionalParams: any[]): never {
36
+ error("[SQL]", msg, optionalParams);
37
+ }
38
+
22
39
  export default {
23
40
  process : processing,
24
41
  ok,
25
42
  info,
26
43
  warn,
27
44
  error,
45
+
46
+ infoMongo,
47
+ errorMongo,
48
+ infoSql,
49
+ errorSql,
28
50
  };
@@ -1,17 +1,12 @@
1
1
  import { Response } from "express";
2
-
3
- export interface responseObject<T>{
4
- ret_code : number,
5
- ret_msg : string,
6
- ret_time?: number,
7
- result: T,
8
- }
2
+ // import { responseObject } from "./../_types/responseMsg.gen";
9
3
 
10
4
  /**
11
5
  * @param res express response object
12
6
  * @param code http status
13
7
  * @param message response data message
14
8
  * @param result response data result
9
+ * @returns express response object
15
10
  */
16
11
  export function send<T = undefined>(
17
12
  res: Response,
@@ -1,5 +1,4 @@
1
- APP_HOST=http://localhost
2
- APP_PORT=3000
1
+ APP_HOST=http://localhost:3000
3
2
  MONGODB_URI=mongodb://localhost:27017/veryExpressDB
4
3
 
5
4
  OAUTH_GOOGLE_CLIENTID=
@@ -6,8 +6,6 @@
6
6
  "dev": "nodemon --watch src --exec ts-node src/server.ts"
7
7
  },
8
8
  "dependencies": {
9
- "binance": "^2.8.9",
10
- "bybit-api": "^3.7.2",
11
9
  "dotenv": "^16.4.4",
12
10
  "express": "^4.18.2",
13
11
  "express-session": "^1.18.0",
@@ -0,0 +1,42 @@
1
+ # Define Json Schema
2
+ ### x-documentConfig:
3
+
4
+ | Fields | Data Type | Options | Description |
5
+ | - | - | - | - |
6
+ | documentName | `string` | - | name of **collection** or **table**, also used at `Model`, camel case recomended. |
7
+ | interfaceName | `string` | - | use to name `endpoint`, `Class`, `Interface`, `File`. |
8
+ | methods | `array<string>` | `get`, `post`, `put`, `patch`, `delete` | available method of REST API. |
9
+
10
+
11
+ ### Properties Mandatory Value:
12
+
13
+ | Fields | Data Type | Required | Description |
14
+ | - | - | - | - |
15
+ | type | `string` | true | [JsonSchema's type field](https://json-schema.org/understanding-json-schema/reference/type) |
16
+ | required | `boolean` | false | api will check field must be defined. |
17
+ | x-foreignKey | `string` | false | `documentName` from other JsonSchema, plan to implemented on [v0.5.0](./RoadMap/v0-5-0.md) |
18
+ <!-- | format | `string` | false | veryExpress did not handle this field | -->
19
+
20
+ ```JSON
21
+ {
22
+ "x-documentConfig": {
23
+ "documentName": "user",
24
+ "interfaceName": "User",
25
+ "methods": [
26
+ "get",
27
+ "post",
28
+ "put",
29
+ "patch",
30
+ "delete"
31
+ ]
32
+ },
33
+ "properties": {
34
+ "invoiceId": {
35
+ "type": "string",
36
+ "format": "uuid",
37
+ "required": false,
38
+ "x-foreignKey": "invoice"
39
+ }
40
+ }
41
+ }
42
+ ```
@@ -0,0 +1,16 @@
1
+ # Development Note
2
+ - "Project" or "The Project" is refer to this package which named "VeryExpress", github repository "[Justin26l/VeryExpress](https://github.com/Justin26l/VeryExpress)", npm package "[very-express](https://www.npmjs.com/package/very-express)".
3
+
4
+ - "Package" is refer to one of the node module, npm package, third-party library.
5
+
6
+
7
+ 1. **TypeScript**
8
+ this project is based on [TypeScript](https://www.typescriptlang.org/).
9
+ we do not use ts-node in development due to file structure of the project,
10
+ you can refer to [package.json](./../package.json) "dev" script to know how to run the project in development.
11
+
12
+ 2. **@vercel/ncc**
13
+ we use this package to compile the project into single file.
14
+ the project must be compiled to run as expected.
15
+
16
+ due to the single file design, working directory of the project will always be at "dist/".
@@ -0,0 +1,30 @@
1
+ # Road Map
2
+
3
+ ## Compatibility
4
+
5
+ - [ ] Dockernize
6
+
7
+ - [x] Generate App
8
+ - [x] generate openapi based on json schema
9
+ - [x] generate controllers based on openapi
10
+ - [x] CRUD API
11
+ - [x] data validation (express-validator)
12
+
13
+ - [ ] Database
14
+ - [x] MongoDB
15
+ - SQL **(ON HOLD)**
16
+
17
+ - [ ] OAuth2
18
+ - [x] google
19
+ - [ ] microsoft
20
+ - [ ] github
21
+
22
+ ## Features
23
+
24
+ - [ ] Schema Definition
25
+ - [ ] App level foreign-key. `x-foreign-key`
26
+ - [ ] DB field encryption. `x-encrypt`
27
+
28
+ - [x] Role Base Access Control
29
+ - [x] API access control
30
+ - different controller data validation rule by role **(ON HOLD)**
@@ -0,0 +1,20 @@
1
+ # v0.5.0 Plan
2
+
3
+ ### App level foreign-key
4
+ - [ ] at JsonSchmea properties item, add attribute `x-foreignKey: "collectionName"`
5
+ use this column value to match with target collectionName's "_id" fields.
6
+ ```JSON
7
+ {
8
+ "invoiceId": {
9
+ "type" : "string",
10
+ "x-foreignKey" : "invoice"
11
+ },
12
+ "orderId": {
13
+ "type" : "string",
14
+ "x-foreignKey" : "order"
15
+ }
16
+ }
17
+ ```
18
+ - [ ] REST API, to request data with foreign-key collection:
19
+ add field name as stringified array into querystring "join"
20
+ example `/myDataApi?join=[invoiceId,orderId]`
@@ -0,0 +1,7 @@
1
+ # v0.6.0 Plan
2
+
3
+ ### dockernize
4
+ - [ ] dockernize
5
+ ### oauth2 support
6
+ - [ ] microsoft
7
+ - [ ] github
@@ -0,0 +1,4 @@
1
+ # v0.7.0 Plan
2
+
3
+ ## app level foreign-key.
4
+ - [ ] db fields encryption
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "very-express",
3
- "version": "0.4.2",
3
+ "version": "0.4.5",
4
4
  "description": "based on json schema and openapi, generate Express RestApi server with mongoDb ",
5
5
  "author": "justin26l",
6
6
  "license": "MIT",
@@ -10,9 +10,8 @@
10
10
  },
11
11
  "scripts": {
12
12
  "start": "node ./dist/index.js",
13
- "dev": "ts-node ./src/cli.ts",
14
- "build": "npm run lint && npm run compile && npm run minify",
15
- "test": "npm run compile && npm run start",
13
+ "dev": "npm run compile && npm run start",
14
+ "build": "npm run lint && npm run compile",
16
15
  "lint": "eslint ./src --ext .ts --fix",
17
16
  "compile": "ncc build ./src/cli.ts -o dist && node ./src/copyTsTemplates.js",
18
17
  "minify": "uglifyjs ./dist/index.js -c -m -o ./dist/index.js"
@@ -1,38 +0,0 @@
1
- // {{headerComment}}
2
-
3
- import mongoose from "mongoose";
4
- import { Request, Response, NextFunction } from "express";
5
- import log from "./../../system/_utils/logger.gen";
6
-
7
- export function connect(mongoUrl:string): mongoose.Connection {
8
- mongoose.connect(mongoUrl, {
9
- autoCreate: true,
10
- });
11
-
12
- mongoose.connection.on("open", () => {
13
- log.ok("MongoDB Connection open : " + mongoUrl);
14
- });
15
-
16
- mongoose.connection.on("error", (err: any) => {
17
- log.error("MongoDB Connection error:", err.message, err);
18
- });
19
-
20
- return mongoose.connection;
21
- }
22
-
23
- export function middleware(req: Request, res: Response, next: NextFunction) {
24
- const db = mongoose.connection;
25
- if (db.readyState !== 1) {
26
- return res.status(503).json({ error: "Database connection issue" });
27
- }
28
- else {
29
- next();
30
- }
31
- }
32
-
33
- const mongo = {
34
- connect,
35
- middleware
36
- };
37
-
38
- export default mongo;
@@ -1,7 +0,0 @@
1
- import * as types from "../../types/types";
2
- export declare function compile(options: {
3
- collectionList: string[];
4
- roleSourceDir: string;
5
- roleOutDir: string;
6
- compilerOptions: types.compilerOptions;
7
- }): Promise<void>;
@@ -1,38 +0,0 @@
1
- // {{headerComment}}
2
-
3
- import mongoose from "mongoose";
4
- import { Request, Response, NextFunction } from "express";
5
- import log from "./../../system/_utils/logger.gen";
6
-
7
- export function connect(mongoUrl:string): mongoose.Connection {
8
- mongoose.connect(mongoUrl, {
9
- autoCreate: true,
10
- });
11
-
12
- mongoose.connection.on("open", () => {
13
- log.ok("MongoDB Connection open : " + mongoUrl);
14
- });
15
-
16
- mongoose.connection.on("error", (err: any) => {
17
- log.error("MongoDB Connection error:", err.message, err);
18
- });
19
-
20
- return mongoose.connection;
21
- }
22
-
23
- export function middleware(req: Request, res: Response, next: NextFunction) {
24
- const db = mongoose.connection;
25
- if (db.readyState !== 1) {
26
- return res.status(503).json({ error: "Database connection issue" });
27
- }
28
- else {
29
- next();
30
- }
31
- }
32
-
33
- const mongo = {
34
- connect,
35
- middleware
36
- };
37
-
38
- export default mongo;