Skip to content

Instantly share code, notes, and snippets.

@silentworks
Forked from weyert/Auth.js
Created March 14, 2017 20:45
Show Gist options
  • Save silentworks/e163566416b723edec80b5bf6b63affe to your computer and use it in GitHub Desktop.
Save silentworks/e163566416b723edec80b5bf6b63affe to your computer and use it in GitHub Desktop.
Trails annotations
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
}
}
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