c2-http 1.0.98 → 1.0.100

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.
@@ -1,6 +1,6 @@
1
1
  import { IJWTPayload } from "../../dto/IJWTPayload";
2
2
  declare class GenerateTokenFlow {
3
- execute(jwtPayload: IJWTPayload): string;
3
+ execute(jwtPayload: IJWTPayload, timeToExpireInMinutes?: number): string;
4
4
  }
5
5
  declare const _default: GenerateTokenFlow;
6
6
  export default _default;
@@ -25,13 +25,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  const jwt = __importStar(require("jsonwebtoken"));
27
27
  class GenerateTokenFlow {
28
- execute(jwtPayload) {
28
+ execute(jwtPayload, timeToExpireInMinutes = 1080) {
29
29
  if (!process.env.JWT_SECRET) {
30
30
  throw new Error("key of token not found");
31
31
  }
32
32
  const now = new Date();
33
33
  const exp = new Date(now);
34
- exp.setMinutes(now.getMinutes() + (60 * 18));
34
+ exp.setMinutes(now.getMinutes() + timeToExpireInMinutes);
35
35
  const payload = {
36
36
  iss: process.env.API_NAME,
37
37
  iat: now.getTime(),
@@ -41,10 +41,15 @@ class ValidateTokenFlowItem {
41
41
  }
42
42
  // if the origin api has API_ACCOUNT, use it to validate the token
43
43
  if (process.env.API_ACCOUNT) {
44
- const res = await axios_1.default.get(`${process.env.API_ACCOUNT}/valid-token`, { headers: { Authorization: `Bearer ${token}` } });
45
- if (res.status !== http_status_1.OK)
46
- throw new Error("Invalid token or expired");
47
- return res.data;
44
+ try {
45
+ const res = await axios_1.default.get(`${process.env.API_ACCOUNT}/valid-token`, { headers: { Authorization: `Bearer ${token}` } });
46
+ if (res.status !== http_status_1.OK)
47
+ throw new Error("Invalid token or expired");
48
+ return res.data;
49
+ }
50
+ catch (error) {
51
+ throw new Error(error.data?.message ?? error.message);
52
+ }
48
53
  }
49
54
  // otherwise, construct the URL to validate the token based on the request context
50
55
  const originUrl = `${express_http_context_1.default.get("requestProtocol")}://${express_http_context_1.default.get("requestHost")}${express_http_context_1.default.get("requestOriginalUrl")}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-http",
3
- "version": "1.0.98",
3
+ "version": "1.0.100",
4
4
  "description": "Biblioteca Typescript para API NodeJS",
5
5
  "repository": "https://cabralsilva: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@github.com/cabralsilva/c2-http.git",
6
6
  "author": "Daniel Cabral <cabralconsultoriaemsoftware@gmail.com>",