// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { type Opencode } from '../client'; import { type PromiseOrValue } from '../internal/types'; import { APIResponseProps, defaultParseResponse } from '../internal/parse'; /** * A subclass of `Promise` providing additional helper methods * for interacting with the SDK. */ export class APIPromise extends Promise { private parsedPromise: Promise | undefined; #client: Opencode; constructor( client: Opencode, private responsePromise: Promise, private parseResponse: ( client: Opencode, props: APIResponseProps, ) => PromiseOrValue = defaultParseResponse, ) { super((resolve) => { // this is maybe a bit weird but this has to be a no-op to not implicitly // parse the response body; instead .then, .catch, .finally are overridden // to parse the response resolve(null as any); }); this.#client = client; } _thenUnwrap(transform: (data: T, props: APIResponseProps) => U): APIPromise { return new APIPromise(this.#client, this.responsePromise, async (client, props) => transform(await this.parseResponse(client, props), props), ); } /** * Gets the raw `Response` instance instead of parsing the response * data. * * If you want to parse the response body but still get the `Response` * instance, you can use {@link withResponse()}. * * 👋 Getting the wrong TypeScript type for `Response`? * Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]` * to your `tsconfig.json`. */ asResponse(): Promise { return this.responsePromise.then((p) => p.response); } /** * Gets the parsed response data and the raw `Response` instance. * * If you just want to get the raw `Response` instance without parsing it, * you can use {@link asResponse()}. * * 👋 Getting the wrong TypeScript type for `Response`? * Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]` * to your `tsconfig.json`. */ async withResponse(): Promise<{ data: T; response: Response }> { const [data, response] = await Promise.all([this.parse(), this.asResponse()]); return { data, response }; } private parse(): Promise { if (!this.parsedPromise) { this.parsedPromise = this.responsePromise.then((data) => this.parseResponse(this.#client, data)); } return this.parsedPromise; } override then( onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null, ): Promise { return this.parse().then(onfulfilled, onrejected); } override catch( onrejected?: ((reason: any) => TResult | PromiseLike) | undefined | null, ): Promise { return this.parse().catch(onrejected); } override finally(onfinally?: (() => void) | undefined | null): Promise { return this.parse().finally(onfinally); } }