very-express 0.3.6 → 0.4.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/.gitattributes +2 -2
- package/dist/{templates/plugins → _plugins}/PassportGoogle.ts +2 -2
- package/dist/{templates/plugins → _plugins}/oauthVerify.ts +1 -1
- package/dist/{templates/services → _services}/mongoConn.ts +1 -1
- package/dist/{templates/utils → _utils}/common.ts +6 -1
- package/dist/_utils/index.ts +11 -0
- package/dist/{templates/utils → _utils}/logger.ts +5 -2
- package/dist/_utils/response.ts +32 -0
- package/dist/cli.d.ts +0 -1
- package/dist/generators/app/openapi.generator.d.ts +3 -0
- package/dist/generators/{role.generator.d.ts → app/role.generator.d.ts} +2 -2
- package/dist/generators/app/server.generator.d.ts +2 -0
- package/dist/generators/{server.template.d.ts → app/server.template.d.ts} +1 -1
- package/dist/generators/{controller.template.d.ts → controller/controller.template.d.ts} +1 -1
- package/dist/generators/{controllers.generator.d.ts → controller/controllers.generator.d.ts} +2 -2
- package/dist/generators/index.d.ts +2 -2
- package/dist/generators/project/packageJson.generator.d.ts +6 -0
- package/dist/generators/{userSchema.generator.d.ts → project/userSchema.generator.d.ts} +2 -2
- package/dist/generators/routes/routes.generator.d.ts +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +991 -121
- package/dist/preprocess/jsonschemaFormat.d.ts +1 -1
- package/dist/preprocess/roleSetupFile.d.ts +1 -1
- package/dist/templates/{plugins/PassportGoogle.gen.ts → _plugins/PassportGoogle.ts} +29 -8
- package/dist/templates/_plugins/oauthVerify.ts +39 -0
- package/dist/templates/_roles/_RoleFactory.ts +22 -0
- package/dist/templates/{services/mongoConn.gen.ts → _services/mongoConn.ts} +12 -1
- package/dist/templates/_types/responseMsg.ts +13 -0
- package/dist/templates/{utils/common.gen.ts → _utils/common.ts} +6 -1
- package/dist/templates/_utils/index.ts +11 -0
- package/dist/templates/{utils/logger.gen.ts → _utils/logger.ts} +5 -2
- package/dist/templates/_utils/response.ts +32 -0
- package/dist/templates/root/tsconfig.json +2 -1
- package/dist/types/types.d.ts +1 -0
- package/dist/utils/configChecker.d.ts +2 -0
- package/dist/utils/index.d.ts +1 -0
- package/package.json +13 -10
- package/tsconfig.json +8 -6
- package/vex.config.json +3 -2
- package/.env +0 -6
- package/dist/cli.js +0 -84
- package/dist/generators/controller.template.js +0 -204
- package/dist/generators/controllers.generator.js +0 -173
- package/dist/generators/index.js +0 -13
- package/dist/generators/openapi.generator.d.ts +0 -3
- package/dist/generators/openapi.generator.js +0 -227
- package/dist/generators/packageJson.generator.d.ts +0 -5
- package/dist/generators/packageJson.generator.js +0 -42
- package/dist/generators/role.generator.js +0 -112
- package/dist/generators/routes/oauth.generator.js +0 -66
- package/dist/generators/routes/routes.generator.js +0 -53
- package/dist/generators/routes/routes.template.js +0 -32
- package/dist/generators/routes/swagger.generator.js +0 -46
- package/dist/generators/routes.generator.d.ts +0 -11
- package/dist/generators/routes.generator.js +0 -53
- package/dist/generators/routes.template.d.ts +0 -11
- package/dist/generators/routes.template.js +0 -33
- package/dist/generators/server.generator.d.ts +0 -2
- package/dist/generators/server.generator.js +0 -30
- package/dist/generators/server.template.js +0 -149
- package/dist/generators/userSchema.generator.js +0 -30
- package/dist/preprocess/jsonschemaFormat.js +0 -107
- package/dist/preprocess/roleSetupFile.js +0 -60
- package/dist/templates/plugins/oauthVerify.gen.ts +0 -34
- package/dist/templates/roles/_RoleFactory.js +0 -17
- package/dist/templates/routes/swagger.gen.ts +0 -17
- package/dist/test.d.ts +0 -1
- package/dist/test.js +0 -10
- package/dist/types/controller.d.ts +0 -0
- package/dist/types/controller.js +0 -1
- package/dist/types/openapi.js +0 -2
- package/dist/types/types.js +0 -5
- package/dist/utils/common.js +0 -74
- package/dist/utils/generator.js +0 -74
- package/dist/utils/index.js +0 -20
- package/dist/utils/jsonSchema.js +0 -84
- package/dist/utils/log.d.ts +0 -13
- package/dist/utils/log.js +0 -31
- package/dist/utils/logger.js +0 -31
- /package/dist/{templates/roles → _roles}/_RoleFactory.ts +0 -0
- /package/dist/templates/{roles → _roles}/_RoleFactory.d.ts +0 -0
|
@@ -1,204 +0,0 @@
|
|
|
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 util_1 = __importDefault(require("util"));
|
|
7
|
-
function controllerTemplate(templateOptions) {
|
|
8
|
-
let template = templateOptions.template || `{{headerComment}}
|
|
9
|
-
import { Router, Request, Response } from 'express';
|
|
10
|
-
import { checkSchema, validationResult } from 'express-validator';
|
|
11
|
-
import { parseFieldsSelect } from '../utils/common.gen';
|
|
12
|
-
import MongoQS from 'mongo-ts-querystring';
|
|
13
|
-
|
|
14
|
-
import { {{interfaceName}}Model } from '{{modelPath}}';
|
|
15
|
-
|
|
16
|
-
class {{interfaceName}}Controller {
|
|
17
|
-
public router: Router;
|
|
18
|
-
|
|
19
|
-
constructor() {
|
|
20
|
-
this.router = Router();
|
|
21
|
-
this.routes();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public routes() {
|
|
25
|
-
|
|
26
|
-
{{getListRoute}}
|
|
27
|
-
|
|
28
|
-
{{getRoute}}
|
|
29
|
-
|
|
30
|
-
{{postRoute}}
|
|
31
|
-
|
|
32
|
-
{{putRoute}}
|
|
33
|
-
|
|
34
|
-
{{patchRoute}}
|
|
35
|
-
|
|
36
|
-
{{deleteRoute}}
|
|
37
|
-
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
public async get{{interfaceName}}(req: Request, res: Response): Promise<Response> {
|
|
41
|
-
try {
|
|
42
|
-
const validationError = validationResult(req);
|
|
43
|
-
if ( ! validationError.isEmpty() ) {
|
|
44
|
-
return res.status(400).json(validationError.array());
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const result = await {{interfaceName}}Model.findById(req.params.id);
|
|
48
|
-
if (!result) {
|
|
49
|
-
return res.status(404).json({ error: "no data found" });
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
return res.status(200).json(result);
|
|
53
|
-
};
|
|
54
|
-
} catch (err:any) {
|
|
55
|
-
return res.status(500).json({ error: err.message });
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public async getList{{interfaceName}}(req: Request, res: Response): Promise<Response> {
|
|
60
|
-
try {
|
|
61
|
-
const validationError = validationResult(req);
|
|
62
|
-
if ( ! validationError.isEmpty() ) {
|
|
63
|
-
return res.status(400).json(validationError.array());
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const searchFilter = new MongoQS().parse(req.query);
|
|
67
|
-
let selectedFields : {[key: string]: number} | undefined = undefined;
|
|
68
|
-
|
|
69
|
-
try { selectedFields = parseFieldsSelect(req.query.select || '');}
|
|
70
|
-
catch (err:any) { return res.status(400).json({ error: err.message });};
|
|
71
|
-
|
|
72
|
-
const result = await {{interfaceName}}Model.find(searchFilter, selectedFields);
|
|
73
|
-
return res.status(200).json(result);
|
|
74
|
-
} catch (err:any) {
|
|
75
|
-
return res.status(500).json({ error: err.message });
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public async create{{interfaceName}}(req: Request, res: Response): Promise<Response> {
|
|
80
|
-
try {
|
|
81
|
-
const validationError = validationResult(req);
|
|
82
|
-
if ( ! validationError.isEmpty() ) {
|
|
83
|
-
return res.status(400).json(validationError.array());
|
|
84
|
-
};{{check_id}}
|
|
85
|
-
|
|
86
|
-
const result = await {{interfaceName}}Model.create(req.body);
|
|
87
|
-
if (!result) {
|
|
88
|
-
return res.status(500).json({ error: 'failed to create data' });
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
return res.status(201).json(result);
|
|
92
|
-
};
|
|
93
|
-
} catch (err:any) {
|
|
94
|
-
return res.status(500).json({ error: err.message });
|
|
95
|
-
};
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
public async update{{interfaceName}}(req: Request, res: Response): Promise<Response> {
|
|
99
|
-
try {
|
|
100
|
-
const validationError = validationResult(req);
|
|
101
|
-
if ( ! validationError.isEmpty() ) {
|
|
102
|
-
return res.status(400).json(validationError.array());
|
|
103
|
-
};{{check_id}}
|
|
104
|
-
|
|
105
|
-
const result = await {{interfaceName}}Model.findByIdAndUpdate(req.params.id, req.body, { new: true });
|
|
106
|
-
if (!result) {
|
|
107
|
-
return res.status(404).json({ error: "failed to update" });
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
return res.status(200).json(result);
|
|
111
|
-
};
|
|
112
|
-
} catch (err:any) {
|
|
113
|
-
return res.status(500).json({ error: err.message });
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
public async replace{{interfaceName}}(req: Request, res: Response): Promise<Response> {
|
|
118
|
-
try {
|
|
119
|
-
const validationError = validationResult(req);
|
|
120
|
-
if ( ! validationError.isEmpty() ) {
|
|
121
|
-
return res.status(400).json(validationError.array());
|
|
122
|
-
};{{check_id}}
|
|
123
|
-
|
|
124
|
-
const result = await {{interfaceName}}Model.replaceOne({_id: req.params.id}, req.body);
|
|
125
|
-
if (!result) {
|
|
126
|
-
return res.status(404).json({ error: "failed to update" });
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
return res.status(200).json(result);
|
|
130
|
-
};
|
|
131
|
-
} catch (err:any) {
|
|
132
|
-
return res.status(500).json({ error: err.message });
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
public async delete{{interfaceName}}(req: Request, res: Response): Promise<Response> {
|
|
137
|
-
try {
|
|
138
|
-
const validationError = validationResult(req);
|
|
139
|
-
if ( ! validationError.isEmpty() ) {
|
|
140
|
-
return res.status(400).json(validationError.array());
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
const result = await {{interfaceName}}Model.findByIdAndDelete(req.params.id);
|
|
144
|
-
if (!result) {
|
|
145
|
-
return res.status(404).json({ error: "failed to update" });
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
return res.status(204).json(result);
|
|
149
|
-
};
|
|
150
|
-
} catch (err:any) {
|
|
151
|
-
return res.status(500).json({ error: err.message });
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export default new {{interfaceName}}Controller().router;
|
|
157
|
-
`;
|
|
158
|
-
const indent3 = " ";
|
|
159
|
-
template = template.replace(/{{headerComment}}/g, templateOptions.compilerOptions.headerComment || "// generated files by very-express");
|
|
160
|
-
template = template.replace(/{{interfaceName}}/g, templateOptions.interfaceName);
|
|
161
|
-
template = template.replace(/{{modelPath}}/g, templateOptions.modelPath);
|
|
162
|
-
template = template.replace(/{{getListRoute}}/g, !templateOptions.validators[templateOptions.endpoint].get ? `
|
|
163
|
-
// getListRoute disabled` : `
|
|
164
|
-
this.router.get('/',
|
|
165
|
-
checkSchema(${util_1.default.inspect(templateOptions.validators[templateOptions.endpoint].get, { depth: null }).replace(/^/gm, indent3)}),
|
|
166
|
-
this.getList${templateOptions.interfaceName}.bind(this)
|
|
167
|
-
);`);
|
|
168
|
-
template = template.replace(/{{getRoute}}/g, !templateOptions.validators[templateOptions.endpoint + "/{id}"].get ? `
|
|
169
|
-
// getRoute disabled` : `
|
|
170
|
-
this.router.get('/:id',
|
|
171
|
-
checkSchema(${util_1.default.inspect(templateOptions.validators[templateOptions.endpoint + "/{id}"].get, { depth: null }).replace(/^/gm, indent3)}),
|
|
172
|
-
this.get${templateOptions.interfaceName}.bind(this)
|
|
173
|
-
);`);
|
|
174
|
-
template = template.replace(/{{postRoute}}/g, !templateOptions.validators[templateOptions.endpoint].post ? `
|
|
175
|
-
// postRoute disabled` : `
|
|
176
|
-
this.router.post('/',
|
|
177
|
-
checkSchema(${util_1.default.inspect(templateOptions.validators[templateOptions.endpoint].post, { depth: null }).replace(/^/gm, indent3)}),
|
|
178
|
-
this.create${templateOptions.interfaceName}.bind(this)
|
|
179
|
-
);`);
|
|
180
|
-
template = template.replace(/{{putRoute}}/g, !templateOptions.validators[templateOptions.endpoint + "/{id}"].put ? `
|
|
181
|
-
// putRoute disabled` : `
|
|
182
|
-
this.router.put('/:id',
|
|
183
|
-
checkSchema(${util_1.default.inspect(templateOptions.validators[templateOptions.endpoint + "/{id}"].put, { depth: null }).replace(/^/gm, indent3)}),
|
|
184
|
-
this.replace${templateOptions.interfaceName}.bind(this)
|
|
185
|
-
);`);
|
|
186
|
-
template = template.replace(/{{patchRoute}}/g, !templateOptions.validators[templateOptions.endpoint + "/{id}"].patch ? `
|
|
187
|
-
// patchRoute disabled` : `
|
|
188
|
-
this.router.patch('/:id',
|
|
189
|
-
checkSchema(${util_1.default.inspect(templateOptions.validators[templateOptions.endpoint + "/{id}"].patch, { depth: null }).replace(/^/gm, indent3)}),
|
|
190
|
-
this.update${templateOptions.interfaceName}.bind(this)
|
|
191
|
-
);`);
|
|
192
|
-
template = template.replace(/{{deleteRoute}}/g, !templateOptions.validators[templateOptions.endpoint + "/{id}"].delete ? `
|
|
193
|
-
// deleteRoute disabled` : `
|
|
194
|
-
this.router.delete('/:id',
|
|
195
|
-
checkSchema(${util_1.default.inspect(templateOptions.validators[templateOptions.endpoint + "/{id}"].delete, { depth: null }).replace(/^/gm, indent3)}),
|
|
196
|
-
this.delete${templateOptions.interfaceName}.bind(this)
|
|
197
|
-
);`);
|
|
198
|
-
template = template.replace(/{{check_id}}/g, templateOptions.compilerOptions.app.allowApiCreateUpdate_id ? "" : `
|
|
199
|
-
if (req.body._id) {
|
|
200
|
-
delete req.body._id;
|
|
201
|
-
};`);
|
|
202
|
-
return template;
|
|
203
|
-
}
|
|
204
|
-
exports.default = controllerTemplate;
|
|
@@ -1,173 +0,0 @@
|
|
|
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 js_yaml_1 = __importDefault(require("js-yaml"));
|
|
32
|
-
const controller_template_1 = __importDefault(require("./controller.template"));
|
|
33
|
-
const logger_1 = __importDefault(require("../utils/logger"));
|
|
34
|
-
const common_1 = require("../utils/common");
|
|
35
|
-
const types = __importStar(require("../types/types"));
|
|
36
|
-
function compile(options) {
|
|
37
|
-
const file = fs_1.default.readFileSync(options.compilerOptions.openapiDir + "/" + options.openapiFile, "utf8");
|
|
38
|
-
const controllerToModelBasePath = (0, common_1.relativePath)(options.controllerOutDir, options.modelDir);
|
|
39
|
-
const openApi = js_yaml_1.default.load(file);
|
|
40
|
-
const endpointsValidator = {};
|
|
41
|
-
const writtedEndpoint = [];
|
|
42
|
-
Object.keys(openApi.paths).forEach((endpoint) => {
|
|
43
|
-
logger_1.default.process(`Controller : ${options.compilerOptions.openapiDir} > ${endpoint}`);
|
|
44
|
-
endpointsValidator[endpoint] = {};
|
|
45
|
-
Object.keys(openApi.paths[endpoint]).forEach((httpMethod) => {
|
|
46
|
-
if (!Object.values(types.httpMethodArr).includes(httpMethod))
|
|
47
|
-
return;
|
|
48
|
-
const methodEnum = httpMethod;
|
|
49
|
-
const validators = Object.assign(buildParamValidator(endpoint, methodEnum, openApi), buildBodyValidator(endpoint, methodEnum, openApi));
|
|
50
|
-
endpointsValidator[endpoint][httpMethod] = validators;
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
Object.keys(openApi.paths).forEach((endpoint) => {
|
|
54
|
-
const documentName = openApi.paths[endpoint]["x-collection"];
|
|
55
|
-
const interfaceName = openApi.paths[endpoint]["x-interface"];
|
|
56
|
-
const endpointFormatted = endpoint.replace("/{id}", "").toLowerCase();
|
|
57
|
-
if (documentName === undefined) {
|
|
58
|
-
logger_1.default.error(`"x-collection" not found in openapi spec's path "${endpoint}"\n - opanapi source: ${options.compilerOptions.openapiDir}`);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
else if (interfaceName === undefined) {
|
|
62
|
-
logger_1.default.error(`"x-interface" not found in openapi spec's path "${endpoint}"\n - opanapi source: ${options.compilerOptions.openapiDir}`);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
else if (writtedEndpoint.includes(endpointFormatted)) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
const outPath = `${options.controllerOutDir}/${interfaceName}Controller.gen.ts`;
|
|
70
|
-
const controllerToModelPath = `${controllerToModelBasePath}/${interfaceName}Model.gen`;
|
|
71
|
-
(0, common_1.writeFile)("Controller", outPath, (0, controller_template_1.default)({
|
|
72
|
-
endpoint: endpoint,
|
|
73
|
-
modelPath: controllerToModelPath,
|
|
74
|
-
interfaceName: interfaceName,
|
|
75
|
-
validators: endpointsValidator,
|
|
76
|
-
compilerOptions: options.compilerOptions,
|
|
77
|
-
}));
|
|
78
|
-
writtedEndpoint.push(endpointFormatted);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
exports.compile = compile;
|
|
83
|
-
function buildParamValidator(endpoint, httpMethod, openapi) {
|
|
84
|
-
const validators = {};
|
|
85
|
-
openapi.paths[endpoint][httpMethod].parameters.forEach((parameter) => {
|
|
86
|
-
if (parameter.name === "_id") {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
Object.assign(validators, processSchema({
|
|
90
|
-
fieldName: parameter.name,
|
|
91
|
-
required: parameter.required,
|
|
92
|
-
param: parameter,
|
|
93
|
-
}));
|
|
94
|
-
});
|
|
95
|
-
return validators;
|
|
96
|
-
}
|
|
97
|
-
function buildBodyValidator(endpoint, httpMethod, openapi) {
|
|
98
|
-
var _a, _b, _c, _d;
|
|
99
|
-
const validators = {};
|
|
100
|
-
const referencePath = (_c = (_b = (_a = openapi.paths[endpoint][httpMethod]) === null || _a === void 0 ? void 0 : _a.requestBody) === null || _b === void 0 ? void 0 : _b.content) === null || _c === void 0 ? void 0 : _c["application/json"].schema.$ref;
|
|
101
|
-
const reference = referencePath ? referencePath.split("/").pop() : undefined;
|
|
102
|
-
const components = reference ? openapi.components.schemas[reference] : undefined;
|
|
103
|
-
if (components !== undefined) {
|
|
104
|
-
for (const key in components.properties) {
|
|
105
|
-
Object.assign(validators, processSchema({
|
|
106
|
-
fieldName: key,
|
|
107
|
-
body: components.properties[key],
|
|
108
|
-
required: ((_d = components.required) === null || _d === void 0 ? void 0 : _d.includes(key)) || components.properties[key].required,
|
|
109
|
-
}));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return validators;
|
|
113
|
-
}
|
|
114
|
-
function processSchema(options) {
|
|
115
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1;
|
|
116
|
-
const validators = {
|
|
117
|
-
[options.fieldName]: {
|
|
118
|
-
in: options.param ? "params" : options.body ? "body" : [],
|
|
119
|
-
optional: !options.required ? { options: { values: "falsy", checkFalsy: true } } : false,
|
|
120
|
-
notEmpty: true,
|
|
121
|
-
},
|
|
122
|
-
};
|
|
123
|
-
const validatorParam = validators[options.fieldName];
|
|
124
|
-
const useBody = options.body !== undefined && options.param == undefined;
|
|
125
|
-
const type = useBody ? (_a = options.body) === null || _a === void 0 ? void 0 : _a.type : (_b = options.param) === null || _b === void 0 ? void 0 : _b.schema.type;
|
|
126
|
-
const useEnum = (_e = (_d = (_c = options.param) === null || _c === void 0 ? void 0 : _c.schema) === null || _d === void 0 ? void 0 : _d.enum) !== null && _e !== void 0 ? _e : (_f = options.body) === null || _f === void 0 ? void 0 : _f.enum;
|
|
127
|
-
const range = {
|
|
128
|
-
min: (_p = (_m = (_j = (_h = (_g = options.param) === null || _g === void 0 ? void 0 : _g.schema) === null || _h === void 0 ? void 0 : _h.minLength) !== null && _j !== void 0 ? _j : (_l = (_k = options.param) === null || _k === void 0 ? void 0 : _k.schema) === null || _l === void 0 ? void 0 : _l.minimum) !== null && _m !== void 0 ? _m : (_o = options.body) === null || _o === void 0 ? void 0 : _o.minLength) !== null && _p !== void 0 ? _p : (_q = options.body) === null || _q === void 0 ? void 0 : _q.minimum,
|
|
129
|
-
max: (_y = (_w = (_t = (_s = (_r = options.param) === null || _r === void 0 ? void 0 : _r.schema) === null || _s === void 0 ? void 0 : _s.maxLength) !== null && _t !== void 0 ? _t : (_v = (_u = options.param) === null || _u === void 0 ? void 0 : _u.schema) === null || _v === void 0 ? void 0 : _v.maximum) !== null && _w !== void 0 ? _w : (_x = options.body) === null || _x === void 0 ? void 0 : _x.maxLength) !== null && _y !== void 0 ? _y : (_z = options.body) === null || _z === void 0 ? void 0 : _z.maximum,
|
|
130
|
-
};
|
|
131
|
-
const rangeValidator = range.min && range.max ? { options: range } : undefined;
|
|
132
|
-
switch (type) {
|
|
133
|
-
case "string":
|
|
134
|
-
validatorParam.isString = true;
|
|
135
|
-
if (range.min && range.max) {
|
|
136
|
-
validatorParam.isLength = { options: range };
|
|
137
|
-
}
|
|
138
|
-
if (useEnum) {
|
|
139
|
-
validatorParam.isEmpty = false;
|
|
140
|
-
validatorParam.isIn = {
|
|
141
|
-
options: useEnum,
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
break;
|
|
145
|
-
case "integer":
|
|
146
|
-
validatorParam.isInt = rangeValidator;
|
|
147
|
-
break;
|
|
148
|
-
case "float":
|
|
149
|
-
validatorParam.isFloat = rangeValidator;
|
|
150
|
-
break;
|
|
151
|
-
case "number":
|
|
152
|
-
validatorParam.isNumeric = true;
|
|
153
|
-
break;
|
|
154
|
-
case "boolean":
|
|
155
|
-
validatorParam.isBoolean = true;
|
|
156
|
-
break;
|
|
157
|
-
case "array":
|
|
158
|
-
validatorParam.isArray = true;
|
|
159
|
-
break;
|
|
160
|
-
case "object":
|
|
161
|
-
if (useBody) {
|
|
162
|
-
for (const key in (_0 = options.body) === null || _0 === void 0 ? void 0 : _0.properties) {
|
|
163
|
-
Object.assign(validators, processSchema({
|
|
164
|
-
fieldName: options.fieldName + "." + key,
|
|
165
|
-
body: options.body.properties[key],
|
|
166
|
-
required: (_1 = options.body.required) === null || _1 === void 0 ? void 0 : _1.includes(key),
|
|
167
|
-
}));
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
return validators;
|
|
173
|
-
}
|
package/dist/generators/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
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 controller_template_1 = __importDefault(require("./controller.template"));
|
|
7
|
-
const routes_template_1 = __importDefault(require("./routes/routes.template"));
|
|
8
|
-
const server_template_1 = __importDefault(require("./server.template"));
|
|
9
|
-
exports.default = {
|
|
10
|
-
controllerTemplate: controller_template_1.default,
|
|
11
|
-
routesTemplate: routes_template_1.default,
|
|
12
|
-
serverTemplate: server_template_1.default,
|
|
13
|
-
};
|
|
@@ -1,227 +0,0 @@
|
|
|
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.compile = void 0;
|
|
7
|
-
const json_schema_to_openapi_schema_1 = __importDefault(require("json-schema-to-openapi-schema"));
|
|
8
|
-
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
9
|
-
const fs_1 = __importDefault(require("fs"));
|
|
10
|
-
const jsonSchema_1 = __importDefault(require("../utils/jsonSchema"));
|
|
11
|
-
const logger_1 = __importDefault(require("../utils/logger"));
|
|
12
|
-
const utils_1 = require("../utils");
|
|
13
|
-
function compile(openapiOutFileName, compilerOptions) {
|
|
14
|
-
let openapiJson = {
|
|
15
|
-
openapi: "3.0.0",
|
|
16
|
-
info: {
|
|
17
|
-
title: "veryExpress generated api server",
|
|
18
|
-
description: "OpenApi Spec generated by veryExpress",
|
|
19
|
-
version: "1.0.0"
|
|
20
|
-
},
|
|
21
|
-
paths: {},
|
|
22
|
-
components: {
|
|
23
|
-
schemas: {},
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
const files = fs_1.default.readdirSync(compilerOptions.jsonSchemaDir);
|
|
27
|
-
files.forEach((file) => {
|
|
28
|
-
if (!file.endsWith(".json"))
|
|
29
|
-
return;
|
|
30
|
-
const jsonSchemaFilePath = compilerOptions.jsonSchemaDir + "/" + file;
|
|
31
|
-
logger_1.default.process(`OpenApi : ${jsonSchemaFilePath}`);
|
|
32
|
-
const jsonSchemaBuffer = fs_1.default.readFileSync(`${compilerOptions.jsonSchemaDir}/${file}`);
|
|
33
|
-
const jsonSchema = JSON.parse(jsonSchemaBuffer.toString());
|
|
34
|
-
openapiJson = jsonToOpenapiPath(openapiJson, jsonSchema, { jsonSchemaFilePath: jsonSchemaFilePath });
|
|
35
|
-
openapiJson = jsonToOpenapiComponentSchema(openapiJson, jsonSchema, { jsonSchemaFilePath: jsonSchemaFilePath }, compilerOptions);
|
|
36
|
-
});
|
|
37
|
-
const validOpenApi = (0, json_schema_to_openapi_schema_1.default)(openapiJson, { version: 3.0 });
|
|
38
|
-
const openapiYaml = js_yaml_1.default.dump(validOpenApi);
|
|
39
|
-
const openapiOutFile = compilerOptions.openapiDir + openapiOutFileName;
|
|
40
|
-
(0, utils_1.writeFile)("OpenApi", openapiOutFile, openapiYaml);
|
|
41
|
-
}
|
|
42
|
-
exports.compile = compile;
|
|
43
|
-
function jsonToOpenapiPath(openapiJson, jsonSchema, additionalinfo) {
|
|
44
|
-
const documentConfig = jsonSchema["x-documentConfig"];
|
|
45
|
-
const lowerDocName = documentConfig.documentName.toLowerCase();
|
|
46
|
-
const interfaceName = documentConfig.interfaceName;
|
|
47
|
-
const properties = jsonSchema.properties;
|
|
48
|
-
const routes = {
|
|
49
|
-
["/" + lowerDocName]: {
|
|
50
|
-
"x-collection": documentConfig.documentName,
|
|
51
|
-
"x-interface": interfaceName,
|
|
52
|
-
},
|
|
53
|
-
["/" + lowerDocName + "/{id}"]: {
|
|
54
|
-
"x-collection": documentConfig.documentName,
|
|
55
|
-
"x-interface": interfaceName,
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
documentConfig.methods.forEach((jsonSchemaMethod) => {
|
|
59
|
-
const routeWithId = ["get", "put", "patch", "delete"].includes(jsonSchemaMethod);
|
|
60
|
-
const useBody = ["post", "put", "patch"].includes(jsonSchemaMethod);
|
|
61
|
-
const route = "/" + lowerDocName + (routeWithId ? "/{id}" : "");
|
|
62
|
-
const httpMethod = jsonSchema_1.default.httpMethod(jsonSchemaMethod, additionalinfo.jsonSchemaFilePath);
|
|
63
|
-
const parameters = [];
|
|
64
|
-
let requestBody = undefined;
|
|
65
|
-
const successResponse = {
|
|
66
|
-
200: {
|
|
67
|
-
description: "OK",
|
|
68
|
-
content: {
|
|
69
|
-
"application/json": { schema: { $ref: `#/components/schemas/${httpMethod}${interfaceName}Response` } },
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
if (routeWithId && properties["_id"]) {
|
|
74
|
-
const idParameter = {
|
|
75
|
-
name: "id",
|
|
76
|
-
in: "path",
|
|
77
|
-
description: properties["_id"].description,
|
|
78
|
-
required: true,
|
|
79
|
-
schema: {
|
|
80
|
-
type: properties["_id"].type,
|
|
81
|
-
format: properties["_id"].format,
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
parameters.push(idParameter);
|
|
85
|
-
}
|
|
86
|
-
if (httpMethod == "post") {
|
|
87
|
-
successResponse[201] = Object.assign({}, successResponse[200]);
|
|
88
|
-
successResponse[201].description = "Crerated";
|
|
89
|
-
delete successResponse[200];
|
|
90
|
-
}
|
|
91
|
-
else if (httpMethod == "delete") {
|
|
92
|
-
delete successResponse[200].content;
|
|
93
|
-
}
|
|
94
|
-
if (useBody) {
|
|
95
|
-
requestBody = {
|
|
96
|
-
description: `${httpMethod} ${documentConfig.documentName}`,
|
|
97
|
-
required: false,
|
|
98
|
-
content: {
|
|
99
|
-
"application/json": { schema: { $ref: `#/components/schemas/${httpMethod}${interfaceName}Body` } },
|
|
100
|
-
},
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
routes[route][httpMethod] = {
|
|
104
|
-
operationId: jsonSchemaMethod + interfaceName,
|
|
105
|
-
tags: [lowerDocName],
|
|
106
|
-
parameters: parameters,
|
|
107
|
-
requestBody: requestBody,
|
|
108
|
-
responses: Object.assign({
|
|
109
|
-
400: { description: "Bad Request", },
|
|
110
|
-
401: { description: "Unauthorized", },
|
|
111
|
-
403: { description: "Forbidden", },
|
|
112
|
-
404: { description: "Not Found", },
|
|
113
|
-
405: { description: "Method Not Allowed", },
|
|
114
|
-
413: { description: "Payload Too Large", },
|
|
115
|
-
429: { description: "Too Many Requests", },
|
|
116
|
-
500: { description: "Internal Server Error", },
|
|
117
|
-
502: { description: "Bad Gateway", },
|
|
118
|
-
503: { description: "Service Unavailable", },
|
|
119
|
-
}, successResponse)
|
|
120
|
-
};
|
|
121
|
-
});
|
|
122
|
-
openapiJson.paths = Object.assign(openapiJson.paths, routes);
|
|
123
|
-
return openapiJson;
|
|
124
|
-
}
|
|
125
|
-
function jsonToOpenapiComponentSchema(openapiJson, jsonSchema, additionalinfo, compilerOptions) {
|
|
126
|
-
const componentSchemaPath = {};
|
|
127
|
-
const documentConfig = jsonSchema["x-documentConfig"];
|
|
128
|
-
const lowerDocName = documentConfig.documentName.toLowerCase();
|
|
129
|
-
const interfaceName = documentConfig.interfaceName;
|
|
130
|
-
const componentSchemaResponse = {
|
|
131
|
-
type: "object",
|
|
132
|
-
properties: (0, json_schema_to_openapi_schema_1.default)(jsonSchema_1.default.cleanXcustomValue(jsonSchema.properties, ["index", "unique", "required"]), { version: 3.0 }),
|
|
133
|
-
};
|
|
134
|
-
const componentSchemaBodyRequired = {
|
|
135
|
-
type: "object",
|
|
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
|
-
required: jsonSchema.required,
|
|
138
|
-
};
|
|
139
|
-
const componentSchemaBodyRequiredWithoutId = {
|
|
140
|
-
type: "object",
|
|
141
|
-
properties: (0, json_schema_to_openapi_schema_1.default)(jsonSchema_1.default.cleanXcustomValue(componentSchemaBodyRequired.properties, { _id: "object" }), { version: 3.0 }),
|
|
142
|
-
required: jsonSchema.required,
|
|
143
|
-
};
|
|
144
|
-
const componentSchemaBody = {
|
|
145
|
-
type: "object",
|
|
146
|
-
properties: (0, json_schema_to_openapi_schema_1.default)(jsonSchema_1.default.cleanXcustomValue(componentSchemaBodyRequiredWithoutId.properties, ["required"]), { version: 3.0 }),
|
|
147
|
-
};
|
|
148
|
-
documentConfig.methods.forEach((jsonSchemaMethod) => {
|
|
149
|
-
const httpMethod = jsonSchema_1.default.httpMethod(jsonSchemaMethod, additionalinfo.jsonSchemaFilePath);
|
|
150
|
-
switch (jsonSchemaMethod) {
|
|
151
|
-
case "delete":
|
|
152
|
-
break;
|
|
153
|
-
case "get":
|
|
154
|
-
componentSchemaPath[httpMethod + interfaceName + "Response"] = componentSchemaResponse;
|
|
155
|
-
break;
|
|
156
|
-
case "getList": {
|
|
157
|
-
const parameters = [];
|
|
158
|
-
Object.keys(jsonSchema.properties).forEach((key) => {
|
|
159
|
-
const props = jsonSchema.properties[key];
|
|
160
|
-
if (props.type == "object")
|
|
161
|
-
return;
|
|
162
|
-
parameters.push({
|
|
163
|
-
name: key,
|
|
164
|
-
in: "query",
|
|
165
|
-
required: false,
|
|
166
|
-
schema: {
|
|
167
|
-
type: props.type,
|
|
168
|
-
format: props.format,
|
|
169
|
-
"x-format": props["x-format"],
|
|
170
|
-
minLength: props.minLength,
|
|
171
|
-
maxLength: props.maxLength,
|
|
172
|
-
minimum: props.minimum,
|
|
173
|
-
maximum: props.maximum,
|
|
174
|
-
enum: props.enum,
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
switch (props["x-format"]) {
|
|
178
|
-
case "minMax":
|
|
179
|
-
parameters.push({
|
|
180
|
-
name: "min_" + key,
|
|
181
|
-
in: "query",
|
|
182
|
-
required: false,
|
|
183
|
-
schema: {
|
|
184
|
-
type: "number",
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
parameters.push({
|
|
188
|
-
name: "max_" + key,
|
|
189
|
-
in: "query",
|
|
190
|
-
required: false,
|
|
191
|
-
schema: {
|
|
192
|
-
type: "number",
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
break;
|
|
196
|
-
default:
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
openapiJson.paths["/" + lowerDocName][httpMethod].parameters = parameters;
|
|
201
|
-
componentSchemaPath[httpMethod + interfaceName + "Response"] = componentSchemaResponse;
|
|
202
|
-
componentSchemaPath[httpMethod + interfaceName + "ResponseList"] = {
|
|
203
|
-
type: "array",
|
|
204
|
-
items: componentSchemaResponse,
|
|
205
|
-
};
|
|
206
|
-
break;
|
|
207
|
-
}
|
|
208
|
-
case "patch":
|
|
209
|
-
componentSchemaPath[httpMethod + interfaceName + "Body"] = componentSchemaBody;
|
|
210
|
-
componentSchemaPath[httpMethod + interfaceName + "Response"] = componentSchemaResponse;
|
|
211
|
-
break;
|
|
212
|
-
case "post":
|
|
213
|
-
componentSchemaPath[httpMethod + interfaceName + "Body"] = compilerOptions.app.allowApiCreateUpdate_id ? componentSchemaBodyRequired : componentSchemaBodyRequiredWithoutId;
|
|
214
|
-
componentSchemaPath[httpMethod + interfaceName + "Response"] = componentSchemaResponse;
|
|
215
|
-
break;
|
|
216
|
-
case "put":
|
|
217
|
-
componentSchemaPath[httpMethod + interfaceName + "Body"] = componentSchemaBodyRequired;
|
|
218
|
-
componentSchemaPath[httpMethod + interfaceName + "Response"] = componentSchemaResponse;
|
|
219
|
-
break;
|
|
220
|
-
default:
|
|
221
|
-
break;
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
openapiJson.components.schemas = Object.assign(openapiJson.components.schemas, componentSchemaPath);
|
|
225
|
-
return openapiJson;
|
|
226
|
-
}
|
|
227
|
-
exports.default = compile;
|
|
@@ -1,42 +0,0 @@
|
|
|
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.compile = void 0;
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const logger_1 = __importDefault(require("../utils/logger"));
|
|
9
|
-
const utils_1 = require("../utils");
|
|
10
|
-
function compile(packageOutPath, schemaDir, outDir) {
|
|
11
|
-
logger_1.default.process(`Project : ${packageOutPath}`);
|
|
12
|
-
let packjson = "";
|
|
13
|
-
if (!fs_1.default.existsSync(packageOutPath)) {
|
|
14
|
-
packjson = fs_1.default.readFileSync(__dirname + "/../templates/root/package.json").toString();
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
packjson = fs_1.default.readFileSync(packageOutPath).toString();
|
|
18
|
-
}
|
|
19
|
-
const packageJson = JSON.parse(packjson);
|
|
20
|
-
const vexgenScript = `vex -j ${schemaDir} -o ${outDir}`;
|
|
21
|
-
if (packageJson.scripts.vexgen !== vexgenScript) {
|
|
22
|
-
logger_1.default.process(`package.json : Update script : vexgen > ${packageJson.scripts.vexgen}`);
|
|
23
|
-
packageJson.scripts.vexgen = vexgenScript;
|
|
24
|
-
}
|
|
25
|
-
if (!packageJson.scripts.build) {
|
|
26
|
-
logger_1.default.process(`package.json : Update script : build > ${packageJson.scripts.build}`);
|
|
27
|
-
packageJson.scripts.build = "tsc -p .";
|
|
28
|
-
}
|
|
29
|
-
if (!packageJson.scripts.dev) {
|
|
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`;
|
|
32
|
-
}
|
|
33
|
-
if (!packageJson.scripts.start) {
|
|
34
|
-
logger_1.default.process(`package.json : Update script : start > ${packageJson.scripts.start}`);
|
|
35
|
-
packageJson.scripts.start = "node ./dist/server.js";
|
|
36
|
-
}
|
|
37
|
-
(0, utils_1.writeFile)(`Project : ${packageOutPath}`, packageOutPath, JSON.stringify(packageJson, null, 4));
|
|
38
|
-
}
|
|
39
|
-
exports.compile = compile;
|
|
40
|
-
exports.default = {
|
|
41
|
-
compile
|
|
42
|
-
};
|