From 29c0b1bf0f06f2545c66f02273b6f5e99f02d87a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=BCsl=C3=BCm=20=C3=96ZT=C3=9CRK?= Date: Fri, 18 Aug 2023 08:49:28 +0300 Subject: [PATCH 1/4] custom renderer option is added. --- README.md | 21 +++++++++++++++++++++ src/cli/index.js | 4 ++++ src/cli/run.js | 15 +++++++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 808d2e11a..bc6a522c0 100644 --- a/README.md +++ b/README.md @@ -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) @@ -392,6 +393,25 @@ 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({ + isError: false, + message: "The Request successful.", + result: res.locals.data, + }) +} +``` + +```bash +json-server db.json --custom-renderer ./renderer.js +``` + ### CLI usage ``` @@ -413,6 +433,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] diff --git a/src/cli/index.js b/src/cli/index.js index d817f9d7b..27a7c073f 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -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, diff --git a/src/cli/run.js b/src/cli/run.js index c4238f473..e6b22a0be 100644 --- a/src/cli/run.js +++ b/src/cli/run.js @@ -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 @@ -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) @@ -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 From 1ac06b0445fd3474cd57bc381d504cd30ccc6652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=BCsl=C3=BCm=20=C3=96ZT=C3=9CRK?= Date: Fri, 18 Aug 2023 09:57:00 +0300 Subject: [PATCH 2/4] added version adn statuscode to renderer.js sample code in readme file. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index bc6a522c0..5a49bb2a2 100644 --- a/README.md +++ b/README.md @@ -401,6 +401,8 @@ You can add your custom renderer from the CLI using `--custom-renderer` option: // renderer.js module.exports = (req, res) => { res.jsonp({ + version: "v1", + statusCode: 200, isError: false, message: "The Request successful.", result: res.locals.data, From 9427ad9fd261d87bc0eca51b2ac627900a2cfde8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=BCsl=C3=BCm=20=C3=96ZT=C3=9CRK?= Date: Fri, 18 Aug 2023 11:21:49 +0300 Subject: [PATCH 3/4] changed version number --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 24d8ab672..a8f415f9d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json-server", - "version": "0.17.3", + "version": "61.17.3", "description": "Get a full fake REST API with zero coding in less than 30 seconds", "main": "./lib/server/index.js", "bin": "./lib/cli/bin.js", @@ -69,7 +69,7 @@ }, "repository": { "type": "git", - "url": "git://github.com/typicode/json-server.git" + "url": "git://github.com/muslumozturk61/json-server.git" }, "keywords": [ "JSON", From a8ca4a8c0dc00642018f5646863ebdd06eec7ee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=BCsl=C3=BCm=20=C3=96ZT=C3=9CRK?= <50518380+muslumozturk61@users.noreply.github.com> Date: Fri, 18 Aug 2023 22:59:05 +0300 Subject: [PATCH 4/4] Update package.json --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a8f415f9d..24d8ab672 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "json-server", - "version": "61.17.3", + "version": "0.17.3", "description": "Get a full fake REST API with zero coding in less than 30 seconds", "main": "./lib/server/index.js", "bin": "./lib/cli/bin.js", @@ -69,7 +69,7 @@ }, "repository": { "type": "git", - "url": "git://github.com/muslumozturk61/json-server.git" + "url": "git://github.com/typicode/json-server.git" }, "keywords": [ "JSON",