Skip to content
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ coverage/

# IDE files
.idea/

# Console
.node_repl_history
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 32 additions & 16 deletions src/interfaces/console/Console.js
Original file line number Diff line number Diff line change
@@ -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')
}
228 changes: 115 additions & 113 deletions src/interfaces/http/user/UsersController.js
Original file line number Diff line number Diff line change
@@ -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;
20 changes: 17 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down