@zauru-sdk/services 2.0.218 → 2.0.219

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.
Files changed (2) hide show
  1. package/dist/esm/common.js +15 -10
  2. package/package.json +2 -2
@@ -106,15 +106,20 @@ async function getGraphQLToken(session) {
106
106
  return handlePossibleAxiosErrors(async () => {
107
107
  const token = (await session.get("graphqlToken"));
108
108
  const headers = (await getHeaders(null, session));
109
- const tokenHasExpired = token &&
110
- token.expires &&
111
- new Date(new Date().getTime() - 3 * 60 * 60 * 1000) >=
112
- new Date(token.expires);
113
- //Si no hay token, es la primera vez que se recibe, lo voy a traer de zauru
114
- if (!token || tokenHasExpired) {
115
- tokenHasExpired
116
- ? console.log(chalk.yellow(`=============== ⚠️ EL TOKEN GRAPHQL ESTÁ EXPIRADO ⚠️ ====================`))
117
- : console.log(chalk.yellow(`=============== ⚠️ NO HAY UN TOKEN GRAPHQL GUARDADO ⚠️ ====================`));
109
+ // Check if token is missing or within one day of expiration
110
+ const now = Date.now();
111
+ let tokenHasExpired = true;
112
+ if (token && token.expires) {
113
+ const expiresAt = Date.parse(token.expires);
114
+ if (!isNaN(expiresAt)) {
115
+ const oneDayMs = 24 * 60 * 60 * 1000;
116
+ // Refresh if already expired or expires within the next 24 hours
117
+ tokenHasExpired = expiresAt <= now + oneDayMs;
118
+ }
119
+ }
120
+ // If there's no valid token or it's expiring soon, fetch a new one
121
+ if (tokenHasExpired) {
122
+ console.log(chalk.yellow(`=============== ⚠️ EL TOKEN GRAPHQL ESTÁ EXPIRADO O NO EXISTE ⚠️ ====================`));
118
123
  const responseToken = await httpZauru.get("/apps/graphql.json", {
119
124
  headers,
120
125
  });
@@ -127,7 +132,7 @@ async function getGraphQLToken(session) {
127
132
  console.log(chalk.red(`=============== ❗ NO HAY INFORMACIÓN OBTENIDA DEL REQUEST A ZAURU - GET_TOKEN ❗ ====================`));
128
133
  throw new Error("No viene información en la solicitud de getGraphQLToken a Zauru");
129
134
  }
130
- //Si ya está guardado un token en la sesión y aún no a expirado.
135
+ // Return the existing valid token
131
136
  return token;
132
137
  });
133
138
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zauru-sdk/services",
3
- "version": "2.0.218",
3
+ "version": "2.0.219",
4
4
  "description": "Servicios de consulta a Zauru",
5
5
  "main": "./dist/esm/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -32,5 +32,5 @@
32
32
  "axios": "^1.6.7",
33
33
  "chalk": "5.3.0"
34
34
  },
35
- "gitHead": "06385c386eb34381a85745685b7db78b549fd4e2"
35
+ "gitHead": "c17d4fe2185c5db7a4da797db6fe57b67cce3b29"
36
36
  }