Ajustando definicao de tipos nas funcoes

This commit is contained in:
Leandro Costa 2023-01-26 22:21:28 -03:00
parent 241726ed2f
commit 05f4e4c1e6

View file

@ -4,6 +4,7 @@ import axios, { AxiosRequestConfig } from 'axios';
import { import {
AccessToken, ContentType, defaultAuthContentType, AccessToken, ContentType, defaultAuthContentType,
defaultAuthHeaderName, defaultAuthHeaderName,
defaultTokenHeaderName,
ExecutarRequestParams, ExecutarRequestParams,
GrantType, LhispOauthClientConstructorParams, GrantType, LhispOauthClientConstructorParams,
} from "./lhisp-oauth-client.t"; } from "./lhisp-oauth-client.t";
@ -14,6 +15,7 @@ export default class LhispOauthClient {
protected clientId: string; protected clientId: string;
protected clientSecret: string; protected clientSecret: string;
protected authHeaderName: string; protected authHeaderName: string;
protected tokenHeaderName: string;
protected authContentType: ContentType; protected authContentType: ContentType;
protected certificado?: string; protected certificado?: string;
protected senhaCertificado?: string; protected senhaCertificado?: string;
@ -49,6 +51,7 @@ export default class LhispOauthClient {
this.clientId = params.clientId; this.clientId = params.clientId;
this.clientSecret = params.clientSecret; this.clientSecret = params.clientSecret;
this.authHeaderName = params.authHeaderName || defaultAuthHeaderName; this.authHeaderName = params.authHeaderName || defaultAuthHeaderName;
this.tokenHeaderName = params.tokenHeaderName || defaultTokenHeaderName;
this.sendAuthCredentialsOnRequestBody = params.sendAuthCredentialsOnRequestBody; this.sendAuthCredentialsOnRequestBody = params.sendAuthCredentialsOnRequestBody;
} }
@ -112,7 +115,7 @@ export default class LhispOauthClient {
return this.accessToken; return this.accessToken;
} }
getRequestAuthToken(){ getAuthToken(){
return `${this.accessToken?.token_type} ${this.accessToken?.access_token}`; return `${this.accessToken?.token_type} ${this.accessToken?.access_token}`;
} }
@ -124,8 +127,8 @@ export default class LhispOauthClient {
let headers = { let headers = {
'Content-Type': contentType, 'Content-Type': contentType,
[this.authRequestHeader]: this.getRequestAuthToken(), [this.tokenHeaderName]: this.getAuthToken(),
...this.headers // ...this.headers
}; };
const response = await axios({ const response = await axios({
@ -140,19 +143,19 @@ export default class LhispOauthClient {
return response.data; return response.data;
} }
async get({ path, contentType = 'application/json', params }) { async get({ path, contentType = ContentType.APPLICATION_JSON, params }: ExecutarRequestParams) {
return this.executarRequest({ method: 'GET', path, contentType, params }); return this.executarRequest({ method: 'GET', path, contentType, params });
} }
async put({ path, data, contentType = 'application/json' }) { async put({ path, data, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
return this.executarRequest({ method: 'PUT', path, data, contentType }); return this.executarRequest({ method: 'PUT', path, data, contentType });
} }
async post({ path, data, contentType = 'application/json' }) { async post({ path, data, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
return this.executarRequest({ method: 'POST', path, data, contentType }); return this.executarRequest({ method: 'POST', path, data, contentType });
} }
async delete({ path, contentType = 'application/json' }) { async delete({ path, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
return this.executarRequest({ method: 'DELETE', path, contentType }); return this.executarRequest({ method: 'DELETE', path, contentType });
} }
} }