@zauru-sdk/services 2.0.140 → 2.0.142
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
|
|
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:
|
|
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 });
|
|
@@ -2,16 +2,18 @@ import { createSessionStorage } from "@remix-run/node";
|
|
|
2
2
|
import crypto from "crypto";
|
|
3
3
|
import { config } from "@zauru-sdk/config";
|
|
4
4
|
import fetch from "node-fetch";
|
|
5
|
-
|
|
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
|
-
|
|
14
|
-
return response;
|
|
16
|
+
return await fetch(url, config);
|
|
15
17
|
}
|
|
16
18
|
catch (error) {
|
|
17
19
|
if (retries > 0) {
|
|
@@ -20,7 +22,8 @@ export async function fetchWithRetries(url, config = {}, retries = 3, backoff =
|
|
|
20
22
|
return fetchWithRetries(url, config, retries - 1, backoff * 2);
|
|
21
23
|
}
|
|
22
24
|
else {
|
|
23
|
-
|
|
25
|
+
console.error(`=> Node Fetch request falló (${url}), no se pudo recuperar.`, `Error message: ${error instanceof Error ? error.message : String(error)}`);
|
|
26
|
+
return null;
|
|
24
27
|
}
|
|
25
28
|
}
|
|
26
29
|
}
|
|
@@ -29,55 +32,38 @@ export function createUpstashSessionStorage({ cookie }) {
|
|
|
29
32
|
return createSessionStorage({
|
|
30
33
|
cookie,
|
|
31
34
|
async createData(data, expires) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return id;
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
console.error("Error al crear la sesión", error);
|
|
43
|
-
return "";
|
|
44
|
-
}
|
|
35
|
+
const id = crypto.randomUUID();
|
|
36
|
+
await fetchWithRetries(`${redisBaseURL}/set/${id}?EX=${expires ? expiresToSeconds(expires) : 60 * 60 * 8}`, {
|
|
37
|
+
method: "post",
|
|
38
|
+
body: JSON.stringify({ data }),
|
|
39
|
+
headers,
|
|
40
|
+
});
|
|
41
|
+
return id;
|
|
45
42
|
},
|
|
46
43
|
async readData(id) {
|
|
44
|
+
const response = await fetch(`${redisBaseURL}/get/${id}`, {
|
|
45
|
+
headers,
|
|
46
|
+
});
|
|
47
47
|
try {
|
|
48
|
-
const response = await fetchWithRetries(`${config.redisBaseURL}/get/${id}`, {
|
|
49
|
-
headers,
|
|
50
|
-
});
|
|
51
48
|
const { result } = (await response.json());
|
|
52
|
-
return JSON.parse(result)
|
|
49
|
+
return JSON.parse(result).data;
|
|
53
50
|
}
|
|
54
51
|
catch (error) {
|
|
55
|
-
console.error("Error al leer la sesión", error);
|
|
56
52
|
return null;
|
|
57
53
|
}
|
|
58
54
|
},
|
|
59
55
|
async updateData(id, data, expires) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
console.error("Error al actualizar la sesión", error);
|
|
69
|
-
}
|
|
56
|
+
await fetchWithRetries(`${redisBaseURL}/set/${id}?EX=${expires ? expiresToSeconds(expires) : 60 * 60 * 8}`, {
|
|
57
|
+
method: "post",
|
|
58
|
+
body: JSON.stringify({ data }),
|
|
59
|
+
headers,
|
|
60
|
+
});
|
|
70
61
|
},
|
|
71
62
|
async deleteData(id) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
catch (error) {
|
|
79
|
-
console.error("Error al eliminar la sesión", error);
|
|
80
|
-
}
|
|
63
|
+
await fetchWithRetries(`${redisBaseURL}/del/${id}`, {
|
|
64
|
+
method: "post",
|
|
65
|
+
headers,
|
|
66
|
+
});
|
|
81
67
|
},
|
|
82
68
|
});
|
|
83
69
|
}
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export declare
|
|
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("node-fetch").Response | 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.
|
|
3
|
+
"version": "2.0.142",
|
|
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
|
"chalk": "5.3.0",
|
|
33
33
|
"node-fetch": "^3.3.2"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "ba2c550a25f3195e12cd3e317df6d28696a46ff6"
|
|
36
36
|
}
|