very-express 0.2.6 → 0.2.8

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/cli.js CHANGED
@@ -30,12 +30,17 @@ config.srcDir = config.srcDir || config.rootDir + "/src";
30
30
  config.openapiDir = config.openapiDir || config.rootDir + "./openapi";
31
31
  config.enableSwagger = config.enableSwagger || true,
32
32
  config.useUserSchema = config.useUserSchema || true,
33
+ config.useObjectID = config.useObjectID || true,
34
+ config.allowApiCreateUpdate_id = config.allowApiCreateUpdate_id || false,
33
35
  config.useOauth = config.useOauth || {
34
36
  google: false,
35
37
  microsoft: false,
36
38
  apple: false,
37
39
  github: false
38
40
  };
41
+ if (config.useObjectID && config.allowApiCreateUpdate_id) {
42
+ logger_1.default.warn("Not recommended to use \"useObjectID\" with \"allowApiCreateUpdate_id\",\nthis may cause security issues");
43
+ }
39
44
  logger_1.default.process("vex.config.json");
40
45
  fs_1.default.writeFileSync("vex.config.json", JSON.stringify(config, null, 4));
41
46
  if (String(args._[0]).toLowerCase() === "h" || args["h"]) {
@@ -81,7 +81,7 @@ class {{interfaceName}}Controller {
81
81
  const validationError = validationResult(req);
82
82
  if ( ! validationError.isEmpty() ) {
83
83
  return res.status(400).json(validationError.array());
84
- };
84
+ };{{check_id}}
85
85
 
86
86
  const result = await {{interfaceName}}Model.create(req.body);
87
87
  if (!result) {
@@ -100,7 +100,8 @@ class {{interfaceName}}Controller {
100
100
  const validationError = validationResult(req);
101
101
  if ( ! validationError.isEmpty() ) {
102
102
  return res.status(400).json(validationError.array());
103
- };
103
+ };{{check_id}}
104
+
104
105
  const result = await {{interfaceName}}Model.findByIdAndUpdate(req.params.id, req.body, { new: true });
105
106
  if (!result) {
106
107
  return res.status(404).json({ error: "failed to update" });
@@ -118,7 +119,8 @@ class {{interfaceName}}Controller {
118
119
  const validationError = validationResult(req);
119
120
  if ( ! validationError.isEmpty() ) {
120
121
  return res.status(400).json(validationError.array());
121
- };
122
+ };{{check_id}}
123
+
122
124
  const result = await {{interfaceName}}Model.replaceOne({_id: req.params.id}, req.body);
123
125
  if (!result) {
124
126
  return res.status(404).json({ error: "failed to update" });
@@ -137,6 +139,7 @@ class {{interfaceName}}Controller {
137
139
  if ( ! validationError.isEmpty() ) {
138
140
  return res.status(400).json(validationError.array());
139
141
  };
142
+
140
143
  const result = await {{interfaceName}}Model.findByIdAndDelete(req.params.id);
141
144
  if (!result) {
142
145
  return res.status(404).json({ error: "failed to update" });
@@ -192,6 +195,10 @@ export default new {{interfaceName}}Controller().router;
192
195
  checkSchema(${util_1.default.inspect(templateOptions.validators[templateOptions.endpoint + "/{id}"].delete, { depth: null }).replace(/^/gm, indent3)}),
193
196
  this.delete${templateOptions.interfaceName}.bind(this)
194
197
  );`);
198
+ template = template.replace(/{{check_id}}/g, templateOptions.compilerOptions.allowApiCreateUpdate_id ? "" : `
199
+ if (req.body._id) {
200
+ delete req.body._id;
201
+ };`);
195
202
  return template;
196
203
  }
197
204
  exports.default = controllerTemplate;
@@ -31,7 +31,7 @@ function compile(openapiOutFileName, compilerOptions) {
31
31
  const jsonSchemaBuffer = fs_1.default.readFileSync(`${compilerOptions.jsonSchemaDir}/${file}`);
32
32
  const jsonSchema = JSON.parse(jsonSchemaBuffer.toString());
33
33
  openapiJson = jsonToOpenapiPath(openapiJson, jsonSchema, { jsonSchemaFilePath: jsonSchemaFilePath });
34
- openapiJson = jsonToOpenapiComponentSchema(openapiJson, jsonSchema, { jsonSchemaFilePath: jsonSchemaFilePath });
34
+ openapiJson = jsonToOpenapiComponentSchema(openapiJson, jsonSchema, { jsonSchemaFilePath: jsonSchemaFilePath }, compilerOptions);
35
35
  });
36
36
  const validOpenApi = (0, json_schema_to_openapi_schema_1.default)(openapiJson, { version: 3.0 });
37
37
  const openapiYaml = js_yaml_1.default.dump(validOpenApi);
@@ -122,7 +122,7 @@ function jsonToOpenapiPath(openapiJson, jsonSchema, additionalinfo) {
122
122
  openapiJson.paths = Object.assign(openapiJson.paths, routes);
123
123
  return openapiJson;
124
124
  }
125
- function jsonToOpenapiComponentSchema(openapiJson, jsonSchema, additionalinfo) {
125
+ function jsonToOpenapiComponentSchema(openapiJson, jsonSchema, additionalinfo, compilerOptions) {
126
126
  const componentSchemaPath = {};
127
127
  const documentConfig = jsonSchema["x-documentConfig"];
128
128
  const lowerDocName = documentConfig.documentName.toLowerCase();
@@ -131,15 +131,20 @@ function jsonToOpenapiComponentSchema(openapiJson, jsonSchema, additionalinfo) {
131
131
  type: "object",
132
132
  properties: (0, json_schema_to_openapi_schema_1.default)(common_1.default.cleanXcustomValue(jsonSchema.properties, ["index", "unique", "required"]), { version: 3.0 }),
133
133
  };
134
- const componentSchemaBody = {
134
+ const componentSchemaBodyRequired = {
135
135
  type: "object",
136
- properties: (0, json_schema_to_openapi_schema_1.default)(common_1.default.cleanXcustomValue(jsonSchema.properties, ["_id", "index", "unique", "required"]), { version: 3.0 }),
136
+ properties: (0, json_schema_to_openapi_schema_1.default)(common_1.default.cleanXcustomValue(jsonSchema.properties, { index: "boolean", unique: "boolean", required: "boolean" }), { version: 3.0 }),
137
+ required: jsonSchema.required,
137
138
  };
138
- const componentSchemaBodyRequired = {
139
+ const componentSchemaBodyRequiredWithoutId = {
139
140
  type: "object",
140
- properties: (0, json_schema_to_openapi_schema_1.default)(common_1.default.cleanXcustomValue(jsonSchema.properties, { _id: "string", index: "boolean", unique: "boolean", required: "boolean" }), { version: 3.0 }),
141
+ properties: (0, json_schema_to_openapi_schema_1.default)(common_1.default.cleanXcustomValue(componentSchemaBodyRequired.properties, { _id: "object" }), { version: 3.0 }),
141
142
  required: jsonSchema.required,
142
143
  };
144
+ const componentSchemaBody = {
145
+ type: "object",
146
+ properties: (0, json_schema_to_openapi_schema_1.default)(common_1.default.cleanXcustomValue(componentSchemaBodyRequiredWithoutId.properties, ["required"]), { version: 3.0 }),
147
+ };
143
148
  documentConfig.methods.forEach((jsonSchemaMethod) => {
144
149
  const httpMethod = common_1.default.httpMethod(jsonSchemaMethod, additionalinfo.jsonSchemaFilePath);
145
150
  switch (jsonSchemaMethod) {
@@ -205,6 +210,9 @@ function jsonToOpenapiComponentSchema(openapiJson, jsonSchema, additionalinfo) {
205
210
  componentSchemaPath[httpMethod + interfaceName + "Response"] = componentSchemaResponse;
206
211
  break;
207
212
  case "post":
213
+ componentSchemaPath[httpMethod + interfaceName + "Body"] = compilerOptions.allowApiCreateUpdate_id ? componentSchemaBodyRequired : componentSchemaBodyRequiredWithoutId;
214
+ componentSchemaPath[httpMethod + interfaceName + "Response"] = componentSchemaResponse;
215
+ break;
208
216
  case "put":
209
217
  componentSchemaPath[httpMethod + interfaceName + "Body"] = componentSchemaBodyRequired;
210
218
  componentSchemaPath[httpMethod + interfaceName + "Response"] = componentSchemaResponse;
package/dist/index.js CHANGED
@@ -78,7 +78,7 @@ function generate(options) {
78
78
  logger_1.default.warn(`Skipping Non Json File : ${schemaPath}`);
79
79
  return;
80
80
  }
81
- const jsonSchema = (0, jsonschemaFormat_1.formatJsonSchema)(schemaPath);
81
+ const jsonSchema = (0, jsonschemaFormat_1.formatJsonSchema)(schemaPath, options);
82
82
  const documentConfig = jsonSchema["x-documentConfig"];
83
83
  json2mongoose_1.default.typesGen.compileFromFile(`${schemaPath}`, `${dir.typeDir}/${documentConfig.interfaceName}.gen.ts`, options || common_1.default.defaultCompilerOptions);
84
84
  json2mongoose_1.default.modelsGen.compileFromFile(`${schemaPath}`, `${common_1.default.relativePath(dir.modelDir, dir.typeDir)}/${documentConfig.interfaceName}.gen`, `${dir.modelDir}/${documentConfig.interfaceName}Model.gen.ts`, options || common_1.default.defaultCompilerOptions);
@@ -1,2 +1,2 @@
1
1
  import * as types from "../types/types";
2
- export declare function formatJsonSchema(jsonSchemaPath: string): types.jsonSchema;
2
+ export declare function formatJsonSchema(jsonSchemaPath: string, compilerOptions: types.compilerOptions): types.jsonSchema;
@@ -31,7 +31,7 @@ const fs_1 = __importDefault(require("fs"));
31
31
  const types = __importStar(require("../types/types"));
32
32
  const common = __importStar(require("../utils/common"));
33
33
  const logger_1 = __importDefault(require("../utils/logger"));
34
- function formatJsonSchema(jsonSchemaPath) {
34
+ function formatJsonSchema(jsonSchemaPath, compilerOptions) {
35
35
  const jsonSchema = common.loadJsonSchema(jsonSchemaPath);
36
36
  if (jsonSchema == undefined) {
37
37
  logger_1.default.error("formatJsonSchema : error at loadJsonSchema()");
@@ -52,6 +52,9 @@ function formatJsonSchema(jsonSchemaPath) {
52
52
  if (typeof jsonSchema.properties !== "object") {
53
53
  logger_1.default.error(`properties is invalid in ${jsonSchemaPath}`);
54
54
  }
55
+ if (compilerOptions.useObjectID) {
56
+ checkField_id(jsonSchema, jsonSchemaPath);
57
+ }
55
58
  jsonSchema.required = getRequiredArrStr(jsonSchema, jsonSchemaPath);
56
59
  fs_1.default.writeFileSync(jsonSchemaPath, JSON.stringify(jsonSchema, null, 4));
57
60
  return jsonSchema;
@@ -95,3 +98,13 @@ function getRequiredArrStr(schema, jsonSchemaPath) {
95
98
  }
96
99
  return requiredArr;
97
100
  }
101
+ function checkField_id(schema, jsonSchemaPath) {
102
+ if (schema.properties && !schema.properties._id) {
103
+ schema.properties._id = {
104
+ type: "string",
105
+ description: "Unique Identifier",
106
+ example: "60c8c9d2b2b4f2c3e8d6f3e1",
107
+ };
108
+ logger_1.default.info(`formatJsonSchema : "_id" field added to ${jsonSchemaPath}`);
109
+ }
110
+ }
@@ -5,8 +5,10 @@ export interface compilerOptions {
5
5
  openapiDir: string;
6
6
  rootDir: string;
7
7
  srcDir: string;
8
- enableSwagger: true;
9
- useUserSchema: true;
8
+ enableSwagger: boolean;
9
+ useUserSchema: boolean;
10
+ useObjectID: boolean;
11
+ allowApiCreateUpdate_id: boolean;
10
12
  useOauth: {
11
13
  google?: boolean;
12
14
  microsoft?: boolean;
@@ -152,6 +152,8 @@ exports.defaultCompilerOptions = {
152
152
  openapiDir: "./openapi",
153
153
  enableSwagger: true,
154
154
  useUserSchema: true,
155
+ useObjectID: true,
156
+ allowApiCreateUpdate_id: false,
155
157
  useOauth: {
156
158
  google: false,
157
159
  microsoft: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "very-express",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "based on json schema and openapi, generate Express RestApi server with mongoDb ",
5
5
  "author": "justin26l",
6
6
  "license": "MIT",
package/vex.config.json CHANGED
@@ -6,6 +6,8 @@
6
6
  "openapiDir": "./output/openapi",
7
7
  "enableSwagger": true,
8
8
  "useUserSchema": true,
9
+ "useObjectID": true,
10
+ "allowApiCreateUpdate_id": false,
9
11
  "useOauth": {
10
12
  "google": true,
11
13
  "microsoft": false,