pagination.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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(api) {
  92. api.components = api.components || {}
  93. api.components.parameters = { ...(api.components.parameters || {}), ..._api_defs.parameters }
  94. api.components.schemas = { ...(api.components.schemas || {}), ..._api_defs.schemas }
  95. return api
  96. },
  97. // Pagination function
  98. paginate(req, res, last_offset) {
  99. // Skip if the response is not an object.
  100. if (Object.prototype.toString.call(res) != '[object Object]') {
  101. debug('Cannot paginate non-objects.')
  102. return res
  103. }
  104. // Defaults for parameters
  105. const offset = req.query.offset || 0
  106. const limit = req.query.limit || 20
  107. debug('Create pagination links from offset=' + offset, 'limit=' + limit)
  108. // Parse current url
  109. const url = require('url')
  110. const req_url = url.parse(req.protocol + '://' + req.get('host') + req.originalUrl)
  111. const params = new url.URLSearchParams(req_url.query)
  112. // Pagination object
  113. const pagination = {
  114. self: req_url.href,
  115. }
  116. const prev = offset - limit
  117. if (prev >= 0) {
  118. params.set('offset', prev)
  119. req_url.search = params.toString()
  120. pagination.prev = url.format(req_url)
  121. }
  122. const next = offset + limit
  123. if (next >= 0) {
  124. params.set('offset', next)
  125. req_url.search = params.toString()
  126. pagination.next = url.format(req_url)
  127. }
  128. if (last_offset) {
  129. params.set('offset', last_offset)
  130. req_url.search = params.toString()
  131. pagination.last = url.format(req_url)
  132. }
  133. // First
  134. params.set('offset', 0)
  135. req_url.search = params.toString()
  136. pagination.first = url.format(req_url)
  137. debug('pagination', pagination)
  138. // Now set pagination values in response.
  139. res.pagination = pagination
  140. return res
  141. },
  142. }