pagination.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.CursorPage = exports.Page = void 0;
  5. const core_1 = require("./core.js");
  6. /**
  7. * Note: no pagination actually occurs yet, this is for forwards-compatibility.
  8. */
  9. class Page extends core_1.AbstractPage {
  10. constructor(client, response, body, options) {
  11. super(client, response, body, options);
  12. this.data = body.data || [];
  13. this.object = body.object;
  14. }
  15. getPaginatedItems() {
  16. return this.data ?? [];
  17. }
  18. // @deprecated Please use `nextPageInfo()` instead
  19. /**
  20. * This page represents a response that isn't actually paginated at the API level
  21. * so there will never be any next page params.
  22. */
  23. nextPageParams() {
  24. return null;
  25. }
  26. nextPageInfo() {
  27. return null;
  28. }
  29. }
  30. exports.Page = Page;
  31. class CursorPage extends core_1.AbstractPage {
  32. constructor(client, response, body, options) {
  33. super(client, response, body, options);
  34. this.data = body.data || [];
  35. this.has_more = body.has_more || false;
  36. }
  37. getPaginatedItems() {
  38. return this.data ?? [];
  39. }
  40. hasNextPage() {
  41. if (this.has_more === false) {
  42. return false;
  43. }
  44. return super.hasNextPage();
  45. }
  46. // @deprecated Please use `nextPageInfo()` instead
  47. nextPageParams() {
  48. const info = this.nextPageInfo();
  49. if (!info)
  50. return null;
  51. if ('params' in info)
  52. return info.params;
  53. const params = Object.fromEntries(info.url.searchParams);
  54. if (!Object.keys(params).length)
  55. return null;
  56. return params;
  57. }
  58. nextPageInfo() {
  59. const data = this.getPaginatedItems();
  60. if (!data.length) {
  61. return null;
  62. }
  63. const id = data[data.length - 1]?.id;
  64. if (!id) {
  65. return null;
  66. }
  67. return { params: { after: id } };
  68. }
  69. }
  70. exports.CursorPage = CursorPage;
  71. //# sourceMappingURL=pagination.js.map