Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom renderer option added to cli #1447

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ __Please help me build OSS__ 👉 [GitHub Sponsors](https://github.com/sponsors/
* [HTTPS](#https)
* [Add custom routes](#add-custom-routes)
* [Add middlewares](#add-middlewares)
* [Add custom renderer](#add-custom-renderer)
* [CLI usage](#cli-usage)
* [Module](#module)
+ [Simple example](#simple-example)
Expand Down Expand Up @@ -392,6 +393,27 @@ json-server db.json --middlewares ./hello.js
json-server db.json --middlewares ./first.js ./second.js
```

### Add custom renderer

You can add your custom renderer from the CLI using `--custom-renderer` option:

```js
// renderer.js
module.exports = (req, res) => {
res.jsonp({
version: "v1",
statusCode: 200,
isError: false,
message: "The Request successful.",
result: res.locals.data,
})
}
```

```bash
json-server db.json --custom-renderer ./renderer.js
```

### CLI usage

```
Expand All @@ -413,6 +435,7 @@ Options:
--id, -i Set database id property (e.g. _id) [default: "id"]
--foreignKeySuffix, --fks Set foreign key suffix, (e.g. _id as in post_id)
[default: "Id"]
--custom-renderer, --cr Path to custom renderer file
--quiet, -q Suppress log messages from output [boolean]
--help, -h Show help [boolean]
--version, -v Show version number [boolean]
Expand Down
4 changes: 4 additions & 0 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ module.exports = function () {
alias: 'r',
description: 'Path to routes file',
},
'custom-renderer': {
alias: 'cr',
description: 'Path to custom renderer file',
},
middlewares: {
alias: 'm',
array: true,
Expand Down
15 changes: 13 additions & 2 deletions src/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function prettyPrint(argv, object, rules) {
console.log()
}

function createApp(db, routes, middlewares, argv) {
function createApp(db, routes, middlewares, argv, customRenderer) {
const app = jsonServer.create()

const { foreignKeySuffix } = argv
Expand Down Expand Up @@ -74,6 +74,11 @@ function createApp(db, routes, middlewares, argv) {
}

router.db._.id = argv.id

if (argv.customRenderer) {
router.render = customRenderer;
}

app.db = router.db
app.use(router)

Expand Down Expand Up @@ -123,11 +128,17 @@ module.exports = function (argv) {
})
}

let customRenderer
if (argv.customRenderer) {
console.log(chalk.gray(' Loading', argv.customRenderer));
customRenderer = require(path.resolve(argv.customRenderer))
}

// Done
console.log(chalk.gray(' Done'))

// Create app and server
app = createApp(db, routes, middlewares, argv)
app = createApp(db, routes, middlewares, argv, customRenderer)
server = app.listen(argv.port, argv.host)

// Enhance with a destroy function
Expand Down