From ac05ae423e87280b726c3c16f589c0fbc10d7e0e Mon Sep 17 00:00:00 2001 From: Juan Diego Moreno Upegui Date: Wed, 15 Oct 2025 14:47:04 -0500 Subject: [PATCH] Added restore session method to recreate the session without needing to create a new one --- src/client.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 6a17418..5a9eb94 100644 --- a/src/client.ts +++ b/src/client.ts @@ -502,6 +502,49 @@ export class TrytonClient { }; } + /** + * Restore session using saved session token + * @param sessionToken - Previously saved session string (format: "username:userId:sessionKey") + * @returns Promise - True if session is valid + */ + async restoreSession(sessionToken: string): Promise { + try { + // Parsear el token guardado + const parts = sessionToken.split(":"); + if (parts.length < 3) { + throw new Error("Invalid session token format"); + } + + const [username, userId, ...sessionKeyParts] = parts; + const sessionKey = sessionKeyParts.join(":"); // Por si el sessionKey tiene ":" + + // Establecer la sesión sin hacer login + this.session = sessionToken; + + // Verificar que la sesión sigue siendo válida + const proxy = this.connection?.getConnection(); + if (!proxy) { + throw new Error("No connection available"); + } + + // Test de validación de sesión + try { + await proxy.request("common.version", []); + console.log("✅ Sesión restaurada exitosamente"); + return true; + } catch (error) { + // Sesión inválida o expirada + this.session = null; + console.log("❌ Sesión inválida o expirada"); + return false; + } + } catch (error) { + console.error("❌ Error restaurando sesión:", error); + this.session = null; + return false; + } + } + /** * Type-safe model operations factory * Creates a typed interface for specific models diff --git a/tsconfig.json b/tsconfig.json index 7bfe999..c497530 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "ES2020", "module": "commonjs", - "lib": ["ES2020"], + "lib": ["ES2020", "esnext", "dom"], "declaration": true, "declarationMap": true, "sourceMap": true,