api-promise.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  3. var _APIPromise_client;
  4. Object.defineProperty(exports, "__esModule", { value: true });
  5. exports.APIPromise = void 0;
  6. const tslib_1 = require("../internal/tslib.js");
  7. const parse_1 = require("../internal/parse.js");
  8. /**
  9. * A subclass of `Promise` providing additional helper methods
  10. * for interacting with the SDK.
  11. */
  12. class APIPromise extends Promise {
  13. constructor(client, responsePromise, parseResponse = parse_1.defaultParseResponse) {
  14. super((resolve) => {
  15. // this is maybe a bit weird but this has to be a no-op to not implicitly
  16. // parse the response body; instead .then, .catch, .finally are overridden
  17. // to parse the response
  18. resolve(null);
  19. });
  20. this.responsePromise = responsePromise;
  21. this.parseResponse = parseResponse;
  22. _APIPromise_client.set(this, void 0);
  23. tslib_1.__classPrivateFieldSet(this, _APIPromise_client, client, "f");
  24. }
  25. _thenUnwrap(transform) {
  26. return new APIPromise(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), this.responsePromise, async (client, props) => (0, parse_1.addRequestID)(transform(await this.parseResponse(client, props), props), props.response));
  27. }
  28. /**
  29. * Gets the raw `Response` instance instead of parsing the response
  30. * data.
  31. *
  32. * If you want to parse the response body but still get the `Response`
  33. * instance, you can use {@link withResponse()}.
  34. *
  35. * 👋 Getting the wrong TypeScript type for `Response`?
  36. * Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
  37. * to your `tsconfig.json`.
  38. */
  39. asResponse() {
  40. return this.responsePromise.then((p) => p.response);
  41. }
  42. /**
  43. * Gets the parsed response data, the raw `Response` instance and the ID of the request,
  44. * returned via the X-Request-ID header which is useful for debugging requests and reporting
  45. * issues to OpenAI.
  46. *
  47. * If you just want to get the raw `Response` instance without parsing it,
  48. * you can use {@link asResponse()}.
  49. *
  50. * 👋 Getting the wrong TypeScript type for `Response`?
  51. * Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
  52. * to your `tsconfig.json`.
  53. */
  54. async withResponse() {
  55. const [data, response] = await Promise.all([this.parse(), this.asResponse()]);
  56. return { data, response, request_id: response.headers.get('x-request-id') };
  57. }
  58. parse() {
  59. if (!this.parsedPromise) {
  60. this.parsedPromise = this.responsePromise.then((data) => this.parseResponse(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), data));
  61. }
  62. return this.parsedPromise;
  63. }
  64. then(onfulfilled, onrejected) {
  65. return this.parse().then(onfulfilled, onrejected);
  66. }
  67. catch(onrejected) {
  68. return this.parse().catch(onrejected);
  69. }
  70. finally(onfinally) {
  71. return this.parse().finally(onfinally);
  72. }
  73. }
  74. exports.APIPromise = APIPromise;
  75. _APIPromise_client = new WeakMap();
  76. //# sourceMappingURL=api-promise.js.map