Skip to content

Commit 65b5f8f

Browse files
committed
chore: generate a controller method for POST requests
1 parent 186e9f7 commit 65b5f8f

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

examples/js/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ app.get('/v1/collections/:collectionId/categories/count', (req, res) => {
5252
)
5353
})
5454

55+
app.post('/v1/categories', (req, res) => {
56+
res.sendStatus(200)
57+
})
5558

5659
const port = process.env.PORT || 3000;
5760
app.listen(port, () => {

examples/js/endpoints.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,25 @@
88
JOIN series s
99
ON s.id = cs.series_id
1010
WHERE cs.collection_id = :collectionId
11+
12+
- path: /v1/categories
13+
post: >-
14+
INSERT
15+
INTO categories2
16+
( name
17+
, name_ru
18+
, slug
19+
, created_at
20+
, created_by
21+
, updated_at
22+
, updated_by
23+
)
24+
VALUES
25+
( :name
26+
, :name_ru
27+
, :slug
28+
, NOW()
29+
, :userId
30+
, NOW()
31+
, :userId
32+
)

src/cli.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ const createEndpoints = async (destDir, fileName, config) => {
2626
const resultFile = path.join(destDir, fileName);
2727

2828
for (let endpoint of config) {
29-
console.log('GET', endpoint.path, '=>', endpoint.get);
29+
if (endpoint.hasOwnProperty('get')) {
30+
console.log('GET', endpoint.path, '=>', endpoint.get);
31+
}
32+
if (endpoint.hasOwnProperty('post')) {
33+
console.log('POST', endpoint.path, '=>', endpoint.post);
34+
}
3035
}
3136

3237
const resultedCode = await ejs.renderFile(

src/templates/app.js.ejs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ const pool = mysql.createPool({
2525
})
2626

2727
<%
28-
endpoints.forEach(function(endpoint) {
29-
const params = extractParams(endpoint.get);
28+
endpoints.forEach(function(endpoint) {
29+
if (endpoint.hasOwnProperty('get')) {
30+
const params = extractParams(endpoint.get);
3031
%>
3132
app.get('<%- endpoint.path %>', (req, res) => {
3233
pool.query(
@@ -40,7 +41,17 @@ app.get('<%- endpoint.path %>', (req, res) => {
4041
}
4142
)
4243
})
43-
<% }); %>
44+
<%
45+
}
46+
if (endpoint.hasOwnProperty('post')) {
47+
%>
48+
app.post('<%- endpoint.path %>', (req, res) => {
49+
res.sendStatus(200)
50+
})
51+
<%
52+
}
53+
});
54+
-%>
4455

4556
const port = process.env.PORT || 3000;
4657
app.listen(port, () => {

0 commit comments

Comments
 (0)