Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
fknop committed Dec 23, 2015
1 parent dbe1459 commit a086c1f
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 80 deletions.
44 changes: 25 additions & 19 deletions lib/config.js
Expand Up @@ -77,15 +77,19 @@ const internals = {
include: ['*'],
exclude: []
}
}
},

schemas: {}
};

internals.metaParameter = Joi.object({

internals.schemas.metaParameter = Joi.object({
active: Joi.boolean().required(),
name: Joi.string().required()
});

internals.schema = Joi.object({

internals.schemas.options = Joi.object({
query: Joi.object({
page: Joi.object({
name: Joi.string().required(),
Expand All @@ -104,14 +108,14 @@ internals.schema = Joi.object({
meta: Joi.object({
baseUri: Joi.string().allow(''),
name: Joi.string().required(),
count: internals.metaParameter,
totalCount: internals.metaParameter,
pageCount: internals.metaParameter,
self: internals.metaParameter,
previous: internals.metaParameter,
next: internals.metaParameter,
first: internals.metaParameter,
last: internals.metaParameter,
count: internals.schemas.metaParameter,
totalCount: internals.schemas.metaParameter,
pageCount: internals.schemas.metaParameter,
self: internals.schemas.metaParameter,
previous: internals.schemas.metaParameter,
next: internals.schemas.metaParameter,
first: internals.schemas.metaParameter,
last: internals.schemas.metaParameter,
page: Joi.object({
active: Joi.boolean().required()
}),
Expand All @@ -132,33 +136,35 @@ internals.schema = Joi.object({
})
});

internals.routeSchema = Joi.object({

internals.schemas.routeOptions = Joi.object({
enabled: Joi.boolean(),
defaults: Joi.object({
page: Joi.number().integer().positive(),
limit: Joi.number().integer().positive(),
pagination: Joi.boolean()
})

});


module.exports = {
getConfig: function (server, options) {

const config = Hoek.applyToDefaults(internals.defaults, options);
const routeSettings = _.map(server.table()[0].table, item => item.settings);
const routeSettings = _.map(server.table()[0].table, (item) => item.settings);

// Check main config
const result = Joi.validate(config, internals.schema);
const result = Joi.validate(config, internals.schemas.options);
if (result.error) {
throw result.error;
}

// Check each route settings if config is valid
_.each(routeSettings, setting => {
const result = Joi.validate(setting.plugins.pagination, internals.routeSchema);
if (result.error) {
throw result.error;
_.each(routeSettings, (setting) => {

const res = Joi.validate(setting.plugins.pagination, internals.schemas.routeOptions);
if (res.error) {
throw res.error;
}
});

Expand Down
1 change: 1 addition & 0 deletions lib/decorate.js
Expand Up @@ -3,6 +3,7 @@
const Hoek = require('hoek');

module.exports = function (config) {

return {
paginate: function (results, totalCount) {

Expand Down
108 changes: 60 additions & 48 deletions lib/ext.js
Expand Up @@ -6,54 +6,69 @@ const Hoek = require('hoek');
const Qs = require('qs');

const internals = {
getRouteOptions: function(request) {
return request.route.settings.plugins.pagination || {};
},
containsPath: (array, path) => {
isUndefined: (value) => (typeof value === 'undefined'),


getRouteOptions: (request) => request.route.settings.plugins.pagination || {},


containsPath: function (array, path) {

return _.any(array, (item) => {

return item instanceof RegExp ? item.test(path) : item === path;
});
},


isValidRoute: function (request) {

const include = this.config.routes.include;
const exclude = this.config.routes.exclude;
const include = internals.config.routes.include;
const exclude = internals.config.routes.exclude;
const path = request.route.path;
const method = request.route.method;
const options = this.getRouteOptions(request);
const options = internals.getRouteOptions(request);


if (typeof options.enabled !== 'undefined') {
if (!internals.isUndefined(options.enabled)) {
return options.enabled;
}

return (method === 'get' &&
(include[0] === '*' || this.containsPath(include, path)) &&
!this.containsPath(exclude, path));
(include[0] === '*' || internals.containsPath(include, path)) &&
!internals.containsPath(exclude, path));
},


getPagination: function (request, config) {

const routeDefaults = this.getRouteOptions(request).defaults || {};
const routeDefaults = internals.getRouteOptions(request).defaults || {};
let pagination = request.query[config.query.pagination.name];

if (pagination === 'false') {
pagination = false;
} else if (pagination === 'true') {
}
else if (pagination === 'true') {
pagination = true;
} else if (typeof routeDefaults.pagination !== 'undefined') {
}
else if (!internals.isUndefined(routeDefaults.pagination)) {
pagination = routeDefaults.pagination;
} else {
}
else {
pagination = config.query.pagination.default;
}

return pagination;
},
name: function (arg) {
return this.config.meta[arg].name;
},


name: (arg) => internals.config.meta[arg].name,


assignIfActive: function (meta, name, value) {
if (this.config.meta[name].active) {
meta[this.name(name)] = value;

if (internals.config.meta[name].active) {
meta[internals.name(name)] = value;
}
}
};
Expand All @@ -79,18 +94,8 @@ module.exports = function (config) {
return reply.continue();
}

let page, limit;
if (typeof routeDefaults.page !== 'undefined') {
page = routeDefaults.page;
} else {
page = config.query.page.default;
}

if (typeof routeDefaults.limit !== 'undefined') {
limit = routeDefaults.limit;
} else {
limit = config.query.limit.default;
}
const page = routeDefaults.page || config.query.page.default;
const limit = routeDefaults.limit || config.query.limit.default;

const setParam = function (arg, defaultValue) {

Expand All @@ -103,7 +108,8 @@ module.exports = function (config) {
if (_.isNaN(value)) {
if (config.query.invalid === 'defaults') {
value = config.query[arg].default;
} else {
}
else {
throw { message: 'Invalid ' + name };
}
}
Expand All @@ -115,7 +121,8 @@ module.exports = function (config) {
try {
setParam('page', page);
setParam('limit', limit);
} catch (err) {
}
catch (err) {
return reply(Boom.badRequest(err.message));
}
}
Expand All @@ -124,8 +131,6 @@ module.exports = function (config) {
},




onPreResponse: function (request, reply) {

if (internals.isValidRoute(request) &&
Expand All @@ -143,7 +148,10 @@ module.exports = function (config) {
Hoek.assert(Array.isArray(results), 'The results must be an array');

let totalCount = source.totalCount;
if (typeof totalCount === 'undefined') {

// source.totalCount || request[config.meta.totalCount.name] does
// not work because of 0
if (internals.isUndefined(totalCount)) {
totalCount = request[config.meta.totalCount.name];
}

Expand All @@ -153,34 +161,38 @@ module.exports = function (config) {
const currentLimit = qs[config.query.limit.name];

const getPageCount = function () {
if (typeof totalCount === 'undefined') {

if (internals.isUndefined(totalCount)) {
return null;
} else if (totalCount === 0) {
}

if (totalCount === 0) {
return 0;
} else {
return Math.trunc(totalCount / currentLimit) +
((totalCount % currentLimit === 0) ? 0 : 1);
}

return Math.trunc(totalCount / currentLimit) +
((totalCount % currentLimit === 0) ? 0 : 1);
};

const getUri = function (page) {

if (!page) {
return null;
} else {
const override = {
[config.query.page.name]: page
};

return baseUri + Qs.stringify(Hoek.applyToDefaults(qs, override));
}

const override = {
[config.query.page.name]: page
};

return baseUri + Qs.stringify(Hoek.applyToDefaults(qs, override));
};

const meta = {};
internals.assignIfActive(meta, 'page', qs[internals.name('page')]);
internals.assignIfActive(meta, 'limit', qs[internals.name('limit')]);
internals.assignIfActive(meta, 'count', results.length);

internals.assignIfActive(meta, 'totalCount', typeof totalCount === 'undefined' ? null : totalCount);
internals.assignIfActive(meta, 'totalCount', internals.isUndefined(totalCount) ? null : totalCount);
internals.assignIfActive(meta, 'pageCount', getPageCount());
internals.assignIfActive(meta, 'self', baseUri + Qs.stringify(qs));
internals.assignIfActive(meta, 'previous', currentPage !== 1 ? getUri(currentPage - 1) : null);
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Expand Up @@ -35,7 +35,8 @@ exports.register = function (server, options, next) {

return next();

} catch (error) {
}
catch (error) {
return next(error);
}

Expand Down

0 comments on commit a086c1f

Please sign in to comment.