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,64 +1,58 @@
|
|
|
1
|
-
// Auto-generated by create-backlist
|
|
1
|
+
// Auto-generated by create-backlist on <%= new Date().toISOString() %>
|
|
2
2
|
import { Router, Request, Response } from 'express';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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 '
|
|
19
|
+
import { protect } from './middleware/Auth.middleware';
|
|
14
20
|
<% } %>
|
|
15
21
|
|
|
16
22
|
const router = Router();
|
|
17
23
|
|
|
18
|
-
|
|
19
|
-
<%
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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;
|