From 6cd9b1b3304178e729c530d8ffff8dc55863cfda Mon Sep 17 00:00:00 2001 From: Carlos Almeida Date: Tue, 12 Feb 2019 16:13:21 +0000 Subject: [PATCH 1/3] defaultLimit and maxLimit added to Options --- README.md | 10 ++++++++++ index.js | 21 ++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 614a97a..e395803 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,16 @@ Default value: `[ "*.find" ]`, which auto added to find method of all models. ### `relatedModels`: Boolean +Apply `Content-Range` to all related models. + +### `defaultLimit`: Integer + +Set the default value when no limit parameter is passed on filter. + +### `maxLimit`: Integer + +Set the maximum value of the limit paramater on filter. + ## Tips To get all records, use: `{ "limit": 0 }` diff --git a/index.js b/index.js index 2fe8c0a..a5bebd4 100644 --- a/index.js +++ b/index.js @@ -4,9 +4,10 @@ module.exports = function(app, options) { const applyRange = function(model, name, ctx, next) { if (!ctx.res._headerSent) { - let filter; - let limit = 50; + const maxLimit = options && options.maxLimit; + let limit = options && options.defaultLimit || 50; let offset = 0; + let filter; if (!ctx.args) ctx.args = {}; @@ -17,10 +18,20 @@ module.exports = function(app, options) { if (ctx.args.filter.where) filter = ctx.args.filter.where; - if (ctx.args.filter.limit) - limit = ctx.args.filter.limit; - else + if ( + ctx.args.filter.limit == null || + ctx.args.filter.limit !== parseInt(ctx.args.filter.limit, 10) + ) { ctx.args.filter.limit = limit; + } else if (maxLimit && + maxLimit > 0 && + (ctx.args.filter.limit > maxLimit || ctx.args.filter.limit == 0) + ) { + limit = maxLimit; + ctx.args.filter.limit = maxLimit; + } else { + limit = ctx.args.filter.limit; + } if (ctx.args.filter.offset) offset = ctx.args.filter.offset; From ab1888d172e4ae650d14059b916c784590171dff Mon Sep 17 00:00:00 2001 From: Carlos Almeida Date: Wed, 20 Feb 2019 09:26:22 +0000 Subject: [PATCH 2/3] Fix one to Many relation paths --- index.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index a5bebd4..2a885f9 100644 --- a/index.js +++ b/index.js @@ -38,15 +38,19 @@ module.exports = function(app, options) { else ctx.args.filter.offset = offset; - model.count(filter, function(err, count) { - const last = Math.min(offset + limit, count); - ctx.res.set('Access-Control-Expose-Headers', 'Content-Range'); - ctx.res.set( - 'Content-Range', - `${name.toLowerCase()} ${offset}-${last}/${count}` - ); + if (typeof model.count === 'function') { + model.count(filter, function(err, count) { + const last = Math.min(offset + limit, count); + ctx.res.set('Access-Control-Expose-Headers', 'Content-Range'); + ctx.res.set( + 'Content-Range', + `${name.toLowerCase()} ${offset}-${last}/${count}` + ); + next(); + }); + } else { next(); - }); + } } }; From 5592bb770f6409d757894308b9407287a04bd92a Mon Sep 17 00:00:00 2001 From: Carlos Almeida Date: Wed, 20 Feb 2019 09:28:04 +0000 Subject: [PATCH 3/3] Update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 343c5a3..90292fa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-content-range", - "version": "1.0.2", + "version": "1.1.0", "description": "Add Content header to all search requests for Loopback 3", "main": "index.js", "scripts": {