@zauru-sdk/services 2.0.139 → 2.0.141

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,11 +1,12 @@
1
1
  import { createCookie } from "@remix-run/node";
2
- import { createUpstashSessionStorage, MAX_AGE_SESSION_COOKIE, } from "./upstash.js";
2
+ import { createUpstashSessionStorage } from "./upstash.js";
3
3
  const sessionCookie = createCookie("_rj_session", {
4
4
  secrets: ["r3m1xr0ck1"],
5
5
  path: "/",
6
6
  sameSite: process.env.NODE_ENV === "production" ? "none" : "lax",
7
7
  httpOnly: true,
8
- maxAge: MAX_AGE_SESSION_COOKIE,
8
+ maxAge: 60 * 60 * 24,
9
+ //expires,
9
10
  secure: process.env.NODE_ENV === "production",
10
11
  });
11
12
  const { getSession, commitSession, destroySession } = createUpstashSessionStorage({ cookie: sessionCookie });
@@ -1,26 +1,32 @@
1
1
  import { createSessionStorage } from "@remix-run/node";
2
2
  import crypto from "crypto";
3
3
  import { config } from "@zauru-sdk/config";
4
- import fetch from "node-fetch";
5
- export const MAX_AGE_SESSION_COOKIE = 60 * 60 * 16; //16 hours
4
+ import axios from "axios";
5
+ const redisBaseURL = config.redisBaseURL;
6
6
  const headers = {
7
7
  Authorization: `Bearer ${config.redisToken}`,
8
8
  Accept: "application/json",
9
9
  "Content-Type": "application/json",
10
10
  };
11
+ const expiresToSeconds = (expires) => {
12
+ return Math.floor((expires.getTime() - new Date().getTime()) / 1000);
13
+ };
11
14
  export async function fetchWithRetries(url, config = {}, retries = 3, backoff = 200) {
12
15
  try {
13
- const response = await fetch(url, config);
14
- return response;
16
+ return await axios.request({
17
+ url,
18
+ ...config,
19
+ });
15
20
  }
16
21
  catch (error) {
17
22
  if (retries > 0) {
18
- console.warn(`=> Node Fetch request falló (${url}), reintentando en ${backoff}ms... (${retries} intentos restantes)`, `Error message: ${error instanceof Error ? error.message : String(error)}`);
23
+ console.warn(`=> Axios request falló (${url}), reintentando en ${backoff}ms... (${retries} intentos restantes)`, `Error message: ${error instanceof Error ? error.message : String(error)}`);
19
24
  await new Promise((resolve) => setTimeout(resolve, backoff));
20
25
  return fetchWithRetries(url, config, retries - 1, backoff * 2);
21
26
  }
22
27
  else {
23
- throw error;
28
+ console.error(`=> Axios request falló (${url}), no se pudo recuperar.`, `Error message: ${error instanceof Error ? error.message : String(error)}`);
29
+ return null;
24
30
  }
25
31
  }
26
32
  }
@@ -29,55 +35,39 @@ export function createUpstashSessionStorage({ cookie }) {
29
35
  return createSessionStorage({
30
36
  cookie,
31
37
  async createData(data, expires) {
32
- try {
33
- const id = crypto.randomUUID();
34
- await fetchWithRetries(`${config.redisBaseURL}/set/${id}?EX=${MAX_AGE_SESSION_COOKIE}`, {
35
- method: "post",
36
- body: JSON.stringify({ data }),
37
- headers,
38
- });
39
- return id;
40
- }
41
- catch (error) {
42
- console.error("Error al crear la sesión", error);
43
- return "";
44
- }
38
+ const id = `${data?.selectedEntity}-${data?.employee_id}-${crypto.randomUUID()}`;
39
+ await fetchWithRetries(`${redisBaseURL}/set/${id}?EX=${expires ? expiresToSeconds(expires) : 60 * 60 * 8}`, {
40
+ method: "post",
41
+ data: { data },
42
+ headers,
43
+ });
44
+ return id;
45
45
  },
46
46
  async readData(id) {
47
+ const response = await fetchWithRetries(`${redisBaseURL}/get/${id}`, {
48
+ headers,
49
+ });
47
50
  try {
48
- const response = await fetchWithRetries(`${config.redisBaseURL}/get/${id}`, {
49
- headers,
50
- });
51
- const { result } = (await response.json());
52
- return JSON.parse(result).data;
51
+ const { result } = response?.data;
52
+ return JSON.parse(result)?.data;
53
53
  }
54
54
  catch (error) {
55
- console.error("Error al leer la sesión", error);
56
- return null;
55
+ console.error("Error al leer la sesión: ", error);
56
+ return {};
57
57
  }
58
58
  },
59
59
  async updateData(id, data, expires) {
60
- try {
61
- await fetchWithRetries(`${config.redisBaseURL}/set/${id}?EX=${MAX_AGE_SESSION_COOKIE}`, {
62
- method: "post",
63
- body: JSON.stringify({ data }),
64
- headers,
65
- });
66
- }
67
- catch (error) {
68
- console.error("Error al actualizar la sesión", error);
69
- }
60
+ await fetchWithRetries(`${redisBaseURL}/set/${id}?EX=${expires ? expiresToSeconds(expires) : 60 * 60 * 8}`, {
61
+ method: "post",
62
+ data: { data },
63
+ headers,
64
+ });
70
65
  },
71
66
  async deleteData(id) {
72
- try {
73
- await fetchWithRetries(`${config.redisBaseURL}/del/${id}`, {
74
- method: "post",
75
- headers,
76
- });
77
- }
78
- catch (error) {
79
- console.error("Error al eliminar la sesión", error);
80
- }
67
+ await fetchWithRetries(`${redisBaseURL}/del/${id}`, {
68
+ method: "post",
69
+ headers,
70
+ });
81
71
  },
82
72
  });
83
73
  }
@@ -1,3 +1,2 @@
1
- export declare const MAX_AGE_SESSION_COOKIE: number;
2
- export declare function fetchWithRetries(url: string, config?: {}, retries?: number, backoff?: number): Promise<import("node-fetch").Response>;
1
+ export declare function fetchWithRetries(url: string, config?: {}, retries?: number, backoff?: number): Promise<import("axios").AxiosResponse<any, any> | null>;
3
2
  export declare function createUpstashSessionStorage({ cookie }: any): import("@remix-run/node").SessionStorage<import("@remix-run/node").SessionData, import("@remix-run/node").SessionData>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zauru-sdk/services",
3
- "version": "2.0.139",
3
+ "version": "2.0.141",
4
4
  "description": "Servicios de consulta a Zauru",
5
5
  "main": "./dist/esm/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -29,8 +29,7 @@
29
29
  "@zauru-sdk/graphql": "^2.0.119",
30
30
  "@zauru-sdk/types": "^2.0.109",
31
31
  "axios": "^1.6.7",
32
- "chalk": "5.3.0",
33
- "node-fetch": "^3.3.2"
32
+ "chalk": "5.3.0"
34
33
  },
35
- "gitHead": "551544150cd689596a8db6d7dbbe66c6fa2ca7c3"
34
+ "gitHead": "7aa8d99feb6bfbae738c017e1d748b444cdbbe00"
36
35
  }