swagger-express-middleware icon indicating copy to clipboard operation
swagger-express-middleware copied to clipboard

support limit and offset query params

Open fheidrich opened this issue 8 years ago • 2 comments

In CRUD GET over a collection the query params offset and limit are usually used to control pagination. For example, limit is in the example for http://swagger.io/specification/#parametersDefinitionsObject

swagger-express-middleware seems to handle offset=0 as a search for resources if the field offset having the value 0.

fheidrich avatar May 05 '16 23:05 fheidrich

a basic implementation would look like query-collection.js#filter()

  // Build the filter object
  var offset = undefined, limit = undefined;
  var filterCriteria = {data: {}};
  queryParams.forEach(function(param) {
    if (req.query[param.name] !== undefined) {
      if (param.name === "offset" && param.type === "integer") {
        offset = req.query[param.name];
      } else if (param.name === "limit" && param.type === "integer") {
        limit = req.query[param.name];
      } else {
        setDeepProperty(filterCriteria.data, param.name, req.query[param.name]);
      }
    }
  });
  if (offset !== undefined || limit !== undefined) {
    util.debug("Filtering pagination offset %d, limit %d", offset || 0, limit || resources.length);
    if (offset === undefined) {
      resources.length = limit;
    } else {
      if (limit !== undefined) {
        resources = resources.slice(offset, offset + limit);
      } else {
        resources = resources.slice(offset);
      }
    }
  }

fheidrich avatar May 06 '16 00:05 fheidrich

:+1:

fragsalat avatar Jan 10 '17 12:01 fragsalat