Skip to content

Commit

Permalink
feat(apis, helpers, config): switch to service id, init request to lou ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 9, 2020
1 parent e5413d6 commit 6b3fad1
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 18 deletions.
8 changes: 8 additions & 0 deletions config/defaults/montaineDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ module.exports = _.merge(defaultConfig, {
cors: {
origin: ['http://localhost:8011'],
},
jwt: {
secret: 'ComeNodeDevSecret', // secret for hash
expiresIn: 7 * 24 * 60 * 60, // token expire in x sec
},
jwtLou: {
secret: 'LouNodeDevSecret', // secret for hash
expiresIn: 360, // token expire in x sec
},
});
32 changes: 32 additions & 0 deletions lib/helpers/montaine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Module dependencies
*/
const axios = require('axios');
const jwt = require('jsonwebtoken');
const path = require('path');

const config = require(path.resolve('./config'));


/**
* @desc request
* @param {String} r - request
* @return {} result
*/
exports.request = async (api) => {
try {
const token = jwt.sign({ userId: api.serviceId }, config.jwtLou.secret, { expiresIn: config.jwtLou.expiresIn });

const res = await axios({
method: 'POST',
url: 'http://localhost:3010/api/scraps/worker/5e567f72778ecf488ce269f9',
headers: {
Cookie: `TOKEN=${token}`,
},
data: {},
});
return res;
} catch (err) {
return err.response.data;
}
};
15 changes: 15 additions & 0 deletions modules/apis/controllers/apis.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ exports.delete = async (req, res) => {
}
};

/**
* @desc Endpoint to load the request of the api to the api
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.load = async (req, res) => {
// TODO if (req.scrap && req.user && req.scrap.user && req.scrap.user.id === req.user.id) next();
try {
const data = await ApisService.load(req.api);
responses.success(res, 'api loaded')(data);
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
};


/**
* @desc MiddleWare to ask the service the api for this id
Expand Down
3 changes: 1 addition & 2 deletions modules/apis/models/apis.model.mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const ApiMongoose = new Schema({
title: String,
url: String,
auth: String,
email: String,
password: String,
serviceId: String,
status: Boolean,
banner: String,
description: String,
Expand Down
8 changes: 2 additions & 6 deletions modules/apis/models/apis.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
*/
const joiZxcvbn = require('joi-zxcvbn');
const PlainJoi = require('joi');
const path = require('path');

const Joi = PlainJoi.extend(joiZxcvbn(PlainJoi));
const config = require(path.resolve('./config'));

/**
* Data Schema
*/
const ApiSchema = Joi.object().keys({
title: Joi.string().trim().default('').required(),
url: Joi.string().trim().required(),
auth: Joi.string().valid(['jwt']).optional(),
email: Joi.string().email({ minDomainAtoms: 2 }),
password: Joi.string().min(4).max(128).default('')
.zxcvbn(config.zxcvbn.minimumScore),
auth: Joi.string().valid(['lou']).required(),
serviceId: Joi.string().trim().default('').required(),
status: Joi.boolean().default(false).optional(),
banner: Joi.string().trim().default('').allow('')
.optional(),
Expand Down
10 changes: 2 additions & 8 deletions modules/apis/policies/apis.policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@ exports.invokeRolesPolicies = () => {
}, {
resources: '/api/apis/:apiId',
permissions: '*',
}],
}, {
roles: ['guest'],
allows: [{
resources: '/api/apis',
permissions: ['get'],
}, {
resources: '/api/apis/:apiId',
permissions: ['get'],
resources: '/api/apis/load/:apiId',
permissions: '*',
}],
}]);
};
3 changes: 3 additions & 0 deletions modules/apis/routes/apis.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module.exports = (app) => {
.put(model.isValid(apisSchema.Api), apis.update) // update
.delete(model.isValid(apisSchema.Api), apis.delete); // delete

app.route('/api/apis/load/:apiId')
.get(apis.load);

// Finish by binding the api middleware
app.param('apiId', apis.apiByID);
};
18 changes: 16 additions & 2 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const path = require('path');

const UserService = require(path.resolve('./modules/users/services/user.service.js'));
const montaine = require(path.resolve('./lib/helpers/montaine'));
const ApisRepository = require('../repositories/apis.repository');


Expand Down Expand Up @@ -48,8 +49,7 @@ exports.update = async (api, body) => {
api.title = body.title;
api.url = body.url;
api.auth = body.auth;
api.email = body.email;
api.password = body.password;
api.serviceId = body.serviceId;
api.status = body.status;
api.banner = body.banner;
api.description = body.description;
Expand All @@ -68,3 +68,17 @@ exports.delete = async (api) => {
const result = await ApisRepository.delete(api);
return Promise.resolve(result);
};


/**
* @desc Functio to ask repository to load an api request
* @param {Object} scrap - original scrap
* @return {Promise} scrap
*/
exports.load = async (api) => {
console.log('load');
const result = await montaine.request(api);
console.log('result', result.data);
// return
return Promise.resolve(result.data);
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"dependencies": {
"acl": "~0.4.11",
"axios": "^0.19.2",
"bcrypt": "^4.0.1",
"body-parser": "^1.19.0",
"chalk": "~3.0.0",
Expand Down

0 comments on commit 6b3fad1

Please sign in to comment.