api-promise.mjs 3.0 KB

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