create-backlist 5.0.6 → 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.6",
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,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;