diff --git a/src/lhisp-oauth-client.ts b/src/lhisp-oauth-client.ts index 2205c71..cd546d8 100644 --- a/src/lhisp-oauth-client.ts +++ b/src/lhisp-oauth-client.ts @@ -4,9 +4,10 @@ import axios, { AxiosRequestConfig } from 'axios'; import { AccessToken, ContentType, defaultAuthContentType, defaultAuthHeaderName, + defaultGrantType, defaultTokenHeaderName, ExecutarRequestParams, - GrantType, LhispOauthClientConstructorParams, + LhispOauthClientConstructorParams, } from "./lhisp-oauth-client.t"; export default class LhispOauthClient { @@ -21,7 +22,7 @@ export default class LhispOauthClient { protected senhaCertificado?: string; protected authScope?: string; protected headers?: Headers; - protected grantType?: GrantType; + protected grantType?: string; protected agent: https.Agent; protected accessToken?: AccessToken; protected refreshToken?: AccessToken; @@ -46,7 +47,7 @@ export default class LhispOauthClient { this.apiUrl = params.apiUrl; this.authUrl = params.authUrl; this.authScope = params.authScope; - this.grantType = params.grantType; + this.grantType = params.grantType || defaultGrantType; this.authContentType = params.authContentType || defaultAuthContentType; this.clientId = params.clientId; this.clientSecret = params.clientSecret; @@ -60,6 +61,8 @@ export default class LhispOauthClient { } parseData({ data, contentType = ContentType.APPLICATION_JSON }: { data: any, contentType: string }) { + if(!data || Object.keys(data).length===0) return undefined; + switch (contentType) { case ContentType.APPLICATION_JSON: return JSON.stringify(data); case ContentType.APPLICATION_X_WWW_FORM_URLENCODED: return qs.stringify(data); @@ -90,18 +93,18 @@ export default class LhispOauthClient { [this.authHeaderName]: this.getAuthHeaderValue(), 'Content-Type': this.authContentType, }, + data: {}, } + if(this.grantType) authRequestOpt.data.grant_type = this.grantType; + if(this.authScope) authRequestOpt.data.scope = this.authScope; + if(this.sendAuthCredentialsOnRequestBody) { - let data: any = {}; - if(this.grantType) data.grant_type = this.grantType; - if(this.authScope) data.scope = this.authScope; - if(this.clientId) data.client_id = this.clientId; - if(this.clientSecret) data.client_secret = this.clientSecret; - - authRequestOpt.data = this.parseData({ data, contentType: this.authContentType }); + if(this.clientId) authRequestOpt.data.client_id = this.clientId; + if(this.clientSecret) authRequestOpt.data.client_secret = this.clientSecret; } + authRequestOpt.data = this.parseData({ data: authRequestOpt.data, contentType: this.authContentType }); const response = await axios.request(authRequestOpt); return this.buildAccessToken(response.data); }