@@ -51,14 +51,15 @@ export interface ClientConfig {
51
51
const toBase64 = ( str : string ) : string => Buffer . from ( str ) . toString ( 'base64' ) ;
52
52
53
53
export class RestService {
54
+ private static instance : RestService ;
54
55
private client : AxiosInstance ;
55
56
private baseURLs : string [ ] ;
56
57
private oauthClient ?: OAuthClient ;
57
58
private oauthBearer : boolean = false ;
58
59
59
60
constructor ( baseURLs : string [ ] , isForward ?: boolean , axiosDefaults ?: CreateAxiosDefaults ,
60
- basicAuthCredentials ?: BasicAuthCredentials , bearerAuthCredentials ?: BearerAuthCredentials ,
61
- maxRetries ?: number , retriesWaitMs ?: number , retriesMaxWaitMs ?: number ) {
61
+ basicAuthCredentials ?: BasicAuthCredentials , bearerAuthCredentials ?: BearerAuthCredentials ,
62
+ maxRetries ?: number , retriesWaitMs ?: number , retriesMaxWaitMs ?: number ) {
62
63
this . client = axios . create ( axiosDefaults ) ;
63
64
axiosRetry ( this . client , {
64
65
retries : maxRetries ?? 2 ,
@@ -80,6 +81,16 @@ export class RestService {
80
81
this . handleBearerAuth ( maxRetries ?? 2 , retriesWaitMs ?? 1000 , retriesMaxWaitMs ?? 20000 , bearerAuthCredentials ) ;
81
82
}
82
83
84
+ static getInstance ( baseURLs : string [ ] , isForward ?: boolean , axiosDefaults ?: CreateAxiosDefaults ,
85
+ basicAuthCredentials ?: BasicAuthCredentials , bearerAuthCredentials ?: BearerAuthCredentials ,
86
+ maxRetries ?: number , retriesWaitMs ?: number , retriesMaxWaitMs ?: number ) : RestService {
87
+ if ( ! this . instance ) {
88
+ this . instance = new RestService ( baseURLs , isForward , axiosDefaults , basicAuthCredentials , bearerAuthCredentials ,
89
+ maxRetries , retriesWaitMs , retriesMaxWaitMs ) ;
90
+ }
91
+ return this . instance ;
92
+ }
93
+
83
94
handleBasicAuth ( basicAuthCredentials ?: BasicAuthCredentials ) : void {
84
95
if ( basicAuthCredentials ) {
85
96
switch ( basicAuthCredentials . credentialsSource ) {
@@ -111,7 +122,7 @@ export class RestService {
111
122
}
112
123
}
113
124
114
- handleBearerAuth ( maxRetries : number ,
125
+ handleBearerAuth ( maxRetries : number ,
115
126
retriesWaitMs : number , retriesMaxWaitMs : number , bearerAuthCredentials ?: BearerAuthCredentials ) : void {
116
127
if ( bearerAuthCredentials ) {
117
128
delete this . client . defaults . auth ;
@@ -150,7 +161,7 @@ export class RestService {
150
161
}
151
162
const issuerEndPointUrl = new URL ( bearerAuthCredentials . issuerEndpointUrl ! ) ;
152
163
this . oauthClient = new OAuthClient ( bearerAuthCredentials . clientId ! , bearerAuthCredentials . clientSecret ! ,
153
- issuerEndPointUrl . origin , issuerEndPointUrl . pathname , bearerAuthCredentials . scope ! ,
164
+ issuerEndPointUrl . origin , issuerEndPointUrl . pathname , bearerAuthCredentials . scope ! ,
154
165
maxRetries , retriesWaitMs , retriesMaxWaitMs ) ;
155
166
break ;
156
167
default :
0 commit comments