Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: implement eslint and clean up code
- Loading branch information
Tom Kirkpatrick
committed
Apr 25, 2017
1 parent
4d8da56
commit d6d3525
Showing
7 changed files
with
218 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/coverage/ | ||
test/fixtures/simple-app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "fullcube", | ||
"root": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) | ||
} | ||
}); | ||
}) | ||
|
||
}); | ||
}; | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "fullcube/mocha" | ||
} |
Oops, something went wrong.