@asad_dev/leo-generator 1.6.0
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/CHANGELOG.md +194 -0
- package/COMMAND_REFERENCE.md +412 -0
- package/README.md +486 -0
- package/dist/app/modules/imagemodule/imagemodule.constants.js +18 -0
- package/dist/app/modules/imagemodule/imagemodule.controller.js +98 -0
- package/dist/app/modules/imagemodule/imagemodule.interface.js +2 -0
- package/dist/app/modules/imagemodule/imagemodule.model.js +10 -0
- package/dist/app/modules/imagemodule/imagemodule.route.js +20 -0
- package/dist/app/modules/imagemodule/imagemodule.service.js +137 -0
- package/dist/app/modules/imagemodule/imagemodule.validation.js +12 -0
- package/dist/app/modules/skiptest/skiptest.controller.js +81 -0
- package/dist/app/modules/skiptest/skiptest.route.js +19 -0
- package/dist/app/modules/skiptest/skiptest.service.js +129 -0
- package/dist/app/modules/skiptest/skiptest.validation.js +12 -0
- package/dist/app/modules/testmodule/testmodule.constants.js +18 -0
- package/dist/app/modules/testmodule/testmodule.controller.js +81 -0
- package/dist/app/modules/testmodule/testmodule.interface.js +2 -0
- package/dist/app/modules/testmodule/testmodule.model.js +11 -0
- package/dist/app/modules/testmodule/testmodule.route.js +19 -0
- package/dist/app/modules/testmodule/testmodule.service.js +129 -0
- package/dist/app/modules/testmodule/testmodule.validation.js +14 -0
- package/dist/helpers/fileHelper.js +44 -0
- package/dist/index.js +586 -0
- package/dist/templates/constants.template.js +24 -0
- package/dist/templates/controller.template.js +108 -0
- package/dist/templates/route.template.js +68 -0
- package/dist/templates/service.template.js +184 -0
- package/dist/types.js +2 -0
- package/dist/utils/documentationUpdater.js +430 -0
- package/dist/utils/fieldParser.js +163 -0
- package/dist/utils/helperGenerator.js +87 -0
- package/dist/utils/interfaceGenerator.js +158 -0
- package/dist/utils/modelGenerator.js +140 -0
- package/dist/utils/postmanApi.js +113 -0
- package/dist/utils/postmanGenerator.js +283 -0
- package/dist/utils/swaggerGenerator.js +444 -0
- package/dist/utils/validationGenerator.js +170 -0
- package/package.json +58 -0
|
@@ -0,0 +1,444 @@
|
|
|
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 () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.generateSwaggerPaths = generateSwaggerPaths;
|
|
37
|
+
exports.generateSwaggerSchemas = generateSwaggerSchemas;
|
|
38
|
+
exports.updateSwaggerFile = updateSwaggerFile;
|
|
39
|
+
const fs = __importStar(require("fs"));
|
|
40
|
+
const path = __importStar(require("path"));
|
|
41
|
+
function generateSwaggerPaths(moduleName, fields) {
|
|
42
|
+
const camelCaseName = toCamelCase(moduleName);
|
|
43
|
+
const folderName = camelCaseName.toLowerCase();
|
|
44
|
+
const schema = generateSwaggerSchema(camelCaseName, fields);
|
|
45
|
+
const createSchema = generateSwaggerCreateSchema(camelCaseName, fields);
|
|
46
|
+
const updateSchema = generateSwaggerUpdateSchema(camelCaseName, fields);
|
|
47
|
+
return {
|
|
48
|
+
[`/${folderName}`]: {
|
|
49
|
+
post: {
|
|
50
|
+
tags: [camelCaseName],
|
|
51
|
+
summary: `Create a new ${camelCaseName}`,
|
|
52
|
+
requestBody: {
|
|
53
|
+
required: true,
|
|
54
|
+
content: {
|
|
55
|
+
"application/json": {
|
|
56
|
+
schema: {
|
|
57
|
+
$ref: `#/components/schemas/${camelCaseName}Create`
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
responses: {
|
|
63
|
+
"201": {
|
|
64
|
+
description: `${camelCaseName} created successfully`,
|
|
65
|
+
content: {
|
|
66
|
+
"application/json": {
|
|
67
|
+
schema: {
|
|
68
|
+
type: "object",
|
|
69
|
+
properties: {
|
|
70
|
+
success: { type: "boolean" },
|
|
71
|
+
message: { type: "string" },
|
|
72
|
+
data: {
|
|
73
|
+
$ref: `#/components/schemas/${camelCaseName}`
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
"400": {
|
|
81
|
+
description: "Bad request"
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
get: {
|
|
86
|
+
tags: [camelCaseName],
|
|
87
|
+
summary: `Get all ${camelCaseName}s`,
|
|
88
|
+
responses: {
|
|
89
|
+
"200": {
|
|
90
|
+
description: `List of ${camelCaseName}s retrieved successfully`,
|
|
91
|
+
content: {
|
|
92
|
+
"application/json": {
|
|
93
|
+
schema: {
|
|
94
|
+
type: "object",
|
|
95
|
+
properties: {
|
|
96
|
+
success: { type: "boolean" },
|
|
97
|
+
message: { type: "string" },
|
|
98
|
+
data: {
|
|
99
|
+
type: "array",
|
|
100
|
+
items: {
|
|
101
|
+
$ref: `#/components/schemas/${camelCaseName}`
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
[`/${folderName}/{id}`]: {
|
|
113
|
+
get: {
|
|
114
|
+
tags: [camelCaseName],
|
|
115
|
+
summary: `Get ${camelCaseName} by ID`,
|
|
116
|
+
parameters: [
|
|
117
|
+
{
|
|
118
|
+
name: "id",
|
|
119
|
+
in: "path",
|
|
120
|
+
required: true,
|
|
121
|
+
schema: {
|
|
122
|
+
type: "string"
|
|
123
|
+
},
|
|
124
|
+
description: `${camelCaseName} ID`
|
|
125
|
+
}
|
|
126
|
+
],
|
|
127
|
+
responses: {
|
|
128
|
+
"200": {
|
|
129
|
+
description: `${camelCaseName} retrieved successfully`,
|
|
130
|
+
content: {
|
|
131
|
+
"application/json": {
|
|
132
|
+
schema: {
|
|
133
|
+
type: "object",
|
|
134
|
+
properties: {
|
|
135
|
+
success: { type: "boolean" },
|
|
136
|
+
message: { type: "string" },
|
|
137
|
+
data: {
|
|
138
|
+
$ref: `#/components/schemas/${camelCaseName}`
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
"404": {
|
|
146
|
+
description: `${camelCaseName} not found`
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
patch: {
|
|
151
|
+
tags: [camelCaseName],
|
|
152
|
+
summary: `Update ${camelCaseName}`,
|
|
153
|
+
parameters: [
|
|
154
|
+
{
|
|
155
|
+
name: "id",
|
|
156
|
+
in: "path",
|
|
157
|
+
required: true,
|
|
158
|
+
schema: {
|
|
159
|
+
type: "string"
|
|
160
|
+
},
|
|
161
|
+
description: `${camelCaseName} ID`
|
|
162
|
+
}
|
|
163
|
+
],
|
|
164
|
+
requestBody: {
|
|
165
|
+
required: true,
|
|
166
|
+
content: {
|
|
167
|
+
"application/json": {
|
|
168
|
+
schema: {
|
|
169
|
+
$ref: `#/components/schemas/${camelCaseName}Update`
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
responses: {
|
|
175
|
+
"200": {
|
|
176
|
+
description: `${camelCaseName} updated successfully`,
|
|
177
|
+
content: {
|
|
178
|
+
"application/json": {
|
|
179
|
+
schema: {
|
|
180
|
+
type: "object",
|
|
181
|
+
properties: {
|
|
182
|
+
success: { type: "boolean" },
|
|
183
|
+
message: { type: "string" },
|
|
184
|
+
data: {
|
|
185
|
+
$ref: `#/components/schemas/${camelCaseName}`
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
"404": {
|
|
193
|
+
description: `${camelCaseName} not found`
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
delete: {
|
|
198
|
+
tags: [camelCaseName],
|
|
199
|
+
summary: `Delete ${camelCaseName}`,
|
|
200
|
+
parameters: [
|
|
201
|
+
{
|
|
202
|
+
name: "id",
|
|
203
|
+
in: "path",
|
|
204
|
+
required: true,
|
|
205
|
+
schema: {
|
|
206
|
+
type: "string"
|
|
207
|
+
},
|
|
208
|
+
description: `${camelCaseName} ID`
|
|
209
|
+
}
|
|
210
|
+
],
|
|
211
|
+
responses: {
|
|
212
|
+
"200": {
|
|
213
|
+
description: `${camelCaseName} deleted successfully`,
|
|
214
|
+
content: {
|
|
215
|
+
"application/json": {
|
|
216
|
+
schema: {
|
|
217
|
+
type: "object",
|
|
218
|
+
properties: {
|
|
219
|
+
success: { type: "boolean" },
|
|
220
|
+
message: { type: "string" },
|
|
221
|
+
data: {
|
|
222
|
+
$ref: `#/components/schemas/${camelCaseName}`
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
"404": {
|
|
230
|
+
description: `${camelCaseName} not found`
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
function generateSwaggerSchemas(moduleName, fields) {
|
|
238
|
+
const camelCaseName = toCamelCase(moduleName);
|
|
239
|
+
const schemas = {};
|
|
240
|
+
// Generate nested schemas for complex types
|
|
241
|
+
fields.forEach(field => {
|
|
242
|
+
var _a, _b;
|
|
243
|
+
if (field.type.toLowerCase() === "array" &&
|
|
244
|
+
((_a = field.ref) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === "object" &&
|
|
245
|
+
((_b = field.objectProperties) === null || _b === void 0 ? void 0 : _b.length)) {
|
|
246
|
+
const nestedSchemaName = `${toCamelCase(field.name)}Item`;
|
|
247
|
+
schemas[nestedSchemaName] = generateSwaggerSchema(nestedSchemaName, field.objectProperties);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
// Main schema
|
|
251
|
+
schemas[camelCaseName] = generateSwaggerSchema(camelCaseName, fields);
|
|
252
|
+
schemas[`${camelCaseName}Create`] = generateSwaggerCreateSchema(camelCaseName, fields);
|
|
253
|
+
schemas[`${camelCaseName}Update`] = generateSwaggerUpdateSchema(camelCaseName, fields);
|
|
254
|
+
return schemas;
|
|
255
|
+
}
|
|
256
|
+
function generateSwaggerSchema(schemaName, fields) {
|
|
257
|
+
const properties = {
|
|
258
|
+
_id: {
|
|
259
|
+
type: "string",
|
|
260
|
+
description: "MongoDB ObjectId"
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
const required = ["_id"];
|
|
264
|
+
fields.forEach(field => {
|
|
265
|
+
if (field.name.toLowerCase() === "_id")
|
|
266
|
+
return;
|
|
267
|
+
properties[field.name] = mapFieldToSwaggerProperty(field);
|
|
268
|
+
if (field.isRequired) {
|
|
269
|
+
required.push(field.name);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
// Add timestamps
|
|
273
|
+
properties.createdAt = {
|
|
274
|
+
type: "string",
|
|
275
|
+
format: "date-time",
|
|
276
|
+
description: "Creation timestamp"
|
|
277
|
+
};
|
|
278
|
+
properties.updatedAt = {
|
|
279
|
+
type: "string",
|
|
280
|
+
format: "date-time",
|
|
281
|
+
description: "Last update timestamp"
|
|
282
|
+
};
|
|
283
|
+
return {
|
|
284
|
+
type: "object",
|
|
285
|
+
properties,
|
|
286
|
+
required
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
function generateSwaggerCreateSchema(schemaName, fields) {
|
|
290
|
+
const properties = {};
|
|
291
|
+
const required = [];
|
|
292
|
+
fields.forEach(field => {
|
|
293
|
+
if (field.name.toLowerCase() === "_id")
|
|
294
|
+
return;
|
|
295
|
+
properties[field.name] = mapFieldToSwaggerProperty(field);
|
|
296
|
+
if (field.isRequired) {
|
|
297
|
+
required.push(field.name);
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
return {
|
|
301
|
+
type: "object",
|
|
302
|
+
properties,
|
|
303
|
+
required
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
function generateSwaggerUpdateSchema(schemaName, fields) {
|
|
307
|
+
const properties = {};
|
|
308
|
+
fields.forEach(field => {
|
|
309
|
+
if (field.name.toLowerCase() === "_id")
|
|
310
|
+
return;
|
|
311
|
+
properties[field.name] = mapFieldToSwaggerProperty(field);
|
|
312
|
+
});
|
|
313
|
+
return {
|
|
314
|
+
type: "object",
|
|
315
|
+
properties
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function mapFieldToSwaggerProperty(field) {
|
|
319
|
+
var _a, _b, _c, _d;
|
|
320
|
+
switch (field.type.toLowerCase()) {
|
|
321
|
+
case "string":
|
|
322
|
+
return {
|
|
323
|
+
type: "string",
|
|
324
|
+
description: `${field.name} field`
|
|
325
|
+
};
|
|
326
|
+
case "number":
|
|
327
|
+
return {
|
|
328
|
+
type: "number",
|
|
329
|
+
description: `${field.name} field`
|
|
330
|
+
};
|
|
331
|
+
case "boolean":
|
|
332
|
+
return {
|
|
333
|
+
type: "boolean",
|
|
334
|
+
description: `${field.name} field`
|
|
335
|
+
};
|
|
336
|
+
case "date":
|
|
337
|
+
return {
|
|
338
|
+
type: "string",
|
|
339
|
+
format: "date-time",
|
|
340
|
+
description: `${field.name} field`
|
|
341
|
+
};
|
|
342
|
+
case "enum":
|
|
343
|
+
return {
|
|
344
|
+
type: "string",
|
|
345
|
+
enum: field.enumValues || [],
|
|
346
|
+
description: `${field.name} field`
|
|
347
|
+
};
|
|
348
|
+
case "array":
|
|
349
|
+
if (((_a = field.ref) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === "object" && ((_b = field.objectProperties) === null || _b === void 0 ? void 0 : _b.length)) {
|
|
350
|
+
const nestedSchemaName = `${toCamelCase(field.name)}Item`;
|
|
351
|
+
return {
|
|
352
|
+
type: "array",
|
|
353
|
+
items: {
|
|
354
|
+
$ref: `#/components/schemas/${nestedSchemaName}`
|
|
355
|
+
},
|
|
356
|
+
description: `Array of ${field.name} objects`
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
else if (((_c = field.arrayItemType) === null || _c === void 0 ? void 0 : _c.toLowerCase()) === "objectid") {
|
|
360
|
+
return {
|
|
361
|
+
type: "array",
|
|
362
|
+
items: {
|
|
363
|
+
type: "string"
|
|
364
|
+
},
|
|
365
|
+
description: `Array of ${field.name} references`
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
return {
|
|
370
|
+
type: "array",
|
|
371
|
+
items: {
|
|
372
|
+
type: "string"
|
|
373
|
+
},
|
|
374
|
+
description: `Array of ${field.name} items`
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
case "object":
|
|
378
|
+
if ((_d = field.objectProperties) === null || _d === void 0 ? void 0 : _d.length) {
|
|
379
|
+
const nestedProperties = {};
|
|
380
|
+
field.objectProperties.forEach(prop => {
|
|
381
|
+
nestedProperties[prop.name] = mapFieldToSwaggerProperty(prop);
|
|
382
|
+
});
|
|
383
|
+
return {
|
|
384
|
+
type: "object",
|
|
385
|
+
properties: nestedProperties,
|
|
386
|
+
description: `${field.name} object`
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
return {
|
|
391
|
+
type: "object",
|
|
392
|
+
description: `${field.name} object`
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
case "objectid":
|
|
396
|
+
case "id":
|
|
397
|
+
return {
|
|
398
|
+
type: "string",
|
|
399
|
+
description: `${field.name} reference ID`
|
|
400
|
+
};
|
|
401
|
+
default:
|
|
402
|
+
return {
|
|
403
|
+
type: "string",
|
|
404
|
+
description: `${field.name} field`
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
function updateSwaggerFile(moduleName, fields, swaggerFilePath = "swagger.json") {
|
|
409
|
+
const fullPath = path.join(process.cwd(), swaggerFilePath);
|
|
410
|
+
let swaggerDoc = {
|
|
411
|
+
openapi: "3.0.0",
|
|
412
|
+
info: {
|
|
413
|
+
title: "API Documentation",
|
|
414
|
+
version: "1.0.0",
|
|
415
|
+
description: "Generated API documentation"
|
|
416
|
+
},
|
|
417
|
+
paths: {},
|
|
418
|
+
components: {
|
|
419
|
+
schemas: {}
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
// Load existing swagger file if it exists
|
|
423
|
+
if (fs.existsSync(fullPath)) {
|
|
424
|
+
try {
|
|
425
|
+
const existingContent = fs.readFileSync(fullPath, "utf-8");
|
|
426
|
+
swaggerDoc = JSON.parse(existingContent);
|
|
427
|
+
}
|
|
428
|
+
catch (error) {
|
|
429
|
+
console.warn("Could not parse existing swagger file, creating new one");
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
// Generate new paths and schemas
|
|
433
|
+
const newPaths = generateSwaggerPaths(moduleName, fields);
|
|
434
|
+
const newSchemas = generateSwaggerSchemas(moduleName, fields);
|
|
435
|
+
// Merge with existing
|
|
436
|
+
swaggerDoc.paths = Object.assign(Object.assign({}, swaggerDoc.paths), newPaths);
|
|
437
|
+
swaggerDoc.components.schemas = Object.assign(Object.assign({}, swaggerDoc.components.schemas), newSchemas);
|
|
438
|
+
// Write updated swagger file
|
|
439
|
+
fs.writeFileSync(fullPath, JSON.stringify(swaggerDoc, null, 2));
|
|
440
|
+
console.log(`✅ Swagger documentation updated: ${fullPath}`);
|
|
441
|
+
}
|
|
442
|
+
function toCamelCase(str) {
|
|
443
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
444
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateValidationContent = generateValidationContent;
|
|
4
|
+
function generateValidationContent(camelCaseName, fields) {
|
|
5
|
+
let validationContent = `import { z } from 'zod';\n\n`;
|
|
6
|
+
// Add nested schemas for array of objects
|
|
7
|
+
fields.forEach((field) => {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
if (field.type.toLowerCase() === "array" &&
|
|
10
|
+
((_a = field.ref) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === "object" &&
|
|
11
|
+
((_b = field.objectProperties) === null || _b === void 0 ? void 0 : _b.length)) {
|
|
12
|
+
const nestedSchemaName = `${field.name}ItemSchema`;
|
|
13
|
+
validationContent += `const ${nestedSchemaName} = z.object({\n`;
|
|
14
|
+
// Add properties from the objectProperties array
|
|
15
|
+
field.objectProperties.forEach((prop) => {
|
|
16
|
+
// Skip _id field
|
|
17
|
+
if (prop.name.toLowerCase() === "_id") {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
let zodType = mapToZodType(prop.type, prop);
|
|
21
|
+
// Add required/optional modifiers
|
|
22
|
+
if (prop.isRequired) {
|
|
23
|
+
// Already required by default in Zod
|
|
24
|
+
}
|
|
25
|
+
else if (prop.isOptional) {
|
|
26
|
+
zodType += ".optional()";
|
|
27
|
+
}
|
|
28
|
+
validationContent += ` ${prop.name}: ${zodType},\n`;
|
|
29
|
+
});
|
|
30
|
+
validationContent += `});\n\n`;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
validationContent += `export const ${camelCaseName}Validations = {\n`;
|
|
34
|
+
// Create validation schema for create operation
|
|
35
|
+
validationContent += ` create: z.object({\n`;
|
|
36
|
+
validationContent += ` body: z.object({\n`;
|
|
37
|
+
// Add validation for each field
|
|
38
|
+
if (fields.length > 0) {
|
|
39
|
+
fields.forEach((field) => {
|
|
40
|
+
// Skip _id field
|
|
41
|
+
if (field.name.toLowerCase() === "_id") {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
let zodType = mapToZodType(field.type, field);
|
|
45
|
+
// Add required/optional modifiers
|
|
46
|
+
if (field.isRequired) {
|
|
47
|
+
// Already required by default in Zod
|
|
48
|
+
}
|
|
49
|
+
else if (field.isOptional) {
|
|
50
|
+
zodType += ".optional()";
|
|
51
|
+
}
|
|
52
|
+
validationContent += ` ${field.name}: ${zodType},\n`;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
validationContent += ` // Add validation fields\n`;
|
|
57
|
+
}
|
|
58
|
+
validationContent += ` }),\n`;
|
|
59
|
+
// Add params validation for ID
|
|
60
|
+
validationContent += ` params: z.object({\n`;
|
|
61
|
+
validationContent += ` id: z.string(),\n`;
|
|
62
|
+
validationContent += ` }).optional(),\n`;
|
|
63
|
+
validationContent += ` }),\n\n`;
|
|
64
|
+
// Add update validation schema (similar to create but all fields optional)
|
|
65
|
+
validationContent += ` update: z.object({\n`;
|
|
66
|
+
validationContent += ` body: z.object({\n`;
|
|
67
|
+
if (fields.length > 0) {
|
|
68
|
+
fields.forEach((field) => {
|
|
69
|
+
// Skip _id field
|
|
70
|
+
if (field.name.toLowerCase() === "_id") {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
let zodType = mapToZodType(field.type, field);
|
|
74
|
+
// All fields are optional in update
|
|
75
|
+
zodType += ".optional()";
|
|
76
|
+
validationContent += ` ${field.name}: ${zodType},\n`;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
validationContent += ` // Add validation fields\n`;
|
|
81
|
+
}
|
|
82
|
+
validationContent += ` }),\n`;
|
|
83
|
+
// Add params validation for ID
|
|
84
|
+
validationContent += ` params: z.object({\n`;
|
|
85
|
+
validationContent += ` id: z.string(),\n`;
|
|
86
|
+
validationContent += ` }),\n`;
|
|
87
|
+
validationContent += ` }),\n\n`;
|
|
88
|
+
// Add getById validation schema
|
|
89
|
+
validationContent += ` getById: z.object({\n`;
|
|
90
|
+
validationContent += ` params: z.object({\n`;
|
|
91
|
+
validationContent += ` id: z.string(),\n`;
|
|
92
|
+
validationContent += ` }),\n`;
|
|
93
|
+
validationContent += ` }),\n\n`;
|
|
94
|
+
// Add getAll validation schema with pagination
|
|
95
|
+
validationContent += ` getAll: z.object({\n`;
|
|
96
|
+
validationContent += ` query: z.object({\n`;
|
|
97
|
+
validationContent += ` page: z.string().optional(),\n`;
|
|
98
|
+
validationContent += ` limit: z.string().optional(),\n`;
|
|
99
|
+
validationContent += ` sortBy: z.string().optional(),\n`;
|
|
100
|
+
validationContent += ` sortOrder: z.string().optional(),\n`;
|
|
101
|
+
validationContent += ` }).optional(),\n`;
|
|
102
|
+
validationContent += ` }),\n\n`;
|
|
103
|
+
// Add delete validation schema
|
|
104
|
+
validationContent += ` delete: z.object({\n`;
|
|
105
|
+
validationContent += ` params: z.object({\n`;
|
|
106
|
+
validationContent += ` id: z.string(),\n`;
|
|
107
|
+
validationContent += ` }),\n`;
|
|
108
|
+
validationContent += ` }),\n`;
|
|
109
|
+
validationContent += `};\n`;
|
|
110
|
+
return validationContent;
|
|
111
|
+
}
|
|
112
|
+
// Helper function to map types to Zod validators
|
|
113
|
+
function mapToZodType(type, field) {
|
|
114
|
+
var _a;
|
|
115
|
+
switch (type.toLowerCase()) {
|
|
116
|
+
case "string":
|
|
117
|
+
if ((field === null || field === void 0 ? void 0 : field.enumValues) && field.enumValues.length > 0) {
|
|
118
|
+
return `z.enum(['${field.enumValues.join("', '")}'])`;
|
|
119
|
+
}
|
|
120
|
+
return "z.string()";
|
|
121
|
+
case "number":
|
|
122
|
+
return "z.number()";
|
|
123
|
+
case "boolean":
|
|
124
|
+
return "z.boolean()";
|
|
125
|
+
case "date":
|
|
126
|
+
return "z.string().datetime()";
|
|
127
|
+
case "enum":
|
|
128
|
+
if ((field === null || field === void 0 ? void 0 : field.enumValues) && field.enumValues.length > 0) {
|
|
129
|
+
return `z.enum(['${field.enumValues.join("', '")}'])`;
|
|
130
|
+
}
|
|
131
|
+
return "z.string()";
|
|
132
|
+
case "array":
|
|
133
|
+
if (field === null || field === void 0 ? void 0 : field.ref) {
|
|
134
|
+
if (field.ref.toLowerCase() === "object" &&
|
|
135
|
+
((_a = field.objectProperties) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
136
|
+
// Array of objects with defined structure
|
|
137
|
+
const nestedSchemaName = `${field.name}ItemSchema`;
|
|
138
|
+
return `z.array(${nestedSchemaName})`;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
// Array of references to other models (e.g., products:array:objectid:Product)
|
|
142
|
+
return "z.array(z.string())";
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
else if (field === null || field === void 0 ? void 0 : field.arrayItemType) {
|
|
146
|
+
if (field.arrayItemType.toLowerCase() === "string") {
|
|
147
|
+
return "z.array(z.string())";
|
|
148
|
+
}
|
|
149
|
+
else if (field.arrayItemType.toLowerCase() === "number") {
|
|
150
|
+
return "z.array(z.number())";
|
|
151
|
+
}
|
|
152
|
+
else if (field.arrayItemType.toLowerCase() === "boolean") {
|
|
153
|
+
return "z.array(z.boolean())";
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
return "z.array(z.string())";
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
return "z.array(z.any())";
|
|
161
|
+
}
|
|
162
|
+
case "object":
|
|
163
|
+
return "z.record(z.string(), z.any())";
|
|
164
|
+
case "objectid":
|
|
165
|
+
case "id":
|
|
166
|
+
return "z.string()"; // ObjectId as string
|
|
167
|
+
default:
|
|
168
|
+
return "z.any()";
|
|
169
|
+
}
|
|
170
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@asad_dev/leo-generator",
|
|
3
|
+
"version": "1.6.0",
|
|
4
|
+
"description": "Enhanced Express module generator with Mongoose models, automated Postman Cloud sync, and Swagger documentation",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"leo-generate": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"ENHANCEMENTS.md",
|
|
13
|
+
"CHANGELOG.md",
|
|
14
|
+
"COMMAND_REFERENCE.md",
|
|
15
|
+
"SYNTAX_SUPPORT_SUMMARY.md"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/unknow69/leo-generator.git"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/unknow69/leo-generator#readme",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/unknow69/leo-generator/issues"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc && npm run copy-templates",
|
|
27
|
+
"copy-templates": "if not exist dist\\templates mkdir dist\\templates && xcopy /E /I /Y src\\templates\\*.hbs dist\\templates\\",
|
|
28
|
+
"prepublishOnly": "npm run build",
|
|
29
|
+
"postinstall": "node -e \"try { require('fs').chmodSync(require('path').join(__dirname, 'dist/index.js'), '755') } catch(e) {}\""
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"leo",
|
|
33
|
+
"messi",
|
|
34
|
+
"goat",
|
|
35
|
+
"express",
|
|
36
|
+
"mongoose",
|
|
37
|
+
"generator",
|
|
38
|
+
"module",
|
|
39
|
+
"cli",
|
|
40
|
+
"postman",
|
|
41
|
+
"swagger",
|
|
42
|
+
"api",
|
|
43
|
+
"documentation",
|
|
44
|
+
"typescript",
|
|
45
|
+
"crud"
|
|
46
|
+
],
|
|
47
|
+
"author": "Asaduzzaman",
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"commander": "^11.1.0",
|
|
51
|
+
"handlebars": "^4.7.8"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/node": "^20.10.5",
|
|
55
|
+
"@types/handlebars": "^4.1.0",
|
|
56
|
+
"typescript": "^5.3.3"
|
|
57
|
+
}
|
|
58
|
+
}
|