lhisp-oauth-client 1.0.5 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lhisp-oauth-client",
3
- "version": "1.0.5",
3
+ "version": "1.0.10",
4
4
  "main": "src/index",
5
5
  "types": "src/index.d.ts",
6
6
  "repository": "git@bitbucket.org:leandro_costa/lhisp-oauth-client.git",
@@ -12,13 +12,14 @@
12
12
  "test:watch": "jest --watchAll"
13
13
  },
14
14
  "devDependencies": {
15
- "@types/jest": "^29.4.0",
16
- "@types/node": "^18.11.18",
17
- "jest": "^29.4.1",
18
- "ts-jest": "^29.0.5",
19
- "typescript": "^4.9.4"
15
+ "@types/jest": "^29.5.2",
16
+ "@types/node": "^20.3.1",
17
+ "jest": "^29.5.0",
18
+ "ts-jest": "^29.1.0",
19
+ "typescript": "^5.1.3"
20
20
  },
21
21
  "dependencies": {
22
- "axios": "^1.2.5"
22
+ "axios": "^1.4.0",
23
+ "lhisp-logger": "^1.0.11"
23
24
  }
24
25
  }
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import https from 'https';
2
+ import https from "https";
3
3
  import { AccessToken, ContentType, ExecutarRequestParams, LhispOauthClientConstructorParams } from "./lhisp-oauth-client.t";
4
4
  export declare class LhispOauthClient {
5
5
  protected authUrl: string;
@@ -26,11 +26,11 @@ export declare class LhispOauthClient {
26
26
  }): string | undefined;
27
27
  isTokenValid(token: AccessToken): boolean;
28
28
  getAccessToken(): Promise<AccessToken>;
29
- buildAccessToken(data: Omit<AccessToken, 'created_at'>): AccessToken;
29
+ buildAccessToken(data: Omit<AccessToken, "created_at">): AccessToken;
30
30
  getAuthToken(): string;
31
- executarRequest({ method, path, data, params, contentType }: ExecutarRequestParams): Promise<any>;
32
- get({ path, contentType, params }: ExecutarRequestParams): Promise<any>;
33
- put({ path, data, contentType }: ExecutarRequestParams): Promise<any>;
34
- post({ path, data, contentType }: ExecutarRequestParams): Promise<any>;
35
- delete({ path, contentType }: ExecutarRequestParams): Promise<any>;
31
+ executarRequest<ResponseType>({ method, path, data, params, contentType, }: ExecutarRequestParams): Promise<ResponseType>;
32
+ get<ResponseType>({ path, contentType, params }: ExecutarRequestParams): Promise<ResponseType>;
33
+ put<ResponseType>({ path, data, contentType }: ExecutarRequestParams): Promise<ResponseType>;
34
+ post<ResponseType>({ path, data, contentType }: ExecutarRequestParams): Promise<ResponseType>;
35
+ delete<ResponseType>({ path, contentType }: ExecutarRequestParams): Promise<ResponseType>;
36
36
  }
@@ -17,6 +17,7 @@ const querystring_1 = __importDefault(require("querystring"));
17
17
  const https_1 = __importDefault(require("https"));
18
18
  const axios_1 = __importDefault(require("axios"));
19
19
  const lhisp_oauth_client_t_1 = require("./lhisp-oauth-client.t");
20
+ const lhisp_logger_1 = __importDefault(require("lhisp-logger"));
20
21
  class LhispOauthClient {
21
22
  constructor(params) {
22
23
  if (params.certificado) {
@@ -28,7 +29,7 @@ class LhispOauthClient {
28
29
  }
29
30
  else {
30
31
  this.agent = new https_1.default.Agent({
31
- rejectUnauthorized: false
32
+ rejectUnauthorized: false,
32
33
  });
33
34
  }
34
35
  this.certificado = params.certificado;
@@ -51,8 +52,10 @@ class LhispOauthClient {
51
52
  if (!data || Object.keys(data).length === 0)
52
53
  return undefined;
53
54
  switch (contentType) {
54
- case lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON: return JSON.stringify(data);
55
- case lhisp_oauth_client_t_1.ContentType.APPLICATION_X_WWW_FORM_URLENCODED: return querystring_1.default.stringify(data);
55
+ case lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON:
56
+ return JSON.stringify(data);
57
+ case lhisp_oauth_client_t_1.ContentType.APPLICATION_X_WWW_FORM_URLENCODED:
58
+ return querystring_1.default.stringify(data);
56
59
  default:
57
60
  throw new Error(`Content Type Inválido: [${contentType}]`);
58
61
  }
@@ -67,33 +70,42 @@ class LhispOauthClient {
67
70
  }
68
71
  getAccessToken() {
69
72
  return __awaiter(this, void 0, void 0, function* () {
70
- if (this.accessToken && this.isTokenValid(this.accessToken)) {
71
- return this.accessToken;
73
+ try {
74
+ if (this.accessToken && this.isTokenValid(this.accessToken)) {
75
+ return this.accessToken;
76
+ }
77
+ // TODO: Implementar Refresh Token.
78
+ let authRequestOpt = {
79
+ method: "POST",
80
+ url: this.authUrl,
81
+ httpsAgent: this.agent,
82
+ headers: {
83
+ [this.authHeaderName]: this.getAuthHeaderValue(),
84
+ "Content-Type": this.authContentType,
85
+ },
86
+ data: {},
87
+ };
88
+ if (this.grantType)
89
+ authRequestOpt.data.grant_type = this.grantType;
90
+ if (this.authScope)
91
+ authRequestOpt.data.scope = this.authScope;
92
+ if (this.sendAuthCredentialsOnRequestBody) {
93
+ if (this.clientId)
94
+ authRequestOpt.data.client_id = this.clientId;
95
+ if (this.clientSecret)
96
+ authRequestOpt.data.client_secret = this.clientSecret;
97
+ }
98
+ authRequestOpt.data = this.parseData({
99
+ data: authRequestOpt.data,
100
+ contentType: this.authContentType,
101
+ });
102
+ const response = yield axios_1.default.request(authRequestOpt);
103
+ return this.buildAccessToken(response.data);
72
104
  }
73
- // TODO: Implementar Refresh Token.
74
- let authRequestOpt = {
75
- method: 'POST',
76
- url: this.authUrl,
77
- httpsAgent: this.agent,
78
- headers: {
79
- [this.authHeaderName]: this.getAuthHeaderValue(),
80
- 'Content-Type': this.authContentType,
81
- },
82
- data: {},
83
- };
84
- if (this.grantType)
85
- authRequestOpt.data.grant_type = this.grantType;
86
- if (this.authScope)
87
- authRequestOpt.data.scope = this.authScope;
88
- if (this.sendAuthCredentialsOnRequestBody) {
89
- if (this.clientId)
90
- authRequestOpt.data.client_id = this.clientId;
91
- if (this.clientSecret)
92
- authRequestOpt.data.client_secret = this.clientSecret;
105
+ catch (error) {
106
+ lhisp_logger_1.default.error({ message: "LhispOauthClient.getAccessToken", error });
107
+ throw error;
93
108
  }
94
- authRequestOpt.data = this.parseData({ data: authRequestOpt.data, contentType: this.authContentType });
95
- const response = yield axios_1.default.request(authRequestOpt);
96
- return this.buildAccessToken(response.data);
97
109
  });
98
110
  }
99
111
  buildAccessToken(data) {
@@ -104,47 +116,72 @@ class LhispOauthClient {
104
116
  var _a, _b;
105
117
  return `${(_a = this.accessToken) === null || _a === void 0 ? void 0 : _a.token_type} ${(_b = this.accessToken) === null || _b === void 0 ? void 0 : _b.access_token}`;
106
118
  }
107
- executarRequest({ method, path, data, params, contentType = lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON }) {
119
+ executarRequest({ method, path, data, params, contentType = lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON, }) {
108
120
  var _a;
109
121
  return __awaiter(this, void 0, void 0, function* () {
110
- yield this.getAccessToken();
111
- if (!((_a = this.accessToken) === null || _a === void 0 ? void 0 : _a.token_type)) {
112
- console.log("## LHOAUTH2 NO TOKEN ?:", this.accessToken);
122
+ try {
123
+ yield this.getAccessToken();
124
+ if (!((_a = this.accessToken) === null || _a === void 0 ? void 0 : _a.token_type)) {
125
+ console.log("## LHOAUTH2 NO TOKEN ?:", this.accessToken);
126
+ }
127
+ let headers = {
128
+ "Content-Type": contentType,
129
+ [this.tokenHeaderName]: this.getAuthToken(),
130
+ // ...this.headers
131
+ };
132
+ const response = yield axios_1.default.request({
133
+ method,
134
+ url: `${this.apiUrl}${path}`,
135
+ httpsAgent: this.agent,
136
+ headers,
137
+ data,
138
+ params,
139
+ });
140
+ return response.data;
141
+ }
142
+ catch (error) {
143
+ lhisp_logger_1.default.error({ message: "LhispOauthClient.executarRequest", method, path, data, params, contentType, error });
144
+ throw error;
113
145
  }
114
- let headers = {
115
- 'Content-Type': contentType,
116
- [this.tokenHeaderName]: this.getAuthToken(),
117
- // ...this.headers
118
- };
119
- const response = yield axios_1.default.request({
120
- method,
121
- url: `${this.apiUrl}${path}`,
122
- httpsAgent: this.agent,
123
- headers,
124
- data,
125
- params
126
- });
127
- return response.data;
128
146
  });
129
147
  }
130
148
  get({ path, contentType = lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON, params }) {
131
149
  return __awaiter(this, void 0, void 0, function* () {
132
- return this.executarRequest({ method: 'GET', path, contentType, params });
150
+ return this.executarRequest({
151
+ method: "GET",
152
+ path,
153
+ contentType,
154
+ params,
155
+ });
133
156
  });
134
157
  }
135
158
  put({ path, data, contentType = lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON }) {
136
159
  return __awaiter(this, void 0, void 0, function* () {
137
- return this.executarRequest({ method: 'PUT', path, data, contentType });
160
+ return this.executarRequest({
161
+ method: "PUT",
162
+ path,
163
+ data,
164
+ contentType,
165
+ });
138
166
  });
139
167
  }
140
168
  post({ path, data, contentType = lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON }) {
141
169
  return __awaiter(this, void 0, void 0, function* () {
142
- return this.executarRequest({ method: 'POST', path, data, contentType });
170
+ return this.executarRequest({
171
+ method: "POST",
172
+ path,
173
+ data,
174
+ contentType,
175
+ });
143
176
  });
144
177
  }
145
178
  delete({ path, contentType = lhisp_oauth_client_t_1.ContentType.APPLICATION_JSON }) {
146
179
  return __awaiter(this, void 0, void 0, function* () {
147
- return this.executarRequest({ method: 'DELETE', path, contentType });
180
+ return this.executarRequest({
181
+ method: "DELETE",
182
+ path,
183
+ contentType,
184
+ });
148
185
  });
149
186
  }
150
187
  }
@@ -5,7 +5,7 @@ var ContentType;
5
5
  (function (ContentType) {
6
6
  ContentType["APPLICATION_JSON"] = "application/json";
7
7
  ContentType["APPLICATION_X_WWW_FORM_URLENCODED"] = "application/x-www-form-urlencoded";
8
- })(ContentType = exports.ContentType || (exports.ContentType = {}));
8
+ })(ContentType || (exports.ContentType = ContentType = {}));
9
9
  exports.defaultGrantType = 'client_credentials';
10
10
  exports.defaultAuthContentType = ContentType.APPLICATION_X_WWW_FORM_URLENCODED;
11
11
  exports.defaultAuthHeaderName = 'Authorization';