Created
March 13, 2017 19:28
-
-
Save weyert/a3d87ec0dddf6699eb6c095cf0dc85a8 to your computer and use it in GitHub Desktop.
Trails annotations
This file contains hidden or 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
const Annotation = require('ecmas-annotations').Annotation | |
const _ = require('lodash') | |
/** | |
* @module Auth | |
* @description Auth annotation | |
*/ | |
module.exports = class Auth extends Annotation { | |
constructor(data, filePath) { | |
super(data, filePath) | |
} | |
/** | |
* The possible targets | |
* | |
* (Annotation.CONSTRUCTOR, Annotation.PROPERTY, Annotation.METHOD) | |
* | |
* @type {Array} | |
*/ | |
static get targets() { | |
return [ | |
Annotation.METHOD | |
] | |
} | |
/** | |
* The function to call when annotations are find | |
* | |
* @type {Function} | |
*/ | |
handler(app, annotation) { | |
if (!annotation.className) { | |
annotation.className = _.last(annotation.filePath.split('/')).replace('.js', '') | |
} | |
const routeTarget = annotation.className + '.' + annotation.target | |
// Check if the route already exists with a handler with the same controller + method | |
const routeIndex = _.findIndex(app.config.routes, {handler: routeTarget}) | |
if (routeIndex !== -1) { | |
const relevantRoute = app.config.routes[routeIndex] | |
if (false === _.has(relevantRoute, 'config')) { | |
relevantRoute.config = {} | |
} | |
// Check if we have a boolean value as value for the annotation | |
if (_.isBoolean(annotation.value)) { | |
relevantRoute.config.auth = annotation.value | |
} | |
else { | |
// Define the authentication options for this route | |
relevantRoute.config.auth = { | |
'strategies': annotation.strategies, | |
} | |
// Add the `mode`-attribute, if passed | |
if (_.has(annotation, 'mode')) { | |
relevantRoute.config.auth.mode = annotation.mode | |
} | |
// Add the `payload`-attribute, if passed | |
if (_.has(annotation, 'payload')) { | |
relevantRoute.config.auth.payload = annotation.payload | |
} | |
} | |
// Update the existing route in the application configuration | |
app.config.routes[routeIndex] = relevantRoute | |
} | |
} | |
/** | |
* File path | |
* | |
* @type {String} | |
* @required | |
*/ | |
static get path() { | |
return __filename | |
} | |
} |
This file contains hidden or 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
const Annotation = require('ecmas-annotations').Annotation | |
const _ = require('lodash') | |
const schemas = require('../schemas') | |
/** | |
* @module Validate | |
* @description Validate annotation | |
* The Validate-annotation expects the schemas to be stored in api/schemas/ in a similar | |
* approach as the order directories e.g. controller, models etc. | |
*/ | |
module.exports = class Validate extends Annotation { | |
constructor(data, filePath) { | |
super(data, filePath) | |
} | |
/** | |
* The possible targets | |
* | |
* (Annotation.CONSTRUCTOR, Annotation.PROPERTY, Annotation.METHOD) | |
* | |
* @type {Array} | |
*/ | |
static get targets() { | |
return [ | |
Annotation.METHOD | |
] | |
} | |
/** | |
* The function to call when annotations are find | |
* | |
* @type {Function} | |
*/ | |
handler(app, annotation) { | |
if (!annotation.className) { | |
annotation.className = _.last(annotation.filePath.split('/')).replace('.js', '') | |
} | |
const routeTarget = annotation.className + '.' + annotation.target | |
// Check if the route already exists with a handler with the same controller + method | |
const routeIndex = _.findIndex(app.config.routes, {handler: routeTarget}) | |
if (routeIndex !== -1) { | |
const relevantRoute = app.config.routes[routeIndex] | |
if (false === _.has(relevantRoute, 'config')) { | |
relevantRoute.config = {} | |
} | |
// Define the validation options for this route | |
const schemaName = annotation.value | |
const currSchema = schemas[schemaName] | |
if (currSchema) { | |
// Add the validate-key when it's not availble | |
if (false === _.has(relevantRoute.config, 'validate')) { | |
relevantRoute.config.validate = {} | |
} | |
relevantRoute.config.validate.payload = currSchema | |
} | |
// Update the existing route in the application configuration | |
app.config.routes[routeIndex] = relevantRoute | |
} | |
} | |
/** | |
* File path | |
* | |
* @type {String} | |
* @required | |
*/ | |
static get path() { | |
return __filename | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment