very-express 0.2.8 → 0.3.1
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/.env +3 -0
- package/dist/cli.js +18 -14
- package/dist/generators/controller.template.js +1 -1
- package/dist/generators/controllers.generator.js +6 -7
- package/dist/generators/index.d.ts +1 -1
- package/dist/generators/index.js +1 -1
- package/dist/generators/openapi.generator.js +10 -10
- package/dist/generators/packageJson.generator.js +6 -16
- package/dist/generators/role.generator.d.ts +7 -0
- package/dist/generators/role.generator.js +109 -0
- package/dist/generators/routes/oauth.generator.js +24 -4
- package/dist/generators/routes/routes.generator.d.ts +11 -0
- package/dist/generators/routes/routes.generator.js +53 -0
- package/dist/generators/routes/routes.template.d.ts +11 -0
- package/dist/generators/routes/routes.template.js +32 -0
- package/dist/generators/server.generator.js +2 -1
- package/dist/generators/server.template.js +62 -40
- package/dist/generators/userSchema.generator.d.ts +4 -0
- package/dist/generators/userSchema.generator.js +27 -0
- package/dist/index.js +41 -18
- package/dist/preprocess/jsonschemaFormat.js +5 -8
- package/dist/preprocess/roleSetupFile.d.ts +6 -0
- package/dist/preprocess/roleSetupFile.js +28 -0
- package/dist/templates/jsonSchema/User.json +12 -11
- package/dist/templates/plugins/PassportGoogle.ts +87 -0
- package/dist/templates/plugins/oauthVerify.ts +39 -0
- package/dist/templates/roles/_RoleFactory.d.ts +10 -0
- package/dist/templates/roles/_RoleFactory.js +17 -0
- package/dist/templates/roles/_RoleFactory.ts +22 -0
- package/dist/templates/services/mongoConn.ts +38 -0
- package/dist/templates/utils/common.ts +48 -0
- package/dist/templates/utils/logger.ts +25 -0
- package/dist/types/types.d.ts +14 -6
- package/dist/utils/common.d.ts +3 -18
- package/dist/utils/common.js +27 -127
- package/dist/utils/generator.d.ts +12 -0
- package/dist/utils/generator.js +72 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/jsonSchema.d.ts +14 -0
- package/dist/utils/jsonSchema.js +84 -0
- package/package.json +1 -1
- package/tsconfig.json +2 -2
- package/vex.config.json +15 -4
package/.env
CHANGED
package/dist/cli.js
CHANGED
|
@@ -10,10 +10,10 @@ const child_process_1 = __importDefault(require("child_process"));
|
|
|
10
10
|
const minimist_1 = __importDefault(require("minimist"));
|
|
11
11
|
const index_1 = require("./index");
|
|
12
12
|
const logger_1 = __importDefault(require("./utils/logger"));
|
|
13
|
-
const
|
|
13
|
+
const utils_1 = require("./utils");
|
|
14
14
|
console.log("\x1b[35m%s\x1b[0m", "\n========== veryExpress CLI (vex) Start ==========\n");
|
|
15
15
|
const args = (0, minimist_1.default)(process.argv.slice(2));
|
|
16
|
-
let config =
|
|
16
|
+
let config = utils_1.defaultCompilerOptions;
|
|
17
17
|
if (fs_1.default.existsSync("vex.config.json")) {
|
|
18
18
|
try {
|
|
19
19
|
config = JSON.parse(fs_1.default.readFileSync("vex.config.json", "utf8"));
|
|
@@ -28,21 +28,25 @@ config.jsonSchemaDir = args.j || args.jsonSchemaDir || config.jsonSchemaDir || "
|
|
|
28
28
|
config.rootDir = args.o || args.rootDir || config.rootDir || ".";
|
|
29
29
|
config.srcDir = config.srcDir || config.rootDir + "/src";
|
|
30
30
|
config.openapiDir = config.openapiDir || config.rootDir + "./openapi";
|
|
31
|
-
config.
|
|
32
|
-
config.
|
|
33
|
-
config.
|
|
34
|
-
config.
|
|
35
|
-
config.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
apple: false,
|
|
39
|
-
github: false
|
|
31
|
+
config.app = config.app || {},
|
|
32
|
+
config.app.enableSwagger = config.app.enableSwagger || true,
|
|
33
|
+
config.app.useUserSchema = config.app.useUserSchema || true,
|
|
34
|
+
config.app.useObjectID = config.app.useObjectID || true,
|
|
35
|
+
config.app.allowApiCreateUpdate_id = config.app.allowApiCreateUpdate_id || false,
|
|
36
|
+
config.useRBAC = config.useRBAC || {
|
|
37
|
+
roles: ["user"]
|
|
40
38
|
};
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
config.useOauth = config.useOauth || {
|
|
40
|
+
google: false,
|
|
41
|
+
microsoft: false,
|
|
42
|
+
apple: false,
|
|
43
|
+
github: false
|
|
44
|
+
};
|
|
45
|
+
if (config.app.useObjectID && config.app.allowApiCreateUpdate_id) {
|
|
46
|
+
logger_1.default.warn("Not recommended to use \"useObjectID\" with \"allowApiCreateUpdate_id\",\nthis may cause some logic issues");
|
|
43
47
|
}
|
|
44
48
|
logger_1.default.process("vex.config.json");
|
|
45
|
-
|
|
49
|
+
(0, utils_1.writeFile)("vex.config", "vex.config.json", JSON.stringify(config, null, 4));
|
|
46
50
|
if (String(args._[0]).toLowerCase() === "h" || args["h"]) {
|
|
47
51
|
console.log(`Very Express CLI Usage :
|
|
48
52
|
vex [flag]
|
|
@@ -195,7 +195,7 @@ export default new {{interfaceName}}Controller().router;
|
|
|
195
195
|
checkSchema(${util_1.default.inspect(templateOptions.validators[templateOptions.endpoint + "/{id}"].delete, { depth: null }).replace(/^/gm, indent3)}),
|
|
196
196
|
this.delete${templateOptions.interfaceName}.bind(this)
|
|
197
197
|
);`);
|
|
198
|
-
template = template.replace(/{{check_id}}/g, templateOptions.compilerOptions.allowApiCreateUpdate_id ? "" : `
|
|
198
|
+
template = template.replace(/{{check_id}}/g, templateOptions.compilerOptions.app.allowApiCreateUpdate_id ? "" : `
|
|
199
199
|
if (req.body._id) {
|
|
200
200
|
delete req.body._id;
|
|
201
201
|
};`);
|
|
@@ -31,17 +31,17 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
31
31
|
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
32
32
|
const controller_template_1 = __importDefault(require("./controller.template"));
|
|
33
33
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
34
|
-
const
|
|
34
|
+
const common_1 = require("../utils/common");
|
|
35
35
|
const types = __importStar(require("../types/types"));
|
|
36
36
|
function compile(options) {
|
|
37
37
|
const file = fs_1.default.readFileSync(options.compilerOptions.openapiDir + "/" + options.openapiFile, "utf8");
|
|
38
|
-
const controllerToModelBasePath =
|
|
38
|
+
const controllerToModelBasePath = (0, common_1.relativePath)(options.controllerOutDir, options.modelDir);
|
|
39
39
|
const openApi = js_yaml_1.default.load(file);
|
|
40
40
|
const endpointsValidator = {};
|
|
41
41
|
const writtedEndpoint = [];
|
|
42
42
|
Object.keys(openApi.paths).forEach((endpoint) => {
|
|
43
|
-
endpointsValidator[endpoint] = {};
|
|
44
43
|
logger_1.default.process(`Controller : ${options.compilerOptions.openapiDir} > ${endpoint}`);
|
|
44
|
+
endpointsValidator[endpoint] = {};
|
|
45
45
|
Object.keys(openApi.paths[endpoint]).forEach((httpMethod) => {
|
|
46
46
|
if (!Object.values(types.httpMethodArr).includes(httpMethod))
|
|
47
47
|
return;
|
|
@@ -51,10 +51,10 @@ function compile(options) {
|
|
|
51
51
|
});
|
|
52
52
|
});
|
|
53
53
|
Object.keys(openApi.paths).forEach((endpoint) => {
|
|
54
|
-
const
|
|
54
|
+
const documentName = openApi.paths[endpoint]["x-collection"];
|
|
55
55
|
const interfaceName = openApi.paths[endpoint]["x-interface"];
|
|
56
56
|
const endpointFormatted = endpoint.replace("/{id}", "").toLowerCase();
|
|
57
|
-
if (
|
|
57
|
+
if (documentName === undefined) {
|
|
58
58
|
logger_1.default.error(`"x-collection" not found in openapi spec's path "${endpoint}"\n - opanapi source: ${options.compilerOptions.openapiDir}`);
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
@@ -68,8 +68,7 @@ function compile(options) {
|
|
|
68
68
|
else {
|
|
69
69
|
const outPath = `${options.controllerOutDir}/${interfaceName}Controller.gen.ts`;
|
|
70
70
|
const controllerToModelPath = `${controllerToModelBasePath}/${interfaceName}Model.gen`;
|
|
71
|
-
|
|
72
|
-
fs_1.default.writeFileSync(outPath, (0, controller_template_1.default)({
|
|
71
|
+
(0, common_1.writeFile)("Controller", outPath, (0, controller_template_1.default)({
|
|
73
72
|
endpoint: endpoint,
|
|
74
73
|
modelPath: controllerToModelPath,
|
|
75
74
|
interfaceName: interfaceName,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import controllerTemplate from "./controller.template";
|
|
2
|
-
import routesTemplate from "./routes.template";
|
|
2
|
+
import routesTemplate from "./routes/routes.template";
|
|
3
3
|
import serverTemplate from "./server.template";
|
|
4
4
|
declare const _default: {
|
|
5
5
|
controllerTemplate: typeof controllerTemplate;
|
package/dist/generators/index.js
CHANGED
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const controller_template_1 = __importDefault(require("./controller.template"));
|
|
7
|
-
const routes_template_1 = __importDefault(require("./routes.template"));
|
|
7
|
+
const routes_template_1 = __importDefault(require("./routes/routes.template"));
|
|
8
8
|
const server_template_1 = __importDefault(require("./server.template"));
|
|
9
9
|
exports.default = {
|
|
10
10
|
controllerTemplate: controller_template_1.default,
|
|
@@ -7,8 +7,9 @@ exports.compile = void 0;
|
|
|
7
7
|
const json_schema_to_openapi_schema_1 = __importDefault(require("json-schema-to-openapi-schema"));
|
|
8
8
|
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
9
9
|
const fs_1 = __importDefault(require("fs"));
|
|
10
|
-
const
|
|
10
|
+
const jsonSchema_1 = __importDefault(require("../utils/jsonSchema"));
|
|
11
11
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
12
|
+
const utils_1 = require("../utils");
|
|
12
13
|
function compile(openapiOutFileName, compilerOptions) {
|
|
13
14
|
let openapiJson = {
|
|
14
15
|
openapi: "3.0.0",
|
|
@@ -36,8 +37,7 @@ function compile(openapiOutFileName, compilerOptions) {
|
|
|
36
37
|
const validOpenApi = (0, json_schema_to_openapi_schema_1.default)(openapiJson, { version: 3.0 });
|
|
37
38
|
const openapiYaml = js_yaml_1.default.dump(validOpenApi);
|
|
38
39
|
const openapiOutFile = compilerOptions.openapiDir + openapiOutFileName;
|
|
39
|
-
|
|
40
|
-
fs_1.default.writeFileSync(openapiOutFile, openapiYaml);
|
|
40
|
+
(0, utils_1.writeFile)("OpenApi", openapiOutFile, openapiYaml);
|
|
41
41
|
}
|
|
42
42
|
exports.compile = compile;
|
|
43
43
|
function jsonToOpenapiPath(openapiJson, jsonSchema, additionalinfo) {
|
|
@@ -59,7 +59,7 @@ function jsonToOpenapiPath(openapiJson, jsonSchema, additionalinfo) {
|
|
|
59
59
|
const routeWithId = ["get", "put", "patch", "delete"].includes(jsonSchemaMethod);
|
|
60
60
|
const useBody = ["post", "put", "patch"].includes(jsonSchemaMethod);
|
|
61
61
|
const route = "/" + lowerDocName + (routeWithId ? "/{id}" : "");
|
|
62
|
-
const httpMethod =
|
|
62
|
+
const httpMethod = jsonSchema_1.default.httpMethod(jsonSchemaMethod, additionalinfo.jsonSchemaFilePath);
|
|
63
63
|
const parameters = [];
|
|
64
64
|
let requestBody = undefined;
|
|
65
65
|
const successResponse = {
|
|
@@ -129,24 +129,24 @@ function jsonToOpenapiComponentSchema(openapiJson, jsonSchema, additionalinfo, c
|
|
|
129
129
|
const interfaceName = documentConfig.interfaceName;
|
|
130
130
|
const componentSchemaResponse = {
|
|
131
131
|
type: "object",
|
|
132
|
-
properties: (0, json_schema_to_openapi_schema_1.default)(
|
|
132
|
+
properties: (0, json_schema_to_openapi_schema_1.default)(jsonSchema_1.default.cleanXcustomValue(jsonSchema.properties, ["index", "unique", "required"]), { version: 3.0 }),
|
|
133
133
|
};
|
|
134
134
|
const componentSchemaBodyRequired = {
|
|
135
135
|
type: "object",
|
|
136
|
-
properties: (0, json_schema_to_openapi_schema_1.default)(
|
|
136
|
+
properties: (0, json_schema_to_openapi_schema_1.default)(jsonSchema_1.default.cleanXcustomValue(jsonSchema.properties, { index: "boolean", unique: "boolean", required: "boolean" }), { version: 3.0 }),
|
|
137
137
|
required: jsonSchema.required,
|
|
138
138
|
};
|
|
139
139
|
const componentSchemaBodyRequiredWithoutId = {
|
|
140
140
|
type: "object",
|
|
141
|
-
properties: (0, json_schema_to_openapi_schema_1.default)(
|
|
141
|
+
properties: (0, json_schema_to_openapi_schema_1.default)(jsonSchema_1.default.cleanXcustomValue(componentSchemaBodyRequired.properties, { _id: "object" }), { version: 3.0 }),
|
|
142
142
|
required: jsonSchema.required,
|
|
143
143
|
};
|
|
144
144
|
const componentSchemaBody = {
|
|
145
145
|
type: "object",
|
|
146
|
-
properties: (0, json_schema_to_openapi_schema_1.default)(
|
|
146
|
+
properties: (0, json_schema_to_openapi_schema_1.default)(jsonSchema_1.default.cleanXcustomValue(componentSchemaBodyRequiredWithoutId.properties, ["required"]), { version: 3.0 }),
|
|
147
147
|
};
|
|
148
148
|
documentConfig.methods.forEach((jsonSchemaMethod) => {
|
|
149
|
-
const httpMethod =
|
|
149
|
+
const httpMethod = jsonSchema_1.default.httpMethod(jsonSchemaMethod, additionalinfo.jsonSchemaFilePath);
|
|
150
150
|
switch (jsonSchemaMethod) {
|
|
151
151
|
case "delete":
|
|
152
152
|
break;
|
|
@@ -210,7 +210,7 @@ function jsonToOpenapiComponentSchema(openapiJson, jsonSchema, additionalinfo, c
|
|
|
210
210
|
componentSchemaPath[httpMethod + interfaceName + "Response"] = componentSchemaResponse;
|
|
211
211
|
break;
|
|
212
212
|
case "post":
|
|
213
|
-
componentSchemaPath[httpMethod + interfaceName + "Body"] = compilerOptions.allowApiCreateUpdate_id ? componentSchemaBodyRequired : componentSchemaBodyRequiredWithoutId;
|
|
213
|
+
componentSchemaPath[httpMethod + interfaceName + "Body"] = compilerOptions.app.allowApiCreateUpdate_id ? componentSchemaBodyRequired : componentSchemaBodyRequiredWithoutId;
|
|
214
214
|
componentSchemaPath[httpMethod + interfaceName + "Response"] = componentSchemaResponse;
|
|
215
215
|
break;
|
|
216
216
|
case "put":
|
|
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.compile = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
9
|
+
const utils_1 = require("../utils");
|
|
9
10
|
function compile(packageOutPath, schemaDir, outDir) {
|
|
10
11
|
logger_1.default.process(`Project : ${packageOutPath}`);
|
|
11
|
-
let writePackage = false;
|
|
12
12
|
let packjson = "";
|
|
13
13
|
if (!fs_1.default.existsSync(packageOutPath)) {
|
|
14
14
|
packjson = fs_1.default.readFileSync(__dirname + "/../templates/root/package.json").toString();
|
|
@@ -19,32 +19,22 @@ function compile(packageOutPath, schemaDir, outDir) {
|
|
|
19
19
|
const packageJson = JSON.parse(packjson);
|
|
20
20
|
const vexgenScript = `vex -j ${schemaDir} -o ${outDir}`;
|
|
21
21
|
if (packageJson.scripts.vexgen !== vexgenScript) {
|
|
22
|
-
packageJson.scripts.vexgen = vexgenScript;
|
|
23
|
-
writePackage = true;
|
|
24
22
|
logger_1.default.process(`package.json : Update script : vexgen > ${packageJson.scripts.vexgen}`);
|
|
23
|
+
packageJson.scripts.vexgen = vexgenScript;
|
|
25
24
|
}
|
|
26
25
|
if (!packageJson.scripts.build) {
|
|
27
|
-
packageJson.scripts.build = "tsc -p .";
|
|
28
|
-
writePackage = true;
|
|
29
26
|
logger_1.default.process(`package.json : Update script : build > ${packageJson.scripts.build}`);
|
|
27
|
+
packageJson.scripts.build = "tsc -p .";
|
|
30
28
|
}
|
|
31
29
|
if (!packageJson.scripts.dev) {
|
|
32
|
-
packageJson.scripts.dev = `nodemon --watch src --exec ts-node ${outDir}/server.ts`;
|
|
33
|
-
writePackage = true;
|
|
34
30
|
logger_1.default.process(`package.json : Update script : dev > ${packageJson.scripts.dev}`);
|
|
31
|
+
packageJson.scripts.dev = `nodemon --watch src --exec ts-node ${outDir}/server.ts`;
|
|
35
32
|
}
|
|
36
33
|
if (!packageJson.scripts.start) {
|
|
37
|
-
packageJson.scripts.start = "node ./dist/server.js";
|
|
38
|
-
writePackage = true;
|
|
39
34
|
logger_1.default.process(`package.json : Update script : start > ${packageJson.scripts.start}`);
|
|
35
|
+
packageJson.scripts.start = "node ./dist/server.js";
|
|
40
36
|
}
|
|
41
|
-
|
|
42
|
-
logger_1.default.writing(`Project : ${packageOutPath}`);
|
|
43
|
-
fs_1.default.writeFileSync(packageOutPath, JSON.stringify(packageJson, null, 4));
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
logger_1.default.info("package.json : No changes");
|
|
47
|
-
}
|
|
37
|
+
(0, utils_1.writeFile)(`Project : ${packageOutPath}`, packageOutPath, JSON.stringify(packageJson, null, 4));
|
|
48
38
|
}
|
|
49
39
|
exports.compile = compile;
|
|
50
40
|
exports.default = {
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.compile = void 0;
|
|
30
|
+
const fs_1 = __importDefault(require("fs"));
|
|
31
|
+
const roleBase = __importStar(require("../templates/roles/_RoleFactory"));
|
|
32
|
+
const logger_1 = __importDefault(require("../utils/logger"));
|
|
33
|
+
const utils_1 = require("../utils");
|
|
34
|
+
const roleSetupFile_1 = require("../preprocess/roleSetupFile");
|
|
35
|
+
function compile(options) {
|
|
36
|
+
var _a;
|
|
37
|
+
(0, roleSetupFile_1.roleSetupFile)({
|
|
38
|
+
collectionList: options.collectionList,
|
|
39
|
+
roleSetupDir: options.roleSourceDir,
|
|
40
|
+
compilerOptions: options.compilerOptions
|
|
41
|
+
});
|
|
42
|
+
const indexFileData = [];
|
|
43
|
+
(_a = options.compilerOptions.useRBAC) === null || _a === void 0 ? void 0 : _a.roles.forEach((role) => {
|
|
44
|
+
logger_1.default.process(`RBAC Class : ${role}`);
|
|
45
|
+
const RoleAccessAction = [];
|
|
46
|
+
const roleSrcFile = `${options.compilerOptions.rootDir}/roles/${role}.json`;
|
|
47
|
+
const roleOutFile = `${options.roleOutDir}/${role}.gen.ts`;
|
|
48
|
+
const roleContent = (0, utils_1.loadJson)(roleSrcFile);
|
|
49
|
+
Object.values(roleContent).forEach((resourceContent) => {
|
|
50
|
+
if (Array.isArray(resourceContent)) {
|
|
51
|
+
resourceContent.forEach((action) => {
|
|
52
|
+
if (!RoleAccessAction.includes(action) && !roleBase.accessActionArr.includes(action)) {
|
|
53
|
+
RoleAccessAction.push(action);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
const RoleAccessActionString = Object.keys(RoleAccessAction).map((key) => `"${key}"`);
|
|
59
|
+
RoleAccessActionString.push("roleFt.accessAction");
|
|
60
|
+
const content = `${options.compilerOptions.headerComment}
|
|
61
|
+
import * as role from "./_RoleFactory.gen";
|
|
62
|
+
|
|
63
|
+
export type accessAction${role} = ${RoleAccessActionString.join(" | ")};
|
|
64
|
+
|
|
65
|
+
const ${role}AccessControl: roleFt.accessControl<accessAction${role}> = ${JSON.stringify(roleContent, null, 4)};
|
|
66
|
+
|
|
67
|
+
export default class Role${role} extends roleFt._RoleFactory<accessAction${role}> {
|
|
68
|
+
constructor() {
|
|
69
|
+
super(${role}AccessControl);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
`;
|
|
73
|
+
(0, utils_1.writeFile)("RBAC Role", roleOutFile, content);
|
|
74
|
+
indexFileData.push({
|
|
75
|
+
name: role,
|
|
76
|
+
from: roleOutFile,
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
const indexFile = `${options.roleOutDir}/index.gen.ts`;
|
|
80
|
+
const indexFileContent = indexFileData.map((data) => `export { default as ${data.name} } from "./${data.from.replace(".ts", "")}";`).join("\n");
|
|
81
|
+
(0, utils_1.writeFile)("RBAC Index", indexFile, `${options.compilerOptions.headerComment}\n${indexFileContent}`);
|
|
82
|
+
if (!fs_1.default.existsSync(`${options.compilerOptions.srcDir}/middleware`)) {
|
|
83
|
+
fs_1.default.mkdirSync(`${options.compilerOptions.srcDir}/middleware`);
|
|
84
|
+
}
|
|
85
|
+
const middlewareFile = `${options.compilerOptions.srcDir}/middleware/RoleBaseAccessControl.gen.ts`;
|
|
86
|
+
const roleTypes = indexFileData.map((data) => `typeof ${data.name}`).join(" | ");
|
|
87
|
+
const middlewareContent = `${options.compilerOptions.headerComment}
|
|
88
|
+
import { Request, Response, NextFunction } from "express";
|
|
89
|
+
import * as roles from "../roles";
|
|
90
|
+
|
|
91
|
+
export { roles };
|
|
92
|
+
|
|
93
|
+
export function roleBaseAccessControl(
|
|
94
|
+
role: ${roleTypes},
|
|
95
|
+
collection: string,
|
|
96
|
+
action: string
|
|
97
|
+
) {
|
|
98
|
+
return function (req: Request, res: Response, next: NextFunction) {
|
|
99
|
+
if ( new role().checkAccess(collection, action) ) {
|
|
100
|
+
next();
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
res.status(403).send("Permission denied");
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}`;
|
|
107
|
+
(0, utils_1.writeFile)("RBAC Middleware", middlewareFile, middlewareContent);
|
|
108
|
+
}
|
|
109
|
+
exports.compile = compile;
|
|
@@ -1,10 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
4
24
|
};
|
|
5
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
26
|
exports.compile = void 0;
|
|
7
|
-
const
|
|
27
|
+
const utilsGenerator = __importStar(require("./../../utils/generator"));
|
|
8
28
|
function loginHtml(providers) {
|
|
9
29
|
let html = "<p> login with : </p>";
|
|
10
30
|
providers.forEach((pp) => {
|
|
@@ -13,7 +33,7 @@ function loginHtml(providers) {
|
|
|
13
33
|
return `\`${html}\``;
|
|
14
34
|
}
|
|
15
35
|
function compile(compilerOptions) {
|
|
16
|
-
const providers =
|
|
36
|
+
const providers = utilsGenerator.isUseOAuth(compilerOptions);
|
|
17
37
|
return `${compilerOptions.headerComment}
|
|
18
38
|
import { Router } from 'express';
|
|
19
39
|
import util from 'util';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as types from "../../types/types";
|
|
2
|
+
export declare function compile(options: {
|
|
3
|
+
routesArr: {
|
|
4
|
+
route: string;
|
|
5
|
+
interfaceName: string;
|
|
6
|
+
controllerPath: string;
|
|
7
|
+
}[];
|
|
8
|
+
routesDir: string;
|
|
9
|
+
openapiFile: string;
|
|
10
|
+
compilerOptions: types.compilerOptions;
|
|
11
|
+
}): void;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.compile = void 0;
|
|
30
|
+
const routes_template_1 = __importDefault(require("./routes.template"));
|
|
31
|
+
const routesOAuthGen = __importStar(require("./oauth.generator"));
|
|
32
|
+
const routesSwaggerGen = __importStar(require("./swagger.generator"));
|
|
33
|
+
const utilsGenerator = __importStar(require("../../utils/generator"));
|
|
34
|
+
const logger_1 = __importDefault(require("../../utils/logger"));
|
|
35
|
+
const utils_1 = require("../../utils");
|
|
36
|
+
function compile(options) {
|
|
37
|
+
logger_1.default.process("Route");
|
|
38
|
+
const routesApiOutPath = `${options.routesDir}/ApiRouter.gen.ts`;
|
|
39
|
+
const routesOAuthOutPath = `${options.routesDir}/OAuthRouter.gen.ts`;
|
|
40
|
+
const routesSwaggerOutPath = `${options.routesDir}/SwaggerRouter.gen.ts`;
|
|
41
|
+
if (utilsGenerator.isUseOAuth(options.compilerOptions).length > 0) {
|
|
42
|
+
(0, utils_1.writeFile)("Route OAuth", routesOAuthOutPath, routesOAuthGen.compile(options.compilerOptions));
|
|
43
|
+
}
|
|
44
|
+
if (options.compilerOptions.app.enableSwagger) {
|
|
45
|
+
(0, utils_1.writeFile)("Route Swagger", routesSwaggerOutPath, routesSwaggerGen.compile(options.compilerOptions));
|
|
46
|
+
}
|
|
47
|
+
(0, utils_1.writeFile)("Route API", routesApiOutPath, (0, routes_template_1.default)({
|
|
48
|
+
routes: options.routesArr,
|
|
49
|
+
openapiFile: options.openapiFile,
|
|
50
|
+
compilerOptions: options.compilerOptions,
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
exports.compile = compile;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as types from "../../types/types";
|
|
2
|
+
export default function routesTemplate(options: {
|
|
3
|
+
template?: string;
|
|
4
|
+
openapiFile?: string;
|
|
5
|
+
routes: {
|
|
6
|
+
route: string;
|
|
7
|
+
interfaceName: string;
|
|
8
|
+
controllerPath: string;
|
|
9
|
+
}[];
|
|
10
|
+
compilerOptions: types.compilerOptions;
|
|
11
|
+
}): string;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function routesTemplate(options) {
|
|
4
|
+
let template = options.template || `{{headerComment}}
|
|
5
|
+
import { Router } from 'express';
|
|
6
|
+
|
|
7
|
+
{{importRoutes}}
|
|
8
|
+
|
|
9
|
+
export default class ApiRouter{
|
|
10
|
+
|
|
11
|
+
public router: Router = Router();
|
|
12
|
+
|
|
13
|
+
constructor() {}
|
|
14
|
+
|
|
15
|
+
public initRoutes() {
|
|
16
|
+
|
|
17
|
+
{{useRoutes}}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
}`;
|
|
21
|
+
let importRoutes = "";
|
|
22
|
+
let useRoutes = "";
|
|
23
|
+
options.routes.forEach((obj) => {
|
|
24
|
+
importRoutes += `import ${obj.interfaceName}Controller from '${obj.controllerPath}';\n`;
|
|
25
|
+
useRoutes += ` this.router.use('${obj.route}', ${obj.interfaceName}Controller);\n`;
|
|
26
|
+
});
|
|
27
|
+
template = template.replace(/{{headerComment}}/g, options.compilerOptions.headerComment || "// generated files by very-express");
|
|
28
|
+
template = template.replace(/{{importRoutes}}/g, importRoutes);
|
|
29
|
+
template = template.replace(/{{useRoutes}}/g, useRoutes);
|
|
30
|
+
return template;
|
|
31
|
+
}
|
|
32
|
+
exports.default = routesTemplate;
|
|
@@ -8,12 +8,13 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
8
8
|
const server_template_1 = __importDefault(require("./server.template"));
|
|
9
9
|
const packageJson_generator_1 = __importDefault(require("./packageJson.generator"));
|
|
10
10
|
const logger_1 = __importDefault(require("../utils/logger"));
|
|
11
|
+
const utils_1 = require("../utils");
|
|
11
12
|
function compile(compilerOptions) {
|
|
12
13
|
const serverOutPath = compilerOptions.srcDir + "/server.ts";
|
|
13
14
|
const packageOutPath = compilerOptions.rootDir + "/package.json";
|
|
14
15
|
const envOutPath = compilerOptions.rootDir + "/.env";
|
|
15
16
|
const tsconfigOutPath = compilerOptions.rootDir + "/tsconfig.json";
|
|
16
|
-
|
|
17
|
+
(0, utils_1.writeFile)("Server", serverOutPath, (0, server_template_1.default)({
|
|
17
18
|
compilerOptions: compilerOptions,
|
|
18
19
|
}));
|
|
19
20
|
if (!fs_1.default.existsSync(envOutPath)) {
|