Merged in development (pull request #13)

Development
This commit is contained in:
Leandro Costa 2023-11-09 03:01:29 +00:00
commit 500cd95309
4 changed files with 829 additions and 550 deletions

1345
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{ {
"name": "lhisp-oauth-client", "name": "lhisp-oauth-client",
"version": "1.0.17", "version": "1.0.21",
"main": "src/index", "main": "src/index",
"types": "src/index.d.ts", "types": "src/index.d.ts",
"repository": "git@bitbucket.org:leandro_costa/lhisp-oauth-client.git", "repository": "git@bitbucket.org:leandro_costa/lhisp-oauth-client.git",
@ -12,14 +12,14 @@
"test:watch": "jest --watchAll" "test:watch": "jest --watchAll"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^29.5.2", "@types/jest": "^29.5.8",
"@types/node": "^20.3.1", "@types/node": "^20.9.0",
"jest": "^29.5.0", "jest": "^29.7.0",
"ts-jest": "^29.1.0", "ts-jest": "^29.1.1",
"typescript": "^5.1.3" "typescript": "^5.2.2"
}, },
"dependencies": { "dependencies": {
"axios": "^1.4.0", "axios": "^1.6.1",
"lhisp-logger": "^1.0.11" "lhisp-logger": "^1.0.14"
} }
} }

View file

@ -9,7 +9,7 @@ export interface LhispOauthClientConstructorParams {
apiUrl: string; apiUrl: string;
clientId: string; clientId: string;
clientSecret: string; clientSecret: string;
certificado?: string; certificado?: string | Buffer;
senhaCertificado?: string; senhaCertificado?: string;
authScope?: string; authScope?: string;
authHeaderName?: string; authHeaderName?: string;
@ -18,6 +18,7 @@ export interface LhispOauthClientConstructorParams {
grantType?: string; grantType?: string;
authContentType?: ContentType; authContentType?: ContentType;
sendAuthCredentialsOnRequestBody?: boolean; sendAuthCredentialsOnRequestBody?: boolean;
forceTokenTypeToCamelCase?: boolean;
debug?: boolean; debug?: boolean;
} }

View file

@ -1,6 +1,6 @@
import qs from "querystring"; import qs from "querystring";
import https from "https"; import https from "https";
import axios, { AxiosRequestConfig } from "axios"; import axios, { AxiosHeaders, AxiosRequestConfig } from "axios";
import { import {
AccessToken, AccessToken,
ContentType, ContentType,
@ -24,7 +24,7 @@ export class LhispOauthClient<iAccessToken extends AccessToken = AccessToken> {
protected certificado?: string | Buffer; protected certificado?: string | Buffer;
protected senhaCertificado?: string; protected senhaCertificado?: string;
protected authScope?: string; protected authScope?: string;
protected headers?: Headers; protected headers?: AxiosHeaders;
protected grantType?: string; protected grantType?: string;
protected agent: https.Agent; protected agent: https.Agent;
protected accessToken?: iAccessToken; protected accessToken?: iAccessToken;
@ -32,6 +32,7 @@ export class LhispOauthClient<iAccessToken extends AccessToken = AccessToken> {
protected tokenCreatedAt = 0; protected tokenCreatedAt = 0;
protected tokenExpiresIn = 0; protected tokenExpiresIn = 0;
protected sendAuthCredentialsOnRequestBody?: boolean; protected sendAuthCredentialsOnRequestBody?: boolean;
protected forceTokenTypeToCamelCase?: boolean;
constructor(params: LhispOauthClientConstructorParams) { constructor(params: LhispOauthClientConstructorParams) {
if (params.certificado) { if (params.certificado) {
@ -47,7 +48,7 @@ export class LhispOauthClient<iAccessToken extends AccessToken = AccessToken> {
} }
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 AxiosHeaders;
this.apiUrl = params.apiUrl; this.apiUrl = params.apiUrl;
this.authUrl = params.authUrl; this.authUrl = params.authUrl;
this.authScope = params.authScope; this.authScope = params.authScope;
@ -131,7 +132,10 @@ export class LhispOauthClient<iAccessToken extends AccessToken = AccessToken> {
} }
getAuthToken() { getAuthToken() {
return `${this.accessToken?.token_type} ${this.accessToken?.access_token}`; const tokenType = this.forceTokenTypeToCamelCase
? `${this.accessToken?.token_type?.[0]?.toUpperCase()}${this.accessToken?.token_type?.substring(1)}`
: this.accessToken?.token_type;
return `${tokenType} ${this.accessToken?.access_token}`;
} }
async executarRequest<ResponseType>({ async executarRequest<ResponseType>({
@ -144,9 +148,10 @@ export class LhispOauthClient<iAccessToken extends AccessToken = AccessToken> {
try { try {
await this.getAccessToken(); await this.getAccessToken();
let headers = { const headers = {
"Content-Type": contentType, "Content-Type": contentType,
[this.tokenHeaderName]: this.getAuthToken(), [this.tokenHeaderName]: this.getAuthToken(),
...(this.headers || {}),
}; };
const response = await axios.request<ResponseType>({ const response = await axios.request<ResponseType>({