pagination.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict';
  19. const debug = require('debug')('joystream:middleware:pagination');
  20. // Pagination definitions
  21. const _api_defs = {
  22. parameters: {
  23. paginationLimit: {
  24. name: 'limit',
  25. in: 'query',
  26. description: 'Number of items per page.',
  27. required: false,
  28. schema: {
  29. type: 'integer',
  30. minimum: 1,
  31. maximum: 50,
  32. default: 20,
  33. },
  34. },
  35. paginationOffset: {
  36. name: 'offset',
  37. in: 'query',
  38. description: 'Page number (offset)',
  39. schema: {
  40. type: 'integer',
  41. minimum: 0,
  42. },
  43. },
  44. },
  45. schemas: {
  46. PaginationInfo: {
  47. type: 'object',
  48. required: ['self'],
  49. properties: {
  50. 'self': {
  51. type: 'string',
  52. },
  53. next: {
  54. type: 'string',
  55. },
  56. prev: {
  57. type: 'string',
  58. },
  59. first: {
  60. type: 'string',
  61. },
  62. last: {
  63. type: 'string',
  64. },
  65. },
  66. },
  67. },
  68. };
  69. /**
  70. * Silly pagination because it's faster than getting other modules to work.
  71. *
  72. * Usage:
  73. * - apiDoc.parameters = pagination.parameters
  74. * -> Validates pagination parameters
  75. * - apiDoc.responses.200.schema.pagination = pagination.response
  76. * -> Generates pagination info on response
  77. * - paginate(req, res, [last_offset])
  78. * -> add (valid) pagination fields to response object
  79. * If last_offset is given, create a last link with that offset
  80. **/
  81. module.exports = {
  82. // Add pagination parameters and pagination info responses.
  83. parameters: [
  84. { '$ref': '#/components/parameters/paginationLimit' },
  85. { '$ref': '#/components/parameters/paginationOffset' },
  86. ],
  87. response: {
  88. '$ref': '#/components/schema/PaginationInfo'
  89. },
  90. // Update swagger/openapi specs with our own parameters and definitions
  91. openapi: function(api)
  92. {
  93. api.components = api.components || {};
  94. api.components.parameters = { ...api.components.parameters || {} , ..._api_defs.parameters };
  95. api.components.schemas = { ...api.components.schemas || {}, ..._api_defs.schemas };
  96. return api;
  97. },
  98. // Pagination function
  99. paginate: function(req, res, last_offset)
  100. {
  101. // Skip if the response is not an object.
  102. if (Object.prototype.toString.call(res) != "[object Object]") {
  103. debug('Cannot paginate non-objects.');
  104. return res;
  105. }
  106. // Defaults for parameters
  107. var offset = req.query.offset || 0;
  108. var limit = req.query.limit || 20;
  109. debug('Create pagination links from offset=' + offset, 'limit=' + limit);
  110. // Parse current url
  111. const url = require('url');
  112. var req_url = url.parse(req.protocol + '://' + req.get('host') + req.originalUrl);
  113. var params = new url.URLSearchParams(req_url.query);
  114. // Pagination object
  115. var pagination = {
  116. 'self': req_url.href,
  117. }
  118. var prev = offset - limit;
  119. if (prev >= 0) {
  120. params.set('offset', prev);
  121. req_url.search = params.toString();
  122. pagination['prev'] = url.format(req_url);
  123. }
  124. var next = offset + limit;
  125. if (next >= 0) {
  126. params.set('offset', next);
  127. req_url.search = params.toString();
  128. pagination['next'] = url.format(req_url);
  129. }
  130. if (last_offset) {
  131. params.set('offset', last_offset);
  132. req_url.search = params.toString();
  133. pagination['last'] = url.format(req_url);
  134. }
  135. // First
  136. params.set('offset', 0);
  137. req_url.search = params.toString();
  138. pagination['first'] = url.format(req_url);
  139. debug('pagination', pagination);
  140. // Now set pagination values in response.
  141. res.pagination = pagination;
  142. return res;
  143. },
  144. };