pagination.js 4.9 KB

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