Skip to content

Commit

Permalink
chore(eslint): resolve ESLint linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Kristian Flaatten committed Jul 3, 2016
1 parent 61ecaf7 commit ce724f1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 55 deletions.
102 changes: 50 additions & 52 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,66 @@ const CACHE_INVALID = process.env.NTB_CACHE_INVALID || 24 * 60 * 60 * 1000;
const API_ENV = process.env.NTB_API_ENV || 'dev';
const RATELIMIT_UNAUTH = process.env.NTB_RATELIMIT_UNAUTH || 100;

module.exports = () => {
return function middleware(req, res, next) {
let promise;

// API key through Authorization header
if (req.headers.authorization) {
const token = req.headers.authorization.split(' ');
promise = module.exports.getUserByToken(token[1]);

// API key through URL query parameter
} else if (req.query && req.query.api_key) {
promise = module.exports.getUserByToken(req.query.api_key);

// No API key
} else {
promise = module.exports.getUserByIp(
req.headers['x-forwarded-for'] || req.connection.remoteAddres
);
module.exports = () => (req, res, next) => {
let promise;

// API key through Authorization header
if (req.headers.authorization) {
const token = req.headers.authorization.split(' ');
promise = module.exports.getUserByToken(token[1]);

// API key through URL query parameter
} else if (req.query && req.query.api_key) {
promise = module.exports.getUserByToken(req.query.api_key);

// No API key
} else {
promise = module.exports.getUserByIp(
req.headers['x-forwarded-for'] || req.connection.remoteAddres
);
}

promise.then(user => {
req.user = user;

res.set('X-User-Auth', user.auth);
if (user.auth) {
res.set('X-User-Provider', user.provider);
}

promise.then(user => {
req.user = user;
res.set('X-RateLimit-Limit', user.limit);
res.set('X-RateLimit-Reset', user.reset);

res.set('X-User-Auth', user.auth);
if (user.auth) {
res.set('X-User-Provider', user.provider);
}

res.set('X-RateLimit-Limit', user.limit);
res.set('X-RateLimit-Reset', user.reset);
if (!user.hasRemainingQuota()) {
res.set('X-RateLimit-Remaining', 0);

if (!user.hasRemainingQuota()) {
res.set('X-RateLimit-Remaining', 0);
return next(new HttpError(
403, `API rate limit exceeded for ${user.type} "${user.key}"`
));
}

return next(new HttpError(
403, `API rate limit exceeded for ${user.type} "${user.key}"`
));
}
res.set('X-RateLimit-Remaining', user.charge());

res.set('X-RateLimit-Remaining', user.charge());
if (!user.can(req.method)) {
return next(new HttpError(
401, `API authentication required for "${req.method}" requests`
));
}

if (!user.can(req.method)) {
return next(new HttpError(
401, `API authentication required for "${req.method}" requests`
));
res.on('finish', function resOnFinishCb() {
// Uncharge user when certain cache features are used.
// 304 Not Modified, and 412 Precondition Failed
if (this.statusCode === 304 || this.statusCode === 412) {
this.req.user.uncharge();
}

res.on('finish', function resOnFinishCb() {
// Uncharge user when certain cache features are used.
// 304 Not Modified, and 412 Precondition Failed
if (this.statusCode === 304 || this.statusCode === 412) {
this.req.user.uncharge();
}

if (this.req.user.getCharge() > 0) {
redis.hincrby(this.req.user.cacheKey, 'remaining', -1);
}
});
if (this.req.user.getCharge() > 0) {
redis.hincrby(this.req.user.cacheKey, 'remaining', -1);
}
});

return next();
}).catch(next);
};
return next();
}).catch(next);
};

module.exports.getUserByIp = function getUserByIp(key) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"greenkeeper-postpublish": "^1.0.0",
"istanbul": "^0.4.4",
"mocha": "^2.5.3",
"mongodb": "^2.1.18",
"nsp": "^2.5.0",
"semantic-release": "^4.3.5",
"sinon": "^1.17.4",
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const users = require('./support/users');

before(done => {
if (mongo.db) { return done(); }
mongo.on('ready', done);
return mongo.on('ready', done);
});

beforeEach(done => redis.flushall(done));
beforeEach(function (done) { this.timeout(10000); mongo.db.dropDatabase(done); });
beforeEach(function (done) { this.timeout(10000); mongo.api.users.insert(users, done); });
beforeEach(done => mongo.db.dropDatabase(done));
beforeEach(done => mongo.api.users.insert(users, done));

0 comments on commit ce724f1

Please sign in to comment.