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,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,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
{
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
|
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;
|