Skip to content

Commit b74706f

Browse files
committed
feat: add support for PUT requests
1 parent 64c3f5c commit b74706f

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
2929
post: >-
3030
INSERT INTO categories(name, slug, created_at, created_by, updated_at, updated_by)
3131
VALUES (:name, :slug, NOW(), :userId, NOW(), :userId)
32+
33+
- path: /v1/categories/:categoryId
34+
put: >-
35+
UPDATE categories
36+
SET name = :name, name_ru = :nameRu, slug = :slug, updated_at = NOW(), updated_by = :userId
37+
WHERE id = :categoryId
3238
```
3339

3440
1. Generate code
@@ -68,4 +74,9 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
6874
ETag: W/"a-bAsFyilMr4Ra1hIU5PyoyFRunpI"
6975
Date: Wed, 15 Jul 2020 18:06:33 GMT
7076
Connection: keep-alive
77+
$ curl -i -H 'Content-Type: application/json' -d '{"name":"Fauna","nameRu":"Фауна","slug":"fauna","userId":101,"categoryId":1}' -X PUT http://localhost:3000/v1/categories/1
78+
HTTP/1.1 204 No Content
79+
ETag: W/"a-bAsFyilMr4Ra1hIU5PyoyFRunpI"
80+
Date: Wed, 15 Jul 2020 18:30:36 GMT
81+
Connection: keep-alive
7182
```

examples/js/app.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ app.post('/v1/categories', (req, res) => {
6767
)
6868
})
6969

70+
app.put('/v1/categories/:categoryId', (req, res) => {
71+
pool.query(
72+
'UPDATE categories SET name = :name , name_ru = :nameRu , slug = :slug , updated_at = NOW() , updated_by = :userId WHERE id = :categoryId',
73+
{ "name": req.body.name, "nameRu": req.body.nameRu, "slug": req.body.slug, "userId": req.body.userId, "categoryId": req.body.categoryId },
74+
(err, rows, fields) => {
75+
if (err) {
76+
throw err
77+
}
78+
res.sendStatus(204)
79+
}
80+
)
81+
})
82+
7083
const port = process.env.PORT || 3000;
7184
app.listen(port, () => {
7285
console.log(`Listen on ${port}`)

examples/js/endpoints.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@
3030
, NOW()
3131
, :userId
3232
)
33+
34+
- path: /v1/categories/:categoryId
35+
put: >-
36+
UPDATE categories
37+
SET name = :name
38+
, name_ru = :nameRu
39+
, slug = :slug
40+
, updated_at = NOW()
41+
, updated_by = :userId
42+
WHERE id = :categoryId

src/cli.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ const createEndpoints = async (destDir, fileName, config) => {
3535
if (endpoint.hasOwnProperty('post')) {
3636
console.log('POST', endpoint.path, '=>', flattenQuery(endpoint.post));
3737
}
38+
if (endpoint.hasOwnProperty('put')) {
39+
console.log('PUT', endpoint.path, '=>', flattenQuery(endpoint.put));
40+
}
3841
}
3942

4043
const resultedCode = await ejs.renderFile(

src/templates/app.js.ejs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ app.post('<%- endpoint.path %>', (req, res) => {
5959
}
6060
)
6161
})
62+
<%
63+
}
64+
if (endpoint.hasOwnProperty('put')) {
65+
const params = extractParams(endpoint.put);
66+
%>
67+
app.put('<%- endpoint.path %>', (req, res) => {
68+
pool.query(
69+
<%- formatQuery(endpoint.put) %>,<%- params.length > 0 ? '\n ' + formatParams(params, 'req.body') + ',' : '' %>
70+
(err, rows, fields) => {
71+
if (err) {
72+
throw err
73+
}
74+
res.sendStatus(204)
75+
}
76+
)
77+
})
6278
<%
6379
}
6480
});

0 commit comments

Comments
 (0)