pagination.mjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 { AnthropicError } 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 AnthropicError('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. export class Page extends AbstractPage {
  73. constructor(client, response, body, options) {
  74. super(client, response, body, options);
  75. this.data = body.data || [];
  76. this.has_more = body.has_more || false;
  77. this.first_id = body.first_id || null;
  78. this.last_id = body.last_id || null;
  79. }
  80. getPaginatedItems() {
  81. return this.data ?? [];
  82. }
  83. hasNextPage() {
  84. if (this.has_more === false) {
  85. return false;
  86. }
  87. return super.hasNextPage();
  88. }
  89. nextPageRequestOptions() {
  90. if (this.options.query?.['before_id']) {
  91. // in reverse
  92. const first_id = this.first_id;
  93. if (!first_id) {
  94. return null;
  95. }
  96. return {
  97. ...this.options,
  98. query: {
  99. ...maybeObj(this.options.query),
  100. before_id: first_id,
  101. },
  102. };
  103. }
  104. const cursor = this.last_id;
  105. if (!cursor) {
  106. return null;
  107. }
  108. return {
  109. ...this.options,
  110. query: {
  111. ...maybeObj(this.options.query),
  112. after_id: cursor,
  113. },
  114. };
  115. }
  116. }
  117. export class TokenPage extends AbstractPage {
  118. constructor(client, response, body, options) {
  119. super(client, response, body, options);
  120. this.data = body.data || [];
  121. this.has_more = body.has_more || false;
  122. this.next_page = body.next_page || null;
  123. }
  124. getPaginatedItems() {
  125. return this.data ?? [];
  126. }
  127. hasNextPage() {
  128. if (this.has_more === false) {
  129. return false;
  130. }
  131. return super.hasNextPage();
  132. }
  133. nextPageRequestOptions() {
  134. const cursor = this.next_page;
  135. if (!cursor) {
  136. return null;
  137. }
  138. return {
  139. ...this.options,
  140. query: {
  141. ...maybeObj(this.options.query),
  142. page_token: cursor,
  143. },
  144. };
  145. }
  146. }
  147. export class PageCursor extends AbstractPage {
  148. constructor(client, response, body, options) {
  149. super(client, response, body, options);
  150. this.data = body.data || [];
  151. this.has_more = body.has_more || false;
  152. this.next_page = body.next_page || null;
  153. }
  154. getPaginatedItems() {
  155. return this.data ?? [];
  156. }
  157. hasNextPage() {
  158. if (this.has_more === false) {
  159. return false;
  160. }
  161. return super.hasNextPage();
  162. }
  163. nextPageRequestOptions() {
  164. const cursor = this.next_page;
  165. if (!cursor) {
  166. return null;
  167. }
  168. return {
  169. ...this.options,
  170. query: {
  171. ...maybeObj(this.options.query),
  172. page: cursor,
  173. },
  174. };
  175. }
  176. }
  177. //# sourceMappingURL=pagination.mjs.map