pagination.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 apiDefs = {
  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, [lastOffset])
  78. * -> add (valid) pagination fields to response object
  79. * If lastOffset 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 || {}), ...apiDefs.parameters }
  94. api.components.schemas = { ...(api.components.schemas || {}), ...apiDefs.schemas }
  95. return api
  96. },
  97. // Pagination function
  98. paginate(req, res, lastOffset) {
  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. // Disable lint because the code (and tests) relied upon obsolete UrlObject. Remove after migration to TypeScript.
  111. // eslint-disable-next-line node/no-deprecated-api
  112. const reqUrl = url.parse(req.protocol + '://' + req.get('host') + req.originalUrl)
  113. const params = new url.URLSearchParams(reqUrl.query)
  114. // Pagination object
  115. const pagination = {
  116. self: reqUrl.href,
  117. }
  118. const prev = offset - limit
  119. if (prev >= 0) {
  120. params.set('offset', prev)
  121. reqUrl.search = params.toString()
  122. pagination.prev = url.format(reqUrl)
  123. }
  124. const next = offset + limit
  125. if (next >= 0) {
  126. params.set('offset', next)
  127. reqUrl.search = params.toString()
  128. pagination.next = url.format(reqUrl)
  129. }
  130. if (lastOffset) {
  131. params.set('offset', lastOffset)
  132. reqUrl.search = params.toString()
  133. pagination.last = url.format(reqUrl)
  134. }
  135. // First
  136. params.set('offset', 0)
  137. reqUrl.search = params.toString()
  138. pagination.first = url.format(reqUrl)
  139. debug('pagination', pagination)
  140. // Now set pagination values in response.
  141. res.pagination = pagination
  142. return res
  143. },
  144. }