Skip to content

Commit

Permalink
style: implement eslint and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Kirkpatrick committed Apr 25, 2017
1 parent 4d8da56 commit d6d3525
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 219 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
@@ -0,0 +1,2 @@
/coverage/
test/fixtures/simple-app
4 changes: 4 additions & 0 deletions .eslintrc
@@ -0,0 +1,4 @@
{
"extends": "fullcube",
"root": true
}
14 changes: 7 additions & 7 deletions lib/index.js
@@ -1,10 +1,10 @@
var deprecate = require('depd')('loopback-ds-readonly-mixin');
var readOnly = require('./read-only');
const deprecate = require('depd')('loopback-ds-readonly-mixin')
const readOnly = require('./read-only')

module.exports = function mixin(app) {
'use strict';
'use strict'
app.loopback.modelBuilder.mixins.define = deprecate.function(app.loopback.modelBuilder.mixins.define,
'app.modelBuilder.mixins.define: Use mixinSources instead; ' +
'see https://github.com/fullcube/loopback-ds-readonly-mixin#mixinsources');
app.loopback.modelBuilder.mixins.define('ReadOnly', readOnly);
};
'app.modelBuilder.mixins.define: Use mixinSources instead ' +
'see https://github.com/fullcube/loopback-ds-readonly-mixin#mixinsources')
app.loopback.modelBuilder.mixins.define('ReadOnly', readOnly)
}
131 changes: 65 additions & 66 deletions lib/read-only.js
@@ -1,83 +1,82 @@
var debug = require('debug')('loopback-ds-readonly-mixin');
const debug = require('debug')('loopback-ds-readonly-mixin')

module.exports = function(Model) {
'use strict';
module.exports = Model => {
'use strict'

debug('ReadOnly mixin for Model %s', Model.modelName);
debug('ReadOnly mixin for Model %s', Model.modelName)

var app;
Model.on('attached', () => {
Model.stripReadOnlyProperties = (modelName, ctx, next) => {
debug('stripReadOnlyProperties for model %s (via remote method %o)', modelName, ctx.methodString)
const body = ctx.req.body

Model.stripReadOnlyProperties = function(modelName, ctx, next) {
debug('stripReadOnlyProperties for model %s (via remote method %o)', modelName, ctx.methodString);
var body = ctx.req.body;
if (!body) {
return next();
}
if (!body) {
return next()
}

const AffectedModel = Model.app.loopback.getModel(modelName);
const options = AffectedModel.settings.mixins.ReadOnly;
const AffectedModel = Model.app.loopback.getModel(modelName)
const options = AffectedModel.settings.mixins.ReadOnly
const properties = (Object.keys(options).length) ? options : null

var properties = (Object.keys(options).length) ? options : null;
if (properties) {
debug('Found read only properties for model %s: %o', modelName, properties);
Object.keys(properties).forEach(function(key) {
debug('The \'%s\' property is read only, removing incoming data', key);
delete body[key];
});
next();
} else {
var err = new Error('Unable to update: ' + modelName + ' is read only.');
err.statusCode = 403;
next(err);
}
};
if (properties) {
debug('Found read only properties for model %s: %o', modelName, properties)
Object.keys(properties).forEach(key => {
debug('The \'%s\' property is read only, removing incoming data', key)
delete body[key]
})
return next()
}
const err = new Error(`Unable to update: ${modelName} is read only.`)

Model.on('attached', function(a) {
app = a;
err.statusCode = 403
return next(err)
}

// Handle native model methods.
Model.beforeRemote('create', function(ctx, modelInstance, next) {
Model.stripReadOnlyProperties(Model.modelName, ctx, next);
});
Model.beforeRemote('upsert', function(ctx, modelInstance, next) {
Model.stripReadOnlyProperties(Model.modelName, ctx, next);
});
Model.beforeRemote('replaceOrCreate', function(ctx, modelInstance, next) {
Model.stripReadOnlyProperties(Model.modelName, ctx, next);
});
Model.beforeRemote('patchOrCreate', function(ctx, modelInstance, next) {
Model.stripReadOnlyProperties(Model.modelName, ctx, next);
});
Model.beforeRemote('prototype.updateAttributes', function(ctx, modelInstance, next) {
Model.stripReadOnlyProperties(Model.modelName, ctx, next);
});
Model.beforeRemote('prototype.patchAttributes', function(ctx, modelInstance, next) {
Model.stripReadOnlyProperties(Model.modelName, ctx, next);
});
Model.beforeRemote('updateAll', function(ctx, modelInstance, next) {
Model.stripReadOnlyProperties(Model.modelName, ctx, next);
});
Model.beforeRemote('upsertWithWhere', function(ctx, modelInstance, next) {
Model.stripReadOnlyProperties(Model.modelName, ctx, next);
});
Model.beforeRemote('replaceById', function(ctx, modelInstance, next) {
Model.stripReadOnlyProperties(Model.modelName, ctx, next);
});
Model.beforeRemote('create', (ctx, modelInstance, next) => {
Model.stripReadOnlyProperties(Model.modelName, ctx, next)
})
Model.beforeRemote('upsert', (ctx, modelInstance, next) => {
Model.stripReadOnlyProperties(Model.modelName, ctx, next)
})
Model.beforeRemote('replaceOrCreate', (ctx, modelInstance, next) => {
Model.stripReadOnlyProperties(Model.modelName, ctx, next)
})
Model.beforeRemote('patchOrCreate', (ctx, modelInstance, next) => {
Model.stripReadOnlyProperties(Model.modelName, ctx, next)
})
Model.beforeRemote('prototype.updateAttributes', (ctx, modelInstance, next) => {
Model.stripReadOnlyProperties(Model.modelName, ctx, next)
})
Model.beforeRemote('prototype.patchAttributes', (ctx, modelInstance, next) => {
Model.stripReadOnlyProperties(Model.modelName, ctx, next)
})
Model.beforeRemote('updateAll', (ctx, modelInstance, next) => {
Model.stripReadOnlyProperties(Model.modelName, ctx, next)
})
Model.beforeRemote('upsertWithWhere', (ctx, modelInstance, next) => {
Model.stripReadOnlyProperties(Model.modelName, ctx, next)
})
Model.beforeRemote('replaceById', (ctx, modelInstance, next) => {
Model.stripReadOnlyProperties(Model.modelName, ctx, next)
})

// Handle updates via relationship.
Object.keys(Model.definition.settings.relations).forEach(relationName => {
var relation = Model.definition.settings.relations[relationName];
const relation = Model.definition.settings.relations[relationName]

if (relation.type.startsWith('has')) {
var modelName = relation.model;
var AffectedModel = Model.app.loopback.getModel(modelName);
Model.beforeRemote(`prototype.__updateById__${relationName}`, function(ctx, modelInstance, next) {
const modelName = relation.model
const AffectedModel = Model.app.loopback.getModel(modelName)

Model.beforeRemote(`prototype.__updateById__${relationName}`, (ctx, modelInstance, next) => {
if (typeof AffectedModel.stripReadOnlyProperties === 'function') {
return AffectedModel.stripReadOnlyProperties(modelName, ctx, next);
return AffectedModel.stripReadOnlyProperties(modelName, ctx, next)
}
return next();
});
return next()
})
}
});
})

});
};
})
}
24 changes: 13 additions & 11 deletions package.json
Expand Up @@ -27,28 +27,30 @@
"test"
],
"scripts": {
"lint": "jscs lib && jshint lib",
"test": "nyc mocha -R spec --timeout 10000 test",
"dev": "DEBUG=*:ping NODE_ENV=development nodemon test/fixtures/simple-app/server/server.js --watch ./lib --watch ./test --ignore db.json --ext js,json",
"lint": "eslint .",
"pretest": "npm run lint",
"test": "NODE_ENV=test nyc --reporter=lcov --reporter=text --reporter=text-summary mocha test/*test.js",
"test:watch": "npm run test -- -w",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"outdated": "npm outdated --depth=0"
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"dependencies": {
"debug": "^2.6.4",
"depd": "^1.1.0"
},
"devDependencies": {
"bluebird": "latest",
"chai": "latest",
"coveralls": "latest",
"jscs": "latest",
"jshint": "latest",
"chai": "^3.5.0",
"coveralls": "^2.13.0",
"dirty-chai": "^1.2.2",
"eslint": "^2.11.1",
"eslint-config-fullcube": "^1.0.46",
"loopback": "^3.6.0",
"loopback-boot": "^2.24.0",
"loopback-component-explorer": "^4.2.0",
"loopback-testing": "^1.4.0",
"mocha": "latest",
"nyc": "latest",
"mocha": "^3.3.0",
"nodemon": "^1.11.0",
"nyc": "^10.2.0",
"supertest": "^3.0.0"
}
}
3 changes: 3 additions & 0 deletions test/.eslintrc
@@ -0,0 +1,3 @@
{
"extends": "fullcube/mocha"
}

0 comments on commit d6d3525

Please sign in to comment.