Skip to content

Commit

Permalink
feat(apis): purge unset values during typing ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 24, 2020
1 parent 5a12320 commit b84dea1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions lib/helpers/montaineType.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const typingTypes = require(path.resolve('./lib/helpers/typing/types'));
* @return {data} data - new data generated
*/
const format = (data, action, object) => {
if (!data) throw new AppError('Typing : format data not defined.', { code: 'HELPERS_ERROR' });
if (!data) throw new AppError(`Typing : format data not defined : ${data}`, { code: 'HELPERS_ERROR' });
if (!action) return data;
if (!object) throw new AppError('Typing : format object not defined.', { code: 'HELPERS_ERROR' });
if (!object) throw new AppError(`Typing : format object not defined : ${object}`, { code: 'HELPERS_ERROR' });

const func = action.split('(')[0];
const params = action.split('(')[1].slice(0, -1).split(',');
Expand Down Expand Up @@ -70,7 +70,8 @@ const prepareFormat = (object, schema) => {
const keys = tricks.objectDeepKeys(object); // get all keys
keys.forEach((k) => {
const value = _.get(object, k);
if (!Array.isArray(value) && typeof value !== 'object') {
if (!value || value === '') _.unset(object, k);
else if (!Array.isArray(value) && typeof value !== 'object') {
const p = k.split('.');
let func;
if (/^\d+$/.test(p[p.length - 1])) { // array
Expand Down
8 changes: 4 additions & 4 deletions modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@ exports.load = async (api, start) => {
result.result = request.data.result;
// Mapping
if (result.result && api.mapping && api.mapping !== '') {
result.mapping = montaineMap.map(result.result, JSON.parse(api.mapping));
result.mapping = montaineMap.map(_.cloneDeep(result.result), JSON.parse(api.mapping));
result.result = result.mapping;
}

// Typing
if (result.result && api.typing && api.typing !== '') {
result.typing = montaineType.type(result.result, JSON.parse(api.typing));
result.typing = montaineType.type(_.cloneDeep(result.result), JSON.parse(api.typing));
result.result = result.typing;
}
// prepare for save
if (result.result) {
result.prepare = montaineSave.prepare(result.result, start);
result.mongo = montaineSave.save(result.prepare, start);
result.prepare = montaineSave.prepare(_.cloneDeep(result.result), start);
result.mongo = montaineSave.save(_.cloneDeep(result.prepare), start);
result.result = result.mongo;
if (api.savedb) result.result = await ApisRepository.import(api.slug, result.result);
}
Expand Down

0 comments on commit b84dea1

Please sign in to comment.