@@ -110,24 +110,30 @@ export class OidcClient {
110
110
}
111
111
112
112
type OmittedProps = 'accessToken' | 'nextToken'
113
- type ExtractOverload < T > = T extends {
114
- ( this : void , ...args : infer P1 ) : infer R1
115
- ( this : void , ...args : infer P2 ) : infer R2
116
- ( this : void , ...args : infer P3 ) : infer R3
113
+ type ExtractOverload < T , U > = T extends {
114
+ ( ...args : infer P1 ) : infer R1
115
+ ( ...args : infer P2 ) : infer R2
116
+ ( ...args : infer P3 ) : infer R3
117
117
}
118
- ? ( ...args : P1 ) => R1
118
+ ? ( this : U , ...args : P1 ) => R1
119
119
: never
120
120
121
+ // Removes all methods that use callbacks instead of promises
122
+ type PromisifyClient < T > = {
123
+ [ P in keyof T ] : T [ P ] extends ( ...args : any [ ] ) => any ? ExtractOverload < T [ P ] , PromisifyClient < T > > : T [ P ]
124
+ }
125
+
121
126
export class SsoClient {
122
- public constructor ( private readonly client : SSO , private readonly provider : SsoAccessTokenProvider ) { }
127
+ public constructor (
128
+ private readonly client : PromisifyClient < SSO > ,
129
+ private readonly provider : SsoAccessTokenProvider
130
+ ) { }
123
131
124
132
public listAccounts (
125
133
request : Omit < ListAccountsRequest , OmittedProps > = { }
126
134
) : AsyncCollection < RequiredProps < AccountInfo , 'accountId' > [ ] > {
127
- const method = this . client . listAccounts . bind ( this . client )
128
135
const requester = ( request : Omit < ListAccountsRequest , 'accessToken' > ) =>
129
- this . call ( method as ExtractOverload < typeof method > , request )
130
-
136
+ this . call ( this . client . listAccounts , request )
131
137
const collection = pageableToCollection ( requester , request , 'nextToken' , 'accountList' )
132
138
133
139
return collection . filter ( isNonNullable ) . map ( accounts => accounts . map ( a => ( assertHasProps ( a , 'accountId' ) , a ) ) )
@@ -136,10 +142,8 @@ export class SsoClient {
136
142
public listAccountRoles (
137
143
request : Omit < ListAccountRolesRequest , OmittedProps >
138
144
) : AsyncCollection < Required < RoleInfo > [ ] > {
139
- const method = this . client . listAccountRoles . bind ( this . client )
140
145
const requester = ( request : Omit < ListAccountRolesRequest , 'accessToken' > ) =>
141
- this . call ( method as ExtractOverload < typeof method > , request )
142
-
146
+ this . call ( this . client . listAccountRoles , request )
143
147
const collection = pageableToCollection ( requester , request , 'nextToken' , 'roleList' )
144
148
145
149
return collection
@@ -148,8 +152,7 @@ export class SsoClient {
148
152
}
149
153
150
154
public async getRoleCredentials ( request : Omit < GetRoleCredentialsRequest , OmittedProps > ) {
151
- const method = this . client . getRoleCredentials . bind ( this . client )
152
- const response = await this . call ( method as ExtractOverload < typeof method > , request )
155
+ const response = await this . call ( this . client . getRoleCredentials , request )
153
156
154
157
assertHasProps ( response , 'roleCredentials' )
155
158
assertHasProps ( response . roleCredentials , 'accessKeyId' , 'secretAccessKey' )
@@ -163,20 +166,19 @@ export class SsoClient {
163
166
}
164
167
165
168
public async logout ( request : Omit < LogoutRequest , OmittedProps > = { } ) {
166
- const method = this . client . logout . bind ( this . client )
167
- await this . call ( method as ExtractOverload < typeof method > , request )
169
+ await this . call ( this . client . logout , request )
168
170
}
169
171
170
172
private call < T extends { accessToken : string | undefined } , U > (
171
- method : ( request : T ) => Promise < U > ,
173
+ method : ( this : typeof this . client , request : T ) => Promise < U > ,
172
174
request : Omit < T , 'accessToken' >
173
175
) : Promise < U > {
174
176
const requester = async ( req : T ) => {
175
177
const token = await this . provider . getToken ( )
176
178
assertHasProps ( token , 'accessToken' )
177
179
178
180
try {
179
- return await method ( { ...req , accessToken : token . accessToken } )
181
+ return await method . call ( this . client , { ...req , accessToken : token . accessToken } )
180
182
} catch ( error ) {
181
183
await this . handleError ( error )
182
184
throw error
0 commit comments