feat: adicionando log de erros

This commit is contained in:
Leandro Costa 2023-06-15 14:50:48 -03:00
parent c113ee7d99
commit d131f13383
4 changed files with 430 additions and 52 deletions

View file

@ -11,6 +11,7 @@ import {
ExecutarRequestParams,
LhispOauthClientConstructorParams,
} from "./lhisp-oauth-client.t";
import logger from "lhisp-logger";
export class LhispOauthClient {
protected authUrl: string;
@ -82,37 +83,42 @@ export class LhispOauthClient {
}
async getAccessToken(): Promise<AccessToken> {
if (this.accessToken && this.isTokenValid(this.accessToken)) {
return this.accessToken;
try {
if (this.accessToken && this.isTokenValid(this.accessToken)) {
return this.accessToken;
}
// TODO: Implementar Refresh Token.
let authRequestOpt: AxiosRequestConfig = {
method: "POST",
url: this.authUrl,
httpsAgent: this.agent,
headers: {
[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) {
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);
} catch (error) {
logger.error({ message: "LhispOauthClient.getAccessToken", error });
throw error;
}
// TODO: Implementar Refresh Token.
let authRequestOpt: AxiosRequestConfig = {
method: "POST",
url: this.authUrl,
httpsAgent: this.agent,
headers: {
[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) {
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);
}
buildAccessToken(data: Omit<AccessToken, "created_at">): AccessToken {
@ -135,27 +141,32 @@ export class LhispOauthClient {
params,
contentType = ContentType.APPLICATION_JSON,
}: ExecutarRequestParams): Promise<ResponseType> {
await this.getAccessToken();
if (!this.accessToken?.token_type) {
console.log("## LHOAUTH2 NO TOKEN ?:", this.accessToken);
try {
await this.getAccessToken();
if (!this.accessToken?.token_type) {
console.log("## LHOAUTH2 NO TOKEN ?:", this.accessToken);
}
let headers = {
"Content-Type": contentType,
[this.tokenHeaderName]: this.getAuthToken(),
// ...this.headers
};
const response = await axios.request<ResponseType>({
method,
url: `${this.apiUrl}${path}`,
httpsAgent: this.agent,
headers,
data,
params,
});
return response.data;
} catch (error) {
logger.error({ message: "LhispOauthClient.executarRequest", method, path, data, params, contentType, error });
throw error;
}
let headers = {
"Content-Type": contentType,
[this.tokenHeaderName]: this.getAuthToken(),
// ...this.headers
};
const response = await axios.request<ResponseType>({
method,
url: `${this.apiUrl}${path}`,
httpsAgent: this.agent,
headers,
data,
params,
});
return response.data;
}
async get<ResponseType>({ path, contentType = ContentType.APPLICATION_JSON, params }: ExecutarRequestParams) {