Skip to content

Commit e20840a

Browse files
committed
feat: also support object parameter and fix optionals
Both the constructor of FusionAuthClient and its exchangeOAuthCodeForAccessToken now also support calling them with an object instead of separate parameters. This helps when a lot of the parameters are optional (as per the docs). Completely backwards compatible. FusionAuth#29 & FusionAuth#36
1 parent fe47338 commit e20840a

File tree

1 file changed

+56
-11
lines changed

1 file changed

+56
-11
lines changed

src/FusionAuthClient.ts

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,40 @@ import {URLSearchParams} from "url";
2424
export class FusionAuthClient {
2525
public clientBuilder: IRESTClientBuilder = new DefaultRESTClientBuilder();
2626
public credentials: RequestCredentials;
27+
public host: string;
28+
public apiKey?: string | null;
29+
public tenantId?: string;
2730

31+
constructor(config: FusionAuthClientConfig);
2832
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+
}
3353

3454
/**
3555
* Sets the tenant id, that will be included in the X-FusionAuth-TenantId header.
3656
*
3757
* @param {string | null} tenantId The value of the X-FusionAuth-TenantId header.
3858
* @returns {FusionAuthClient}
3959
*/
40-
setTenantId(tenantId: string | null): FusionAuthClient {
60+
setTenantId(tenantId?: string | null): FusionAuthClient {
4161
this.tenantId = tenantId;
4262
return this;
4363
}
@@ -899,6 +919,10 @@ export class FusionAuthClient {
899919
.go();
900920
}
901921

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+
902926
/**
903927
* Exchanges an OAuth authorization code for an access token.
904928
* 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 {
909933
* @param {string} redirect_uri The URI to redirect to upon a successful request.
910934
* @returns {Promise<ClientResponse<AccessToken>>}
911935
*/
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>> {
913937
let body = new URLSearchParams();
914938

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+
920953
return this.startAnonymous<AccessToken, OAuthError>()
921954
.withUri('/oauth2/token')
922955
.withFormData(body)
@@ -3459,6 +3492,11 @@ export default FusionAuthClient;
34593492
*/
34603493
export type UUID = string;
34613494

3495+
export interface FusionAuthClientConfig {
3496+
host: string;
3497+
apiKey?: string;
3498+
tenantId?: string;
3499+
}
34623500

34633501
/**
34643502
* @author Daniel DeGroff
@@ -4307,6 +4345,13 @@ export enum EventType {
43074345
Test = "test"
43084346
}
43094347

4348+
export interface ExchangeOAuthCodeForAccessTokenConfig {
4349+
code: string;
4350+
client_id?: string;
4351+
client_secret?: string;
4352+
redirect_uri: string;
4353+
}
4354+
43104355
/**
43114356
* @author Brian Pontarelli
43124357
*/

0 commit comments

Comments
 (0)