From 74cc33d286e87a53126e05923ff2628b569eb71c Mon Sep 17 00:00:00 2001 From: Liran Brimer Date: Tue, 25 Sep 2018 10:08:11 +0300 Subject: [PATCH 1/3] upgrade awilix-express --- package.json | 2 +- src/interfaces/http/user/UsersController.js | 228 ++++++++++---------- yarn.lock | 20 +- 3 files changed, 133 insertions(+), 117 deletions(-) diff --git a/package.json b/package.json index dac12c7..c71e77c 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "license": "MIT", "dependencies": { "awilix": "^3.0.9", - "awilix-express": "^0.11.0", + "awilix-express": "^1.1.0", "body-parser": "^1.17.1", "compression": "^1.6.2", "cors": "^2.8.1", diff --git a/src/interfaces/http/user/UsersController.js b/src/interfaces/http/user/UsersController.js index ee4f935..6be6c39 100644 --- a/src/interfaces/http/user/UsersController.js +++ b/src/interfaces/http/user/UsersController.js @@ -1,125 +1,127 @@ -const { Router } = require('express'); -const { inject } = require('awilix-express'); +const {Router} = require('express'); +const {inject} = require('awilix-express'); const Status = require('http-status'); const UsersController = { - get router() { + get router () { const router = Router(); - router.use(inject('userSerializer')); - - router.get('/', inject('getAllUsers'), this.index); - router.get('/:id', inject('getUser'), this.show); - router.post('/', inject('createUser'), this.create); - router.put('/:id', inject('updateUser'), this.update); - router.delete('/:id', inject('deleteUser'), this.delete); + router.get('/', this.index); + router.get('/:id', this.show); + router.post('/', this.create); + router.put('/:id', this.update); + router.delete('/:id', this.delete); return router; }, - index(req, res, next) { - const { getAllUsers, userSerializer } = req; - const { SUCCESS, ERROR } = getAllUsers.outputs; - - getAllUsers - .on(SUCCESS, (users) => { - res - .status(Status.OK) - .json(users.map(userSerializer.serialize)); - }) - .on(ERROR, next); - - getAllUsers.execute(); - }, - - show(req, res, next) { - const { getUser, userSerializer } = req; - - const { SUCCESS, ERROR, NOT_FOUND } = getUser.outputs; - - getUser - .on(SUCCESS, (user) => { - res - .status(Status.OK) - .json(userSerializer.serialize(user)); - }) - .on(NOT_FOUND, (error) => { - res.status(Status.NOT_FOUND).json({ - type: 'NotFoundError', - details: error.details - }); - }) - .on(ERROR, next); - - getUser.execute(Number(req.params.id)); - }, - - create(req, res, next) { - const { createUser, userSerializer } = req; - const { SUCCESS, ERROR, VALIDATION_ERROR } = createUser.outputs; - - createUser - .on(SUCCESS, (user) => { - res - .status(Status.CREATED) - .json(userSerializer.serialize(user)); - }) - .on(VALIDATION_ERROR, (error) => { - res.status(Status.BAD_REQUEST).json({ - type: 'ValidationError', - details: error.details - }); - }) - .on(ERROR, next); - - createUser.execute(req.body); - }, - - update(req, res, next) { - const { updateUser, userSerializer } = req; - const { SUCCESS, ERROR, VALIDATION_ERROR, NOT_FOUND } = updateUser.outputs; - - updateUser - .on(SUCCESS, (user) => { - res - .status(Status.ACCEPTED) - .json(userSerializer.serialize(user)); - }) - .on(VALIDATION_ERROR, (error) => { - res.status(Status.BAD_REQUEST).json({ - type: 'ValidationError', - details: error.details - }); - }) - .on(NOT_FOUND, (error) => { - res.status(Status.NOT_FOUND).json({ - type: 'NotFoundError', - details: error.details - }); - }) - .on(ERROR, next); - - updateUser.execute(Number(req.params.id), req.body); - }, - - delete(req, res, next) { - const { deleteUser } = req; - const { SUCCESS, ERROR, NOT_FOUND } = deleteUser.outputs; - - deleteUser - .on(SUCCESS, () => { - res.status(Status.ACCEPTED).end(); - }) - .on(NOT_FOUND, (error) => { - res.status(Status.NOT_FOUND).json({ - type: 'NotFoundError', - details: error.details - }); - }) - .on(ERROR, next); - - deleteUser.execute(Number(req.params.id)); - } + index: inject(({getAllUsers, userSerializer}) => + (req, res, next) => { + const {SUCCESS, ERROR} = getAllUsers.outputs; + + getAllUsers + .on(SUCCESS, (users) => { + res + .status(Status.OK) + .json(users.map(userSerializer.serialize)); + }) + .on(ERROR, next); + + getAllUsers.execute(); + } + ), + + show: inject(({getUser, userSerializer}) => + (req, res, next) => { + const {SUCCESS, ERROR, NOT_FOUND} = getUser.outputs; + + getUser + .on(SUCCESS, (user) => { + res + .status(Status.OK) + .json(userSerializer.serialize(user)); + }) + .on(NOT_FOUND, (error) => { + res.status(Status.NOT_FOUND).json({ + type: 'NotFoundError', + details: error.details + }); + }) + .on(ERROR, next); + + getUser.execute(Number(req.params.id)); + } + ), + + create: inject(({createUser, userSerializer}) => + (req, res, next) => { + const {SUCCESS, ERROR, VALIDATION_ERROR} = createUser.outputs; + + createUser + .on(SUCCESS, (user) => { + res + .status(Status.CREATED) + .json(userSerializer.serialize(user)); + }) + .on(VALIDATION_ERROR, (error) => { + res.status(Status.BAD_REQUEST).json({ + type: 'ValidationError', + details: error.details + }); + }) + .on(ERROR, next); + + createUser.execute(req.body); + } + ), + + update: inject(({updateUser, userSerializer}) => + (req, res, next) => { + const {SUCCESS, ERROR, VALIDATION_ERROR, NOT_FOUND} = updateUser.outputs; + + updateUser + .on(SUCCESS, (user) => { + res + .status(Status.ACCEPTED) + .json(userSerializer.serialize(user)); + }) + .on(VALIDATION_ERROR, (error) => { + res.status(Status.BAD_REQUEST).json({ + type: 'ValidationError', + details: error.details + }); + }) + .on(NOT_FOUND, (error) => { + res.status(Status.NOT_FOUND).json({ + type: 'NotFoundError', + details: error.details + }); + }) + .on(ERROR, next); + + updateUser.execute(Number(req.params.id), req.body); + } + ), + + delete: inject(({deleteUser}) => + (req, res, next) => { + const {SUCCESS, ERROR, NOT_FOUND} = deleteUser.outputs; + + deleteUser + .on(SUCCESS, () => { + res.status(Status.ACCEPTED).end(); + }) + .on(NOT_FOUND, (error) => { + res.status(Status.NOT_FOUND).json({ + type: 'NotFoundError', + details: error.details + }); + }) + .on(ERROR, next); + + deleteUser.execute(Number(req.params.id)); + } + ) }; module.exports = UsersController; diff --git a/yarn.lock b/yarn.lock index 8631ec8..0a682d4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -208,9 +208,19 @@ asynckit@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" -awilix-express@^0.11.0: - version "0.11.0" - resolved "https://registry.yarnpkg.com/awilix-express/-/awilix-express-0.11.0.tgz#6c41d30219dea53b83982045c5235b4804791425" +awilix-express@^1.1.0: + version "1.1.0" + resolved "https://nexus.exelator.net/repository/npm-all/awilix-express/-/awilix-express-1.1.0.tgz#70fa02d6211f15e5ab9c7af542c5e1e58c26a1b7" + dependencies: + awilix-router-core "^1.3.2" + tslib "^1.8.0" + +awilix-router-core@^1.3.2: + version "1.3.2" + resolved "https://nexus.exelator.net/repository/npm-all/awilix-router-core/-/awilix-router-core-1.3.2.tgz#055a1b6ff62d22e59c5362221c3d8105946eec2f" + dependencies: + glob "^7.1.2" + tslib "^1.8.1" awilix@^3.0.9: version "3.0.9" @@ -3915,6 +3925,10 @@ tryit@^1.0.1: version "1.0.3" resolved "https://registry.yarnpkg.com/tryit/-/tryit-1.0.3.tgz#393be730a9446fd1ead6da59a014308f36c289cb" +tslib@^1.8.0, tslib@^1.8.1: + version "1.9.3" + resolved "https://nexus.exelator.net/repository/npm-all/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286" + tunnel-agent@~0.4.1: version "0.4.3" resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.4.3.tgz#6373db76909fe570e08d73583365ed828a74eeeb" From a23bb4af13bcd7b98fb4e74d59296df5382a9ec8 Mon Sep 17 00:00:00 2001 From: Liran Date: Sun, 7 Oct 2018 11:04:17 +0300 Subject: [PATCH 2/3] save history of console's repl --- src/interfaces/console/Console.js | 48 ++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/interfaces/console/Console.js b/src/interfaces/console/Console.js index 8f191ce..1065dea 100644 --- a/src/interfaces/console/Console.js +++ b/src/interfaces/console/Console.js @@ -1,33 +1,49 @@ -const REPL = require('repl'); -const vm = require('vm'); +const REPL = require('repl') +const vm = require('vm') +const fs = require('fs') + +const HISTORY_FILE = '.node_repl_history' module.exports = { - start(options = {}) { - const { expose } = options; + start (options = {}) { + const { expose } = options const repl = REPL.start({ eval: promisableEval - }); - Object.assign(repl.context, expose); + }) + + Object.assign(repl.context, expose) + + try { + // load command history from a file + fs.statSync(HISTORY_FILE) + fs.readFileSync(HISTORY_FILE, { encoding: 'utf8' }) + .split('\n') + .reverse() + .filter(line => line.trim()) + .map(line => repl.history.push(line)) + } catch (err) { } } -}; +} +function promisableEval (cmd, context, filename, callback) { + const result = vm.runInContext(cmd, context) -function promisableEval(cmd, context, filename, callback) { - const result = vm.runInContext(cmd, context); + // write command to history file + fs.appendFileSync(HISTORY_FILE, cmd) - if(isPromise(result)) { + if (isPromise(result)) { return result .then((v) => callback(null, v)) - .catch((e) => callback(e)); + .catch((e) => callback(e)) } - return callback(null, result); + return callback(null, result) } -function isPromise(value) { - return value - && (typeof value.then === 'function') - && (typeof value.catch === 'function'); +function isPromise (value) { + return value && + (typeof value.then === 'function') && + (typeof value.catch === 'function') } From 020cf4fda44a65d50358f6e68138bf02803ed867 Mon Sep 17 00:00:00 2001 From: Liran Date: Sun, 7 Oct 2018 11:04:59 +0300 Subject: [PATCH 3/3] dont push console history to git --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index ea4e6a1..8c83290 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ coverage/ # IDE files .idea/ + +# Console +.node_repl_history