pagination.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. var _AbstractPage_client;
  3. import { __classPrivateFieldGet, __classPrivateFieldSet } from "../internal/tslib.mjs";
  4. import { OpenAIError } from "./error.mjs";
  5. import { defaultParseResponse } from "../internal/parse.mjs";
  6. import { APIPromise } from "./api-promise.mjs";
  7. import { maybeObj } from "../internal/utils/values.mjs";
  8. export class AbstractPage {
  9. constructor(client, response, body, options) {
  10. _AbstractPage_client.set(this, void 0);
  11. __classPrivateFieldSet(this, _AbstractPage_client, client, "f");
  12. this.options = options;
  13. this.response = response;
  14. this.body = body;
  15. }
  16. hasNextPage() {
  17. const items = this.getPaginatedItems();
  18. if (!items.length)
  19. return false;
  20. return this.nextPageRequestOptions() != null;
  21. }
  22. async getNextPage() {
  23. const nextOptions = this.nextPageRequestOptions();
  24. if (!nextOptions) {
  25. throw new OpenAIError('No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.');
  26. }
  27. return await __classPrivateFieldGet(this, _AbstractPage_client, "f").requestAPIList(this.constructor, nextOptions);
  28. }
  29. async *iterPages() {
  30. let page = this;
  31. yield page;
  32. while (page.hasNextPage()) {
  33. page = await page.getNextPage();
  34. yield page;
  35. }
  36. }
  37. async *[(_AbstractPage_client = new WeakMap(), Symbol.asyncIterator)]() {
  38. for await (const page of this.iterPages()) {
  39. for (const item of page.getPaginatedItems()) {
  40. yield item;
  41. }
  42. }
  43. }
  44. }
  45. /**
  46. * This subclass of Promise will resolve to an instantiated Page once the request completes.
  47. *
  48. * It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg:
  49. *
  50. * for await (const item of client.items.list()) {
  51. * console.log(item)
  52. * }
  53. */
  54. export class PagePromise extends APIPromise {
  55. constructor(client, request, Page) {
  56. super(client, request, async (client, props) => new Page(client, props.response, await defaultParseResponse(client, props), props.options));
  57. }
  58. /**
  59. * Allow auto-paginating iteration on an unawaited list call, eg:
  60. *
  61. * for await (const item of client.items.list()) {
  62. * console.log(item)
  63. * }
  64. */
  65. async *[Symbol.asyncIterator]() {
  66. const page = await this;
  67. for await (const item of page) {
  68. yield item;
  69. }
  70. }
  71. }
  72. /**
  73. * Note: no pagination actually occurs yet, this is for forwards-compatibility.
  74. */
  75. export class Page extends AbstractPage {
  76. constructor(client, response, body, options) {
  77. super(client, response, body, options);
  78. this.data = body.data || [];
  79. this.object = body.object;
  80. }
  81. getPaginatedItems() {
  82. return this.data ?? [];
  83. }
  84. nextPageRequestOptions() {
  85. return null;
  86. }
  87. }
  88. export class CursorPage extends AbstractPage {
  89. constructor(client, response, body, options) {
  90. super(client, response, body, options);
  91. this.data = body.data || [];
  92. this.has_more = body.has_more || false;
  93. }
  94. getPaginatedItems() {
  95. return this.data ?? [];
  96. }
  97. hasNextPage() {
  98. if (this.has_more === false) {
  99. return false;
  100. }
  101. return super.hasNextPage();
  102. }
  103. nextPageRequestOptions() {
  104. const data = this.getPaginatedItems();
  105. const id = data[data.length - 1]?.id;
  106. if (!id) {
  107. return null;
  108. }
  109. return {
  110. ...this.options,
  111. query: {
  112. ...maybeObj(this.options.query),
  113. after: id,
  114. },
  115. };
  116. }
  117. }
  118. export class ConversationCursorPage extends AbstractPage {
  119. constructor(client, response, body, options) {
  120. super(client, response, body, options);
  121. this.data = body.data || [];
  122. this.has_more = body.has_more || false;
  123. this.last_id = body.last_id || '';
  124. }
  125. getPaginatedItems() {
  126. return this.data ?? [];
  127. }
  128. hasNextPage() {
  129. if (this.has_more === false) {
  130. return false;
  131. }
  132. return super.hasNextPage();
  133. }
  134. nextPageRequestOptions() {
  135. const cursor = this.last_id;
  136. if (!cursor) {
  137. return null;
  138. }
  139. return {
  140. ...this.options,
  141. query: {
  142. ...maybeObj(this.options.query),
  143. after: cursor,
  144. },
  145. };
  146. }
  147. }
  148. //# sourceMappingURL=pagination.mjs.map