Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 54 additions & 13 deletions src/FusionAuthClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,41 @@ import {URLSearchParams} from "url";
export class FusionAuthClient {
public clientBuilder: IRESTClientBuilder = new DefaultRESTClientBuilder();
public credentials: RequestCredentials;
public host: string;
public apiKey?: string | null;
public tenantId?: string;

constructor(config: FusionAuthClientConfig);
constructor(
public apiKey: string,
public host: string,
public tenantId?: string,
) { }
apiKey: string,
host: string,
tenantId?: string,
);

constructor(
apiKeyOrConfig: string | FusionAuthClientConfig,
host?: string,
tenantId?: string,
) {
if (typeof apiKeyOrConfig === 'string') {
this.apiKey = apiKeyOrConfig;
this.host = host;
this.tenantId = tenantId;
} else {
this.apiKey = apiKeyOrConfig.apiKey;
this.host = apiKeyOrConfig.host;
this.tenantId = apiKeyOrConfig.tenantId;
}
}


/**
* Sets the tenant id, that will be included in the X-FusionAuth-TenantId header.
*
* @param {string | null} tenantId The value of the X-FusionAuth-TenantId header.
* @returns {FusionAuthClient}
*/
setTenantId(tenantId: string | null): FusionAuthClient {
setTenantId(tenantId?: string | null): FusionAuthClient {
this.tenantId = tenantId;
return this;
}
Expand Down Expand Up @@ -1494,14 +1515,23 @@ export class FusionAuthClient {
* @param {string} redirect_uri The URI to redirect to upon a successful request.
* @returns {Promise<ClientResponse<AccessToken>>}
*/
exchangeOAuthCodeForAccessToken(code: string, client_id: string, client_secret: string, redirect_uri: string): Promise<ClientResponse<AccessToken>> {
exchangeOAuthCodeForAccessToken(codeOrConfig?: string | ExchangeOAuthCodeForAccessTokenConfig, client_id?: string, client_secret?: string, redirect_uri?: string): Promise<ClientResponse<AccessToken>> {
let body = new URLSearchParams();

body.append('code', code);
body.append('client_id', client_id);
body.append('client_secret', client_secret);
body.append('grant_type', 'authorization_code');
body.append('redirect_uri', redirect_uri);
if (typeof codeOrConfig === 'string') {
body.append('code', codeOrConfig);
body.append('client_id', client_id);
body.append('client_secret', client_secret);
body.append('grant_type', 'authorization_code');
body.append('redirect_uri', redirect_uri);
} else {
body.append('code', codeOrConfig.code);
body.append('client_id', codeOrConfig.client_id);
body.append('client_secret', codeOrConfig.client_secret);
body.append('grant_type', 'authorization_code');
body.append('redirect_uri', codeOrConfig.redirect_uri);
}

return this.startAnonymous<AccessToken, OAuthError>()
.withUri('/oauth2/token')
.withFormData(body)
Expand Down Expand Up @@ -5409,6 +5439,18 @@ export default FusionAuthClient;
*/
export type UUID = string;

export interface FusionAuthClientConfig {
host: string;
apiKey?: string;
tenantId?: string;
}

export interface ExchangeOAuthCodeForAccessTokenConfig {
code: string;
client_id?: string;
client_secret?: string;
redirect_uri: string;
}

/**
* @author Rob Davis
Expand Down Expand Up @@ -11979,5 +12021,4 @@ export interface EventLogResponse {
*/
export interface TenantRegistrationConfiguration {
blockedDomains?: Array<string>;
}

}