72 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
		
		
			
		
	
	
			72 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
|  | // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
 | ||
|  | var _APIPromise_client; | ||
|  | import { __classPrivateFieldGet, __classPrivateFieldSet } from "../internal/tslib.mjs"; | ||
|  | import { defaultParseResponse, addRequestID, } from "../internal/parse.mjs"; | ||
|  | /** | ||
|  |  * A subclass of `Promise` providing additional helper methods | ||
|  |  * for interacting with the SDK. | ||
|  |  */ | ||
|  | export class APIPromise extends Promise { | ||
|  |     constructor(client, responsePromise, parseResponse = 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); | ||
|  |         }); | ||
|  |         this.responsePromise = responsePromise; | ||
|  |         this.parseResponse = parseResponse; | ||
|  |         _APIPromise_client.set(this, void 0); | ||
|  |         __classPrivateFieldSet(this, _APIPromise_client, client, "f"); | ||
|  |     } | ||
|  |     _thenUnwrap(transform) { | ||
|  |         return new APIPromise(__classPrivateFieldGet(this, _APIPromise_client, "f"), this.responsePromise, async (client, props) => addRequestID(transform(await this.parseResponse(client, props), props), props.response)); | ||
|  |     } | ||
|  |     /** | ||
|  |      * 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() { | ||
|  |         return this.responsePromise.then((p) => p.response); | ||
|  |     } | ||
|  |     /** | ||
|  |      * Gets the parsed response data, the raw `Response` instance and the ID of the request, | ||
|  |      * returned via the X-Request-ID header which is useful for debugging requests and reporting | ||
|  |      * issues to OpenAI. | ||
|  |      * | ||
|  |      * 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() { | ||
|  |         const [data, response] = await Promise.all([this.parse(), this.asResponse()]); | ||
|  |         return { data, response, request_id: response.headers.get('x-request-id') }; | ||
|  |     } | ||
|  |     parse() { | ||
|  |         if (!this.parsedPromise) { | ||
|  |             this.parsedPromise = this.responsePromise.then((data) => this.parseResponse(__classPrivateFieldGet(this, _APIPromise_client, "f"), data)); | ||
|  |         } | ||
|  |         return this.parsedPromise; | ||
|  |     } | ||
|  |     then(onfulfilled, onrejected) { | ||
|  |         return this.parse().then(onfulfilled, onrejected); | ||
|  |     } | ||
|  |     catch(onrejected) { | ||
|  |         return this.parse().catch(onrejected); | ||
|  |     } | ||
|  |     finally(onfinally) { | ||
|  |         return this.parse().finally(onfinally); | ||
|  |     } | ||
|  | } | ||
|  | _APIPromise_client = new WeakMap(); | ||
|  | //# sourceMappingURL=api-promise.mjs.map
 |