feat: adiciona generics para o response type
This commit is contained in:
parent
13b8b2903d
commit
5c2137fb9d
1 changed files with 176 additions and 145 deletions
|
@ -1,8 +1,10 @@
|
||||||
import qs from 'querystring';
|
import qs from "querystring";
|
||||||
import https from 'https';
|
import https from "https";
|
||||||
import axios, { AxiosRequestConfig } from 'axios';
|
import axios, { AxiosRequestConfig } from "axios";
|
||||||
import {
|
import {
|
||||||
AccessToken, ContentType, defaultAuthContentType,
|
AccessToken,
|
||||||
|
ContentType,
|
||||||
|
defaultAuthContentType,
|
||||||
defaultAuthHeaderName,
|
defaultAuthHeaderName,
|
||||||
defaultGrantType,
|
defaultGrantType,
|
||||||
defaultTokenHeaderName,
|
defaultTokenHeaderName,
|
||||||
|
@ -37,11 +39,10 @@ export class LhispOauthClient {
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.agent = new https.Agent({
|
this.agent = new https.Agent({
|
||||||
rejectUnauthorized: false
|
rejectUnauthorized: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
this.certificado = params.certificado;
|
this.certificado = params.certificado;
|
||||||
this.headers = (params.headers ? params.headers : {}) as any as Headers;
|
this.headers = (params.headers ? params.headers : {}) as any as Headers;
|
||||||
this.apiUrl = params.apiUrl;
|
this.apiUrl = params.apiUrl;
|
||||||
|
@ -60,12 +61,14 @@ export class LhispOauthClient {
|
||||||
return `Basic ${Buffer.from(`${this.clientId}:${this.clientSecret}`).toString("base64")}`;
|
return `Basic ${Buffer.from(`${this.clientId}:${this.clientSecret}`).toString("base64")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
parseData({ data, contentType = ContentType.APPLICATION_JSON }: { data: any, contentType: string }) {
|
parseData({ data, contentType = ContentType.APPLICATION_JSON }: { data: any; contentType: string }) {
|
||||||
if (!data || Object.keys(data).length === 0) return undefined;
|
if (!data || Object.keys(data).length === 0) return undefined;
|
||||||
|
|
||||||
switch (contentType) {
|
switch (contentType) {
|
||||||
case ContentType.APPLICATION_JSON: return JSON.stringify(data);
|
case ContentType.APPLICATION_JSON:
|
||||||
case ContentType.APPLICATION_X_WWW_FORM_URLENCODED: return qs.stringify(data);
|
return JSON.stringify(data);
|
||||||
|
case ContentType.APPLICATION_X_WWW_FORM_URLENCODED:
|
||||||
|
return qs.stringify(data);
|
||||||
default:
|
default:
|
||||||
throw new Error(`Content Type Inválido: [${contentType}]`);
|
throw new Error(`Content Type Inválido: [${contentType}]`);
|
||||||
}
|
}
|
||||||
|
@ -86,15 +89,15 @@ export class LhispOauthClient {
|
||||||
// TODO: Implementar Refresh Token.
|
// TODO: Implementar Refresh Token.
|
||||||
|
|
||||||
let authRequestOpt: AxiosRequestConfig = {
|
let authRequestOpt: AxiosRequestConfig = {
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
url: this.authUrl,
|
url: this.authUrl,
|
||||||
httpsAgent: this.agent,
|
httpsAgent: this.agent,
|
||||||
headers: {
|
headers: {
|
||||||
[this.authHeaderName]: this.getAuthHeaderValue(),
|
[this.authHeaderName]: this.getAuthHeaderValue(),
|
||||||
'Content-Type': this.authContentType,
|
"Content-Type": this.authContentType,
|
||||||
},
|
},
|
||||||
data: {},
|
data: {},
|
||||||
}
|
};
|
||||||
|
|
||||||
if (this.grantType) authRequestOpt.data.grant_type = this.grantType;
|
if (this.grantType) authRequestOpt.data.grant_type = this.grantType;
|
||||||
if (this.authScope) authRequestOpt.data.scope = this.authScope;
|
if (this.authScope) authRequestOpt.data.scope = this.authScope;
|
||||||
|
@ -104,15 +107,18 @@ export class LhispOauthClient {
|
||||||
if (this.clientSecret) authRequestOpt.data.client_secret = this.clientSecret;
|
if (this.clientSecret) authRequestOpt.data.client_secret = this.clientSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
authRequestOpt.data = this.parseData({ data: authRequestOpt.data, contentType: this.authContentType });
|
authRequestOpt.data = this.parseData({
|
||||||
|
data: authRequestOpt.data,
|
||||||
|
contentType: this.authContentType,
|
||||||
|
});
|
||||||
const response = await axios.request(authRequestOpt);
|
const response = await axios.request(authRequestOpt);
|
||||||
return this.buildAccessToken(response.data);
|
return this.buildAccessToken(response.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
buildAccessToken(data: Omit<AccessToken,'created_at'>): AccessToken{
|
buildAccessToken(data: Omit<AccessToken, "created_at">): AccessToken {
|
||||||
this.accessToken = {
|
this.accessToken = {
|
||||||
...data,
|
...data,
|
||||||
created_at: Date.now()
|
created_at: Date.now(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.accessToken;
|
return this.accessToken;
|
||||||
|
@ -122,43 +128,68 @@ export class LhispOauthClient {
|
||||||
return `${this.accessToken?.token_type} ${this.accessToken?.access_token}`;
|
return `${this.accessToken?.token_type} ${this.accessToken?.access_token}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async executarRequest({ method, path, data, params, contentType = ContentType.APPLICATION_JSON } : ExecutarRequestParams) {
|
async executarRequest<ResponseType>({
|
||||||
|
method,
|
||||||
|
path,
|
||||||
|
data,
|
||||||
|
params,
|
||||||
|
contentType = ContentType.APPLICATION_JSON,
|
||||||
|
}: ExecutarRequestParams): Promise<ResponseType> {
|
||||||
await this.getAccessToken();
|
await this.getAccessToken();
|
||||||
if (!this.accessToken?.token_type) {
|
if (!this.accessToken?.token_type) {
|
||||||
console.log("## LHOAUTH2 NO TOKEN ?:", this.accessToken);
|
console.log("## LHOAUTH2 NO TOKEN ?:", this.accessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
let headers = {
|
let headers = {
|
||||||
'Content-Type': contentType,
|
"Content-Type": contentType,
|
||||||
[this.tokenHeaderName]: this.getAuthToken(),
|
[this.tokenHeaderName]: this.getAuthToken(),
|
||||||
// ...this.headers
|
// ...this.headers
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await axios.request({
|
const response = await axios.request<ResponseType>({
|
||||||
method,
|
method,
|
||||||
url: `${this.apiUrl}${path}`,
|
url: `${this.apiUrl}${path}`,
|
||||||
httpsAgent: this.agent,
|
httpsAgent: this.agent,
|
||||||
headers,
|
headers,
|
||||||
data,
|
data,
|
||||||
params
|
params,
|
||||||
});
|
});
|
||||||
|
|
||||||
return response.data;
|
return response.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async get({ path, contentType = ContentType.APPLICATION_JSON, params }: ExecutarRequestParams) {
|
async get<ResponseType>({ path, contentType = ContentType.APPLICATION_JSON, params }: ExecutarRequestParams) {
|
||||||
return this.executarRequest({ method: 'GET', path, contentType, params });
|
return this.executarRequest<ResponseType>({
|
||||||
|
method: "GET",
|
||||||
|
path,
|
||||||
|
contentType,
|
||||||
|
params,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async put({ path, data, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
async put<ResponseType>({ path, data, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
||||||
return this.executarRequest({ method: 'PUT', path, data, contentType });
|
return this.executarRequest<ResponseType>({
|
||||||
|
method: "PUT",
|
||||||
|
path,
|
||||||
|
data,
|
||||||
|
contentType,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async post({ path, data, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
async post<ResponseType>({ path, data, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
||||||
return this.executarRequest({ method: 'POST', path, data, contentType });
|
return this.executarRequest<ResponseType>({
|
||||||
|
method: "POST",
|
||||||
|
path,
|
||||||
|
data,
|
||||||
|
contentType,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete({ path, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
async delete<ResponseType>({ path, contentType = ContentType.APPLICATION_JSON }: ExecutarRequestParams) {
|
||||||
return this.executarRequest({ method: 'DELETE', path, contentType });
|
return this.executarRequest<ResponseType>({
|
||||||
|
method: "DELETE",
|
||||||
|
path,
|
||||||
|
contentType,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue