1
1
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
3
3
import { type OpenAI } from '../client' ;
4
+ import { OpenAIError } from './error' ;
4
5
5
6
import { type PromiseOrValue } from '../internal/types' ;
6
7
import {
@@ -14,9 +15,12 @@ import {
14
15
* A subclass of `Promise` providing additional helper methods
15
16
* for interacting with the SDK.
16
17
*/
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
+
17
22
export class APIPromise < T > extends Promise < WithRequestID < T > > {
18
23
private parsedPromise : Promise < WithRequestID < T > > | undefined ;
19
- #client: OpenAI ;
20
24
21
25
constructor (
22
26
client : OpenAI ,
@@ -32,11 +36,13 @@ export class APIPromise<T> extends Promise<WithRequestID<T>> {
32
36
// to parse the response
33
37
resolve ( null as any ) ;
34
38
} ) ;
35
- this . #client = client ;
39
+ apiPromiseClient . set ( this , client ) ;
36
40
}
37
41
38
42
_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 ) =>
40
46
addRequestID ( transform ( await this . parseResponse ( client , props ) , props ) , props . response ) ,
41
47
) ;
42
48
}
@@ -75,8 +81,10 @@ export class APIPromise<T> extends Promise<WithRequestID<T>> {
75
81
76
82
private parse ( ) : Promise < WithRequestID < T > > {
77
83
if ( ! this . parsedPromise ) {
84
+ const client = apiPromiseClient . get ( this ) ;
85
+ if ( ! client ) throw new OpenAIError ( 'Illegal invocation of APIPromise method' ) ;
78
86
this . parsedPromise = this . responsePromise . then ( ( data ) =>
79
- this . parseResponse ( this . # client, data ) ,
87
+ this . parseResponse ( client , data ) ,
80
88
) as any as Promise < WithRequestID < T > > ;
81
89
}
82
90
return this . parsedPromise ;
0 commit comments