create-backlist 5.0.5 → 5.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-backlist",
3
- "version": "5.0.5",
3
+ "version": "5.0.7",
4
4
  "description": "An advanced, multi-language backend generator based on frontend analysis.",
5
5
  "type": "commonjs",
6
6
  "bin": {
@@ -1,11 +1,17 @@
1
- const ejs = require('ejs');
2
1
  const fs = require('fs-extra');
2
+ const ejs = require('ejs');
3
3
  const path = require('path');
4
4
 
5
- async function renderAndWrite(templatePath, destinationPath, data) {
6
- const template = await fs.readFile(templatePath, 'utf-8');
7
- const content = ejs.render(template, data);
8
- await fs.outputFile(destinationPath, content);
5
+ async function renderAndWrite(templatePath, outPath, data) {
6
+ try {
7
+ const tpl = await fs.readFile(templatePath, 'utf-8');
8
+ const code = ejs.render(tpl, data || {}, { filename: templatePath }); // filename helps with EJS errors
9
+ await fs.outputFile(outPath, code);
10
+ } catch (err) {
11
+ console.error('EJS render failed for:', templatePath);
12
+ console.error('Data keys:', Object.keys(data || {}));
13
+ throw err;
14
+ }
9
15
  }
10
16
 
11
17
  function getTemplatePath(subpath) {
@@ -1,125 +1,50 @@
1
- // Auto-generated by create-backlist v5.0
2
- using Microsoft.AspNetCore.Mvc;
3
- using Microsoft.EntityFrameworkCore;
4
- using <%= projectName %>.Data;
5
- using <%= projectName %>.Models;
6
-
7
- namespace <%= projectName %>.Controllers
8
- {
9
- [ApiController]
10
- [Route("api/[controller]")]
11
- public class <%= controllerName %>Controller : ControllerBase
12
- {
13
- private readonly ApplicationDbContext _context;
14
-
15
- public <%= controllerName %>Controller(ApplicationDbContext context)
16
- {
17
- _context = context;
18
- }
19
-
20
- // GET: api/<%= controllerName.toLowerCase() %>
21
- // Retrieves all items.
22
- [HttpGet]
23
- public async Task<ActionResult<IEnumerable<<%= controllerName %>>>> Get<%= controllerName %>s()
24
- {
25
- if (_context.<%= controllerName %>s == null)
26
- {
27
- return NotFound("Entity set '<%= controllerName %>s' is null.");
28
- }
29
- return await _context.<%= controllerName %>s.ToListAsync();
30
- }
31
-
32
- // GET: api/<%= controllerName.toLowerCase() %>/{id}
33
- // Retrieves a specific item by its ID.
34
- [HttpGet("{id}")]
35
- public async Task<ActionResult<<%= controllerName %>>> Get<%= controllerName %>(Guid id)
36
- {
37
- if (_context.<%= controllerName %>s == null)
38
- {
39
- return NotFound();
40
- }
41
- var item = await _context.<%= controllerName %>s.FindAsync(id);
42
-
43
- if (item == null)
44
- {
45
- return NotFound();
46
- }
47
-
48
- return item;
49
- }
50
-
51
- // PUT: api/<%= controllerName.toLowerCase() %>/{id}
52
- // Updates a specific item.
53
- [HttpPut("{id}")]
54
- public async Task<IActionResult> Put<%= controllerName %>(Guid id, <%= controllerName %> item)
55
- {
56
- if (id != item.Id)
57
- {
58
- return BadRequest();
59
- }
60
-
61
- _context.Entry(item).State = EntityState.Modified;
62
-
63
- try
64
- {
65
- await _context.SaveChangesAsync();
66
- }
67
- catch (DbUpdateConcurrencyException)
68
- {
69
- if (!ItemExists(id))
70
- {
71
- return NotFound();
72
- }
73
- else
74
- {
75
- throw;
76
- }
77
- }
78
-
79
- return NoContent();
80
- }
81
-
82
- // POST: api/<%= controllerName.toLowerCase() %>
83
- // Creates a new item.
84
- [HttpPost]
85
- public async Task<ActionResult<<%= controllerName %>>> Post<%= controllerName %>(<%= controllerName %> item)
86
- {
87
- if (_context.<%= controllerName %>s == null)
88
- {
89
- return Problem("Entity set '<%= controllerName %>s' is null.");
90
- }
91
- // Ensure a new Guid is created for the new item
92
- item.Id = Guid.NewGuid();
93
- _context.<%= controllerName %>s.Add(item);
94
- await _context.SaveChangesAsync();
95
-
96
- return CreatedAtAction(nameof(Get<%= controllerName %>), new { id = item.Id }, item);
97
- }
98
-
99
- // DELETE: api/<%= controllerName.toLowerCase() %>/{id}
100
- // Deletes a specific item.
101
- [HttpDelete("{id}")]
102
- public async Task<IActionResult> Delete<%= controllerName %>(Guid id)
103
- {
104
- if (_context.<%= controllerName %>s == null)
105
- {
106
- return NotFound();
107
- }
108
- var item = await _context.<%= controllerName %>s.FindAsync(id);
109
- if (item == null)
110
- {
111
- return NotFound();
112
- }
113
-
114
- _context.<%= controllerName %>s.Remove(item);
115
- await _context.SaveChangesAsync();
116
-
117
- return NoContent();
118
- }
119
-
120
- private bool ItemExists(Guid id)
121
- {
122
- return (_context.<%= controllerName %>s?.Any(e => e.Id == id)).GetValueOrDefault();
123
- }
124
- }
125
- }
1
+ // Auto-generated by create-backlist on <%= new Date().toISOString() %>
2
+ import { Request, Response } from 'express';
3
+
4
+ export const create<%= modelName %> = async (req: Request, res: Response) => {
5
+ try {
6
+ // TODO: implement create logic
7
+ res.status(201).json({ message: '<%= modelName %> created', data: req.body });
8
+ } catch (error) {
9
+ res.status(500).json({ message: 'Error creating <%= modelName %>', error });
10
+ }
11
+ };
12
+
13
+ export const getAll<%= modelName %>s = async (_req: Request, res: Response) => {
14
+ try {
15
+ // TODO: implement list logic
16
+ res.status(200).json([]);
17
+ } catch (error) {
18
+ res.status(500).json({ message: 'Error fetching <%= modelName %>s', error });
19
+ }
20
+ };
21
+
22
+ export const get<%= modelName %>ById = async (req: Request, res: Response) => {
23
+ try {
24
+ const { id } = req.params;
25
+ // TODO: implement get by id logic
26
+ res.status(200).json({ id });
27
+ } catch (error) {
28
+ res.status(500).json({ message: 'Error fetching <%= modelName %>', error });
29
+ }
30
+ };
31
+
32
+ export const update<%= modelName %>ById = async (req: Request, res: Response) => {
33
+ try {
34
+ const { id } = req.params;
35
+ // TODO: implement update logic
36
+ res.status(200).json({ id, ...req.body });
37
+ } catch (error) {
38
+ res.status(500).json({ message: 'Error updating <%= modelName %>', error });
39
+ }
40
+ };
41
+
42
+ export const delete<%= modelName %>ById = async (req: Request, res: Response) => {
43
+ try {
44
+ const { id } = req.params;
45
+ // TODO: implement delete logic
46
+ res.status(204).send();
47
+ } catch (error) {
48
+ res.status(500).json({ message: 'Error deleting <%= modelName %>', error });
49
+ }
50
+ };
@@ -1,64 +1,58 @@
1
- // Auto-generated by create-backlist v3.0 on <%= new Date().toISOString() %>
1
+ // Auto-generated by create-backlist on <%= new Date().toISOString() %>
2
2
  import { Router, Request, Response } from 'express';
3
- <%# Create a unique set of controller names from the endpoints array %>
4
- <% const controllersToImport = new Set(endpoints.map(ep => ep.controllerName).filter(name => name !== 'Default')); %>
5
-
6
- // Import all the generated controllers
7
- <% for (const controller of controllersToImport) { %>
8
- import * as <%= controller %>Controller from '../controllers/<%= controller %>.controller';
9
- <% } %>
3
+ <%
4
+ // Build unique controller list safely
5
+ const controllers = [];
6
+ if (Array.isArray(endpoints)) {
7
+ endpoints.forEach((ep) => {
8
+ if (ep && ep.controllerName && ep.controllerName !== 'Default' && !controllers.includes(ep.controllerName)) {
9
+ controllers.push(ep.controllerName);
10
+ }
11
+ });
12
+ }
13
+ %>
14
+ <% controllers.forEach((ctrl) => { %>
15
+ import * as <%= ctrl %>Controller from './controllers/<%= ctrl %>.controller';
16
+ <% }) %>
10
17
 
11
- <%# Import the protect middleware only if authentication is enabled %>
12
18
  <% if (addAuth) { %>
13
- import { protect } from '../middleware/Auth.middleware';
19
+ import { protect } from './middleware/Auth.middleware';
14
20
  <% } %>
15
21
 
16
22
  const router = Router();
17
23
 
18
- <%# Loop through each endpoint found by the analyzer %>
19
- <% endpoints.forEach(endpoint => { %>
20
- <%
21
- // Convert URL path for Express router (e.g., /api/users/{id} -> /users/:id)
22
- const expressPath = endpoint.path.replace('/api', '').replace(/{(\w+)}/g, ':$1');
23
- const controllerName = endpoint.controllerName;
24
- let handlerFunction;
24
+ // If no endpoints detected, emit a basic route so file is valid
25
+ <% if (!Array.isArray(endpoints) || endpoints.length === 0) { %>
26
+ router.get('/health', (_req: Request, res: Response) => {
27
+ res.status(200).json({ ok: true, message: 'Auto-generated routes alive' });
28
+ });
29
+ <% } %>
25
30
 
26
- // --- LOGIC TO MAP ENDPOINT TO A CRUD CONTROLLER FUNCTION ---
27
- if (controllerName !== 'Default') {
28
- if (endpoint.method === 'POST' && !expressPath.includes(':')) {
29
- handlerFunction = `${controllerName}Controller.create${controllerName}`;
30
- } else if (endpoint.method === 'GET' && !expressPath.includes(':')) {
31
- handlerFunction = `${controllerName}Controller.getAll${controllerName}s`;
32
- } else if (endpoint.method === 'GET' && expressPath.includes(':')) {
33
- handlerFunction = `${controllerName}Controller.get${controllerName}ById`;
34
- } else if (endpoint.method === 'PUT' && expressPath.includes(':')) {
35
- handlerFunction = `${controllerName}Controller.update${controllerName}ById`;
36
- } else if (endpoint.method === 'DELETE' && expressPath.includes(':')) {
37
- handlerFunction = `${controllerName}Controller.delete${controllerName}ById`;
31
+ <%
32
+ if (Array.isArray(endpoints)) {
33
+ endpoints.forEach((ep) => {
34
+ const rawPath = (ep && ep.path) ? ep.path : '/';
35
+ const expressPath = (rawPath.replace(/^\/api/, '') || '/').replace(/{(\w+)}/g, ':$1');
36
+ const method = ((ep && ep.method) ? ep.method : 'GET').toLowerCase();
37
+ const ctrl = (ep && ep.controllerName) ? ep.controllerName : 'Default';
38
+ const hasId = expressPath.includes(':');
39
+ let handler = '';
40
+
41
+ if (ctrl !== 'Default') {
42
+ if (method === 'post' && !hasId) handler = `${ctrl}Controller.create${ctrl}`;
43
+ else if (method === 'get' && !hasId) handler = `${ctrl}Controller.getAll${ctrl}s`;
44
+ else if (method === 'get' && hasId) handler = `${ctrl}Controller.get${ctrl}ById`;
45
+ else if (method === 'put' && hasId) handler = `${ctrl}Controller.update${ctrl}ById`;
46
+ else if (method === 'delete' && hasId) handler = `${ctrl}Controller.delete${ctrl}ById`;
38
47
  }
39
- }
40
-
41
- // If no specific CRUD function matches, create a placeholder handler.
42
- if (!handlerFunction) {
43
- handlerFunction = `(req: Request, res: Response) => {
44
- res.status(501).json({ message: 'Handler not implemented for <%= endpoint.method %> <%= expressPath %>' });
45
- }`;
46
- }
47
48
 
48
- // --- V3.0 AUTH LOGIC: Decide if the route should be protected ---
49
- // We protect all routes that modify data (POST, PUT, DELETE) if auth is enabled.
50
- // We leave GET routes public by default. This is a common pattern.
51
- const middleware = (addAuth && (endpoint.method === 'POST' || endpoint.method === 'PUT' || endpoint.method === 'DELETE'))
52
- ? 'protect, '
53
- : '';
49
+ const needsProtect = !!addAuth && (method === 'post' || method === 'put' || method === 'delete');
50
+ const middleware = needsProtect ? 'protect, ' : '';
51
+ %>
52
+ router.<%= method %>('<%- expressPath || "/" %>', <%- middleware %><%- handler || '(req: Request, res: Response) => res.status(501).json({ message: "Not Implemented" })' %>);
53
+ <%
54
+ });
55
+ }
54
56
  %>
55
- /**
56
- * Route for <%= endpoint.method.toUpperCase() %> <%= endpoint.path %>
57
- * Mapped to: <%- handlerFunction.includes('=>') ? 'Inline Handler' : handlerFunction %>
58
- * Protected: <%= middleware ? 'Yes' : 'No' %>
59
- */
60
- router.<%= endpoint.method.toLowerCase() %>('<%- expressPath %>', <%- middleware %><%- handlerFunction %>);
61
-
62
- <% }); %>
63
57
 
64
58
  export default router;