pagination.mjs 1.9 KB

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