@@ -3,38 +3,50 @@ import type { WebsocketsSocket } from "../api/resources/websockets/client/Socket
33import * as core from "../core/index.js" ;
44import * as environments from "../environments.js" ;
55
6- export type GetPaymentCredentials = ( wsUrl : string ) => Promise < Record < string , string > > ;
6+ export type GetPaymentCredentials = (
7+ wsUrl : string
8+ ) => Promise < Record < string , string > > ;
79
810export class WebsocketsClient extends FernWebsocketsClient {
9- private readonly _getPaymentCredentials : GetPaymentCredentials | undefined ;
11+ private readonly _getPaymentCredentials : GetPaymentCredentials | undefined ;
1012
11- constructor ( options : FernWebsocketsClient . Options , getPaymentCredentials ?: GetPaymentCredentials ) {
12- super ( options ) ;
13- this . _getPaymentCredentials = getPaymentCredentials ;
14- }
15-
16- public override async connect ( args : FernWebsocketsClient . ConnectArgs = { } ) : Promise < WebsocketsSocket > {
17- let connectArgs = args ;
13+ constructor (
14+ options : FernWebsocketsClient . Options ,
15+ getPaymentCredentials ?: GetPaymentCredentials
16+ ) {
17+ super ( options ) ;
18+ this . _getPaymentCredentials = getPaymentCredentials ;
19+ }
1820
19- if ( this . _getPaymentCredentials ) {
20- const wsUrl = core . url . join (
21- ( await core . Supplier . get ( this . _options . baseUrl ) ) ??
22- ( ( await core . Supplier . get ( this . _options . environment ) ) ?? environments . AgentMailEnvironment . Prod )
23- . websockets ,
24- "/v0" ,
25- ) ;
26- const credentials = await this . _getPaymentCredentials ( wsUrl ) ;
27- connectArgs = {
28- ...args ,
29- queryParams : { ...credentials , ...args . queryParams } ,
30- } ;
31- } else if ( ! args . apiKey ) {
32- const apiKey = ( await core . Supplier . get ( this . _options . apiKey ) ) ?? process . env . AGENTMAIL_API_KEY ;
33- connectArgs = { ...args , apiKey } ;
34- }
21+ public override async connect (
22+ args : FernWebsocketsClient . ConnectArgs & { waitForOpen ?: boolean } = { }
23+ ) : Promise < WebsocketsSocket > {
24+ const { waitForOpen = true , ...rest } = args ;
25+ let connectArgs = rest ;
3526
36- const socket = await super . connect ( connectArgs ) ;
37- await socket . waitForOpen ( ) ;
38- return socket ;
27+ if ( this . _getPaymentCredentials ) {
28+ const wsUrl = core . url . join (
29+ ( await core . Supplier . get ( this . _options . baseUrl ) ) ??
30+ (
31+ ( await core . Supplier . get ( this . _options . environment ) ) ??
32+ environments . AgentMailEnvironment . Prod
33+ ) . websockets ,
34+ "/v0"
35+ ) ;
36+ const credentials = await this . _getPaymentCredentials ( wsUrl ) ;
37+ connectArgs = {
38+ ...rest ,
39+ queryParams : { ...credentials , ...rest . queryParams } ,
40+ } ;
41+ } else if ( ! rest . apiKey ) {
42+ const apiKey =
43+ ( await core . Supplier . get ( this . _options . apiKey ) ) ??
44+ process . env . AGENTMAIL_API_KEY ;
45+ connectArgs = { ...rest , apiKey } ;
3946 }
47+
48+ const socket = await super . connect ( connectArgs ) ;
49+ if ( waitForOpen ) await socket . waitForOpen ( ) ;
50+ return socket ;
51+ }
4052}
0 commit comments