Skip to content

Commit

Permalink
feat(barabara): added redirect and html abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
satblip committed Aug 8, 2019
1 parent 6ff5a7e commit f0787b1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 50 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![npm version](https://img.shields.io/npm/v/barabara.svg)](https://www.npmjs.com/package/barabara)
[![Dependency Status](https://img.shields.io/david/webinmove/barabara.svg?style=flat-square)](https://david-dm.org/webinmove/barabara)

Automatic router for simple REST json api
Automatic express-router from generic controllers

Barabara will automatically create one or more Express router for you reading the content of your controller folder(s).

Expand Down Expand Up @@ -91,6 +91,13 @@ module.exports = {
internal: () => { /* ... */ }
};
```

#### Return behaviors

- If you return an object without `redirect` key in it, it will respond JSON
- If you return an object with `redirect` key in it, it will redirect to the value
- If you return a non-object, it will respond this non-object (html for example)

*Note: not all the methods need to be implemented*

`req.query`, `req.body`, `req.params` will be merged in `params` (in this order).
Expand Down
44 changes: 0 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "barabara",
"version": "1.0.0",
"description": "Automatic express-router generator",
"main": "lib/Barabara.js",
"description": "Automatic express-router from generic controllers",
"main": "src/Barabara.js",
"directories": {
"lib": "lib",
"src": "src",
"test": "tests"
},
"scripts": {
Expand Down Expand Up @@ -39,7 +39,6 @@
],
"license": "MIT",
"dependencies": {
"ci": "^1.0.0",
"lodash": "^4.17.11",
"to-slug-case": "^1.0.0"
},
Expand Down
9 changes: 8 additions & 1 deletion src/Barabara.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ class Barabara {

try {
const result = await controller[action](options, meta);
res.json(result);
if (typeof result === 'object') {
if (result.redirect) {
return res.redirect(result.redirect);
}
return res.json(result);
} else {
return res.send(result);
}
} catch (e) {
return next(e);
}
Expand Down

0 comments on commit f0787b1

Please sign in to comment.