@@ -24,20 +24,40 @@ import {URLSearchParams} from "url";
24
24
export class FusionAuthClient {
25
25
public clientBuilder : IRESTClientBuilder = new DefaultRESTClientBuilder ( ) ;
26
26
public credentials : RequestCredentials ;
27
+ public host : string ;
28
+ public apiKey ?: string | null ;
29
+ public tenantId ?: string ;
27
30
31
+ constructor ( config : FusionAuthClientConfig ) ;
28
32
constructor (
29
- public apiKey : string ,
30
- public host : string ,
31
- public tenantId ?: string ,
32
- ) { }
33
+ apiKey : string ,
34
+ host : string ,
35
+ tenantId ?: string ,
36
+ ) ;
37
+
38
+ constructor (
39
+ apiKeyOrConfig : string | FusionAuthClientConfig ,
40
+ host ?: string ,
41
+ tenantId ?: string ,
42
+ ) {
43
+ if ( typeof apiKeyOrConfig === 'string' ) {
44
+ this . apiKey = apiKeyOrConfig ;
45
+ this . host = host ;
46
+ this . tenantId = tenantId ;
47
+ } else {
48
+ this . apiKey = apiKeyOrConfig . apiKey ;
49
+ this . host = apiKeyOrConfig . host ;
50
+ this . tenantId = apiKeyOrConfig . tenantId ;
51
+ }
52
+ }
33
53
34
54
/**
35
55
* Sets the tenant id, that will be included in the X-FusionAuth-TenantId header.
36
56
*
37
57
* @param {string | null } tenantId The value of the X-FusionAuth-TenantId header.
38
58
* @returns {FusionAuthClient }
39
59
*/
40
- setTenantId ( tenantId : string | null ) : FusionAuthClient {
60
+ setTenantId ( tenantId ? : string | null ) : FusionAuthClient {
41
61
this . tenantId = tenantId ;
42
62
return this ;
43
63
}
@@ -899,6 +919,10 @@ export class FusionAuthClient {
899
919
. go ( ) ;
900
920
}
901
921
922
+ exchangeOAuthCodeForAccessToken ( config : ExchangeOAuthCodeForAccessTokenConfig ) : Promise < ClientResponse < AccessToken > > ;
923
+
924
+ exchangeOAuthCodeForAccessToken ( code : string , client_id : string , client_secret : string , redirect_uri : string ) : Promise < ClientResponse < AccessToken > > ;
925
+
902
926
/**
903
927
* Exchanges an OAuth authorization code for an access token.
904
928
* If you will be using the Authorization Code grant, you will make a request to the Token endpoint to exchange the authorization code returned from the Authorize endpoint for an access token.
@@ -909,14 +933,23 @@ export class FusionAuthClient {
909
933
* @param {string } redirect_uri The URI to redirect to upon a successful request.
910
934
* @returns {Promise<ClientResponse<AccessToken>> }
911
935
*/
912
- exchangeOAuthCodeForAccessToken ( code : string , client_id : string , client_secret : string , redirect_uri : string ) : Promise < ClientResponse < AccessToken > > {
936
+ exchangeOAuthCodeForAccessToken ( codeOrConfig ? : string | ExchangeOAuthCodeForAccessTokenConfig , client_id ? : string , client_secret ? : string , redirect_uri ? : string ) : Promise < ClientResponse < AccessToken > > {
913
937
let body = new URLSearchParams ( ) ;
914
938
915
- body . append ( 'code' , code ) ;
916
- body . append ( 'client_id' , client_id ) ;
917
- body . append ( 'client_secret' , client_secret ) ;
918
- body . append ( 'grant_type' , 'authorization_code' ) ;
919
- body . append ( 'redirect_uri' , redirect_uri ) ;
939
+ if ( typeof codeOrConfig === 'string' ) {
940
+ body . append ( 'code' , codeOrConfig ) ;
941
+ body . append ( 'client_id' , client_id ) ;
942
+ body . append ( 'client_secret' , client_secret ) ;
943
+ body . append ( 'grant_type' , 'authorization_code' ) ;
944
+ body . append ( 'redirect_uri' , redirect_uri ) ;
945
+ } else {
946
+ body . append ( 'code' , codeOrConfig . code ) ;
947
+ body . append ( 'client_id' , codeOrConfig . client_id ) ;
948
+ body . append ( 'client_secret' , codeOrConfig . client_secret ) ;
949
+ body . append ( 'grant_type' , 'authorization_code' ) ;
950
+ body . append ( 'redirect_uri' , codeOrConfig . redirect_uri ) ;
951
+ }
952
+
920
953
return this . startAnonymous < AccessToken , OAuthError > ( )
921
954
. withUri ( '/oauth2/token' )
922
955
. withFormData ( body )
@@ -3459,6 +3492,11 @@ export default FusionAuthClient;
3459
3492
*/
3460
3493
export type UUID = string ;
3461
3494
3495
+ export interface FusionAuthClientConfig {
3496
+ host : string ;
3497
+ apiKey ?: string ;
3498
+ tenantId ?: string ;
3499
+ }
3462
3500
3463
3501
/**
3464
3502
* @author Daniel DeGroff
@@ -4307,6 +4345,13 @@ export enum EventType {
4307
4345
Test = "test"
4308
4346
}
4309
4347
4348
+ export interface ExchangeOAuthCodeForAccessTokenConfig {
4349
+ code : string ;
4350
+ client_id ?: string ;
4351
+ client_secret ?: string ;
4352
+ redirect_uri : string ;
4353
+ }
4354
+
4310
4355
/**
4311
4356
* @author Brian Pontarelli
4312
4357
*/
0 commit comments