Commit 1110e4bf authored by Jarrod's avatar Jarrod 💬

Lint

parent 148f0f84
/* global _, sails */
module.exports = {
friendlyName: 'Get API Documentation',
description: 'Get machine-readable API Documentation',
fn: async function(inputs, exits){
fn: async function (inputs, exits) {
var endpointsByMethodName = {}
var extraEndpointsOnlyForTestsByMethodName = {}
_.each(sails.config.routes, (target)=>{
_.each(sails.config.routes, (target) => {
// If the route target is an array, then only consider
// the very last sub-target in the array.
if (_.isArray(target)) {
target = _.last(target)
}//fi
}
// Skip redirects
// (Note that, by doing this, we also skip traditional shorthand
......@@ -39,14 +40,14 @@ module.exports = {
// (but still generate them for use in tests, for convenience)
if (target.view || (bareActionName.match(/^view-/))) {
extraEndpointsOnlyForTestsByMethodName[methodName] = {
verb: (expandedAddress.method||'get').toUpperCase(),
verb: (expandedAddress.method || 'get').toUpperCase(),
url: expandedAddress.url
}
return
}//•
}
endpointsByMethodName[methodName] = {
verb: (expandedAddress.method||'get').toUpperCase(),
verb: (expandedAddress.method || 'get').toUpperCase(),
url: expandedAddress.url,
package: target.package
}
......@@ -57,7 +58,7 @@ module.exports = {
// > method for this one.
var requestable = sails.getActions()[target.action]
if (!requestable) {
sails.log.warn('Skipping unrecognized action: `'+target.action+'`')
sails.log.warn('Skipping unrecognized action: `' + target.action + '`')
return
}
var def = requestable.toJSON && requestable.toJSON()
......@@ -70,7 +71,7 @@ module.exports = {
if (def.args !== undefined) {
endpointsByMethodName[methodName].args = def.args
} else {
endpointsByMethodName[methodName].args = _.reduce(def.inputs, (args, inputDef, inputCodeName)=>{
endpointsByMethodName[methodName].args = _.reduce(def.inputs, (args, inputDef, inputCodeName) => {
args.push(inputCodeName)
return args
}, [])
......@@ -79,9 +80,9 @@ module.exports = {
endpointsByMethodName[methodName].inputs = def.inputs
endpointsByMethodName[methodName].exits = def.exits
}
})//
}) //
return exits.success(endpointsByMethodName)
}
}
\ No newline at end of file
}
/* global _, sails */
module.exports = {
friendlyName: 'Get Cloud SDK JavaScript',
description: 'Generate the configuration for the "Cloud SDK" -- the JavaScript module used for AJAX and WebSockets.',
fn: async function(inputs, exits){
fn: async function (inputs, exits) {
var endpointsByMethodName = {}
var extraEndpointsOnlyForTestsByMethodName = {}
_.each(sails.config.routes, (target)=>{
_.each(sails.config.routes, (target) => {
// If the route target is an array, then only consider
// the very last sub-target in the array.
if (_.isArray(target)) {
target = _.last(target)
}//
} //
// Skip redirects
// (Note that, by doing this, we also skip traditional shorthand
......@@ -39,15 +40,15 @@ module.exports = {
// (but still generate them for use in tests, for convenience)
if (target.view || (bareActionName.match(/^view-/))) {
extraEndpointsOnlyForTestsByMethodName[methodName] = {
verb: (expandedAddress.method||'get').toUpperCase(),
verb: (expandedAddress.method || 'get').toUpperCase(),
url: expandedAddress.url
}
return
}//
} //
endpointsByMethodName[methodName] = {
verb: (expandedAddress.method||'get').toUpperCase(),
url: expandedAddress.url,
verb: (expandedAddress.method || 'get').toUpperCase(),
url: expandedAddress.url
}
// If this is an actions2 action, then determine appropriate serial usage.
......@@ -56,7 +57,7 @@ module.exports = {
// > method for this one.
var requestable = sails.getActions()[target.action]
if (!requestable) {
sails.log.warn('Skipping unrecognized action: `'+target.action+'`')
sails.log.warn('Skipping unrecognized action: `' + target.action + '`')
return
}
var def = requestable.toJSON && requestable.toJSON()
......@@ -64,7 +65,7 @@ module.exports = {
if (def.args !== undefined) {
endpointsByMethodName[methodName].args = def.args
} else {
endpointsByMethodName[methodName].args = _.reduce(def.inputs, (args, inputDef, inputCodeName)=>{
endpointsByMethodName[methodName].args = _.reduce(def.inputs, (args, inputDef, inputCodeName) => {
args.push(inputCodeName)
return args
}, [])
......@@ -76,8 +77,7 @@ module.exports = {
if (target.isSocket) {
endpointsByMethodName[methodName].protocol = 'io.socket'
}
})//∞
}) // ∞
var jsCode =
`/**
......@@ -88,7 +88,7 @@ module.exports = {
* Above all, the purpose of this file is to provide endpoint definitions,
* each of which corresponds with one particular route+action on the server.
*
`+//* > This file was automatically generated. `+new Date()+`
` + //* > This file was automatically generated. ` + new Date() + `
`* > This file was automatically generated.
* > (To regenerate, run \`sails run rebuild-cloud-sdk\`)
*/
......@@ -96,7 +96,7 @@ module.exports = {
Cloud.setup({
/* eslint-disable */
methods: `+JSON.stringify(endpointsByMethodName)+`
methods: ` + JSON.stringify(endpointsByMethodName) + `
/* eslint-enable */
})\n`
......@@ -106,5 +106,4 @@ Cloud.setup({
this.res.set('Content-Type', 'application/javascript')
return exits.success(jsCode)
}
}
\ No newline at end of file
}
const mcache = require('memory-cache');
/* global sails */
const mcache = require('memory-cache')
module.exports = (req, res, next) => {
const logLevel = sails.hooks.ahoy.logLevel || 'debug'
// let routeConfig = req.options // sails.config.routes[ req.method + ' ' + req.route.path ];
let duration = req.options && req.options.cache ? req.options.cache : null;
// let routeConfig = req.options // sails.config.routes[ req.method + ' ' + req.route.path ]
let duration = req.options && req.options.cache ? req.options.cache : null
if (!duration) {
sails.log.warn('Cache middleware called but no cache time specified', req.options)
return next();
return next()
}
let key = '__ahoy_cache__' + req.method + '__'+ req.originalUrl || req.url
let key = '__ahoy_cache__' + req.method + '__' + req.originalUrl || req.url
let cached = mcache.get(key)
// Return the cached copy and bail if we have one
if (cached) {
sails.log[logLevel](' - Sending cached response');
res.set('X-AppCache-Hit', 'HIT');
res.set('Content-Type', cached.contentType);
sails.log[logLevel](' - Sending cached response')
res.set('X-AppCache-Hit', 'HIT')
res.set('Content-Type', cached.contentType)
res.status(cached.statusCode)
res.send(cached.body)
return
} else {
sails.log[logLevel](' - Generating and caching response');
res.sendResponse = res.send
sails.lastRes = res
res.send = (body) => {
// This stops the occasional explosion
if (res.headersSent || !body) { return res }
}
// Else intercept final response and hand off to controller
sails.log[logLevel](' - Generating and caching response')
res.sendResponse = res.send
sails.lastRes = res
res.send = (body) => {
// This stops the occasional explosion
if (res.headersSent || !body) { return res }
mcache.put(key, {
body,
statusCode: res.statusCode,
contentType: res.get('Content-Type')
}, duration * 1000);
res.set('X-AppCache-Hit', 'MISS');
res.sendResponse(body)
return res
}
next()
mcache.put(key, {
body,
statusCode: res.statusCode,
contentType: res.get('Content-Type')
}, duration * 1000)
res.set('X-AppCache-Hit', 'MISS')
res.sendResponse(body)
return res
}
}
\ No newline at end of file
next()
}
/* global sails */
module.exports = function (req, res, next) {
const logLevel = 'debug'
sails.log[logLevel]('Checking permission for ' + req.method + ' ' + req.route.path);
sails.log[logLevel]('Checking permission for ' + req.method + ' ' + req.route.path)
let routeConfig = req.options // sails.config.routes[ req.method + ' ' + req.route.path ];
let routeConfig = req.options // sails.config.routes[ req.method + ' ' + req.route.path ]
if (!routeConfig) { return next() }
let permission = routeConfig.permission;
let permission = routeConfig.permission
if ((routeConfig.auth || permission) && !req.me) {
sails.log[logLevel](' ❌ User is not authenticated - bailing on request');
res.set('X-Token-Update', '');
return res.unauthorized();
sails.log[logLevel](' ❌ User is not authenticated - bailing on request')
res.set('X-Token-Update', '')
return res.unauthorized()
}
if (!permission) {
sails.log[logLevel](' ✓ No special permissions required for route');
return next();
sails.log[logLevel](' ✓ No special permissions required for route')
return next()
}
sails.log[logLevel](' - Permission is required: ', permission);
sails.log[logLevel](' - Permission is required: ', permission)
// TODO: This should be handled a bit more betterly.
// So the user model has permissions[] and you could have 'blog' and something something other permission
......@@ -27,10 +29,10 @@ module.exports = function (req, res, next) {
const isType = `is${permission.charAt(0).toUpperCase()}${permission.substr(1)}`
if (req.me[isType]) {
// if (req.me && User.hasPermission(req.me, User.PERMISSIONS[permission])) {
sails.log[logLevel](' ✓ User has required permission ' + permission);
return next();
sails.log[logLevel](' ✓ User has required permission ' + permission)
return next()
}
sails.log[logLevel](' ❌ Permission does not exist for this user - bailing on request');
return res.forbidden(`You don't have permission to access this resource`);
sails.log[logLevel](' ❌ Permission does not exist for this user - bailing on request')
return res.forbidden(`You don't have permission to access this resource`)
}
const jwt = require('machinepack-jwt-ng');
/* global sails, User */
const jwt = require('machinepack-jwt-ng')
module.exports = async function (req, res, next) {
const logLevel = sails.hooks.ahoy.logLevel || 'info'
......@@ -23,13 +24,13 @@ module.exports = async function (req, res, next) {
}
shimUserProperty()
let token;
let token
try {
token = await jwt.fromRequest(req.headers, {
algorithm: sails.config.custom.jwt.algorithm || 'HS256',
issuer: null,
acceptedKeys: sails.config.custom.jwt.acceptedKeys
});
})
} catch (err) {
// console.log('Unable to load JWT from request headers', err.stack)
}
......
/* global _ */
/**
* User.js
*
......@@ -63,8 +64,8 @@ module.exports = {
type: 'ref',
columnType: 'timestamptz',
defaultsTo: null,
description: 'A timestamp representing the moment at which this user most recently interacted with the backend while logged in (or null if they have not interacted with the backend at all yet).',
},
description: 'A timestamp representing the moment at which this user most recently interacted with the backend while logged in (or null if they have not interacted with the backend at all yet).'
}
/*
passwordResetToken: {
......@@ -134,10 +135,10 @@ without necessarily having a billing card.`
},
customToJSON () {
return _.omit(this, ['password']);
return _.omit(this, ['password'])
},
pickCore (user) {
return _.pick(user, ['id', 'username', 'avatar'])
}
};
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment