11// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22
33import { type OpenAI } from '../client' ;
4+ import { OpenAIError } from './error' ;
45
56import { type PromiseOrValue } from '../internal/types' ;
67import {
@@ -14,9 +15,12 @@ import {
1415 * A subclass of `Promise` providing additional helper methods
1516 * for interacting with the SDK.
1617 */
18+ // Associate instance with client via a module WeakMap to avoid
19+ // JS private-field brand checks across bundles/copies.
20+ const apiPromiseClient = /* @__PURE__ */ new WeakMap < object , OpenAI > ( ) ;
21+
1722export class APIPromise < T > extends Promise < WithRequestID < T > > {
1823 private parsedPromise : Promise < WithRequestID < T > > | undefined ;
19- #client: OpenAI ;
2024
2125 constructor (
2226 client : OpenAI ,
@@ -32,11 +36,13 @@ export class APIPromise<T> extends Promise<WithRequestID<T>> {
3236 // to parse the response
3337 resolve ( null as any ) ;
3438 } ) ;
35- this . #client = client ;
39+ apiPromiseClient . set ( this , client ) ;
3640 }
3741
3842 _thenUnwrap < U > ( transform : ( data : T , props : APIResponseProps ) => U ) : APIPromise < U > {
39- return new APIPromise ( this . #client, this . responsePromise , async ( client , props ) =>
43+ const client = apiPromiseClient . get ( this ) ;
44+ if ( ! client ) throw new OpenAIError ( 'Illegal invocation of APIPromise method' ) ;
45+ return new APIPromise ( client , this . responsePromise , async ( client , props ) =>
4046 addRequestID ( transform ( await this . parseResponse ( client , props ) , props ) , props . response ) ,
4147 ) ;
4248 }
@@ -75,8 +81,10 @@ export class APIPromise<T> extends Promise<WithRequestID<T>> {
7581
7682 private parse ( ) : Promise < WithRequestID < T > > {
7783 if ( ! this . parsedPromise ) {
84+ const client = apiPromiseClient . get ( this ) ;
85+ if ( ! client ) throw new OpenAIError ( 'Illegal invocation of APIPromise method' ) ;
7886 this . parsedPromise = this . responsePromise . then ( ( data ) =>
79- this . parseResponse ( this . # client, data ) ,
87+ this . parseResponse ( client , data ) ,
8088 ) as any as Promise < WithRequestID < T > > ;
8189 }
8290 return this . parsedPromise ;
0 commit comments