feat(login): campo de dirección del servidor configurable
- config.ts: BASE_URL/ORIGIN dejan de ser constantes; ahora hay setServerOrigin/getBaseUrl/getServerOrigin con normalizeServerUrl (añade http:// si falta, quita barras finales y el sufijo /api/v1). - client.ts: construye las URLs con getBaseUrl() en cada petición. - session: signIn recibe la URL, la fija antes del login y la persiste en SecureStore (clave server_url); se rehidrata al arrancar junto al token. - LoginScreen: campo "Servidor" precargado con el último valor usado. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+37
-10
@@ -1,25 +1,52 @@
|
||||
/**
|
||||
* Configuración de la app.
|
||||
*
|
||||
* BASE_URL: raíz de la API v1. En despliegue real apunta a `https://<host>/api/v1`.
|
||||
* Ojo: desde un dispositivo físico/emulador "localhost" NO es la máquina de desarrollo:
|
||||
* - Emulador Android: usa `http://10.0.2.2/...`
|
||||
* - Dispositivo físico: usa la IP LAN del PC, p.ej. `http://192.168.1.50/...`
|
||||
* Ajusta DEV_HOST a tu entorno o sobreescribe BASE_URL.
|
||||
* La dirección del servidor es CONFIGURABLE en runtime (campo en el login,
|
||||
* persistida por la sesión). DEFAULT_SERVER es solo el valor inicial.
|
||||
* Ojo: desde un dispositivo físico/emulador "localhost" NO es la máquina de
|
||||
* desarrollo:
|
||||
* - Emulador Android: `http://10.0.2.2/...`
|
||||
* - Dispositivo físico: la IP LAN del PC, p.ej. `http://192.168.1.50/...`
|
||||
*/
|
||||
|
||||
const DEV_HOST = 'http://10.0.2.2/construprogress/public';
|
||||
export const DEFAULT_SERVER = 'http://10.0.2.2/construprogress/public';
|
||||
|
||||
export const BASE_URL = `${DEV_HOST}/api/v1`;
|
||||
/** Raíz del host vigente (media relativa, p.ej. `/storage/...`, se resuelve contra ella). */
|
||||
let serverOrigin = DEFAULT_SERVER;
|
||||
|
||||
/** Raíz del host (para resolver URLs relativas de media, p.ej. `/storage/...`). */
|
||||
export const ORIGIN = DEV_HOST;
|
||||
/**
|
||||
* Normaliza lo que teclea el usuario a una raíz de servidor:
|
||||
* añade `http://` si falta el esquema, quita barras finales y el sufijo
|
||||
* `/api/v1` si lo incluyó.
|
||||
*/
|
||||
export function normalizeServerUrl(input: string): string {
|
||||
let url = input.trim();
|
||||
if (!url) return DEFAULT_SERVER;
|
||||
if (!/^https?:\/\//i.test(url)) url = `http://${url}`;
|
||||
url = url.replace(/\/+$/, '');
|
||||
url = url.replace(/\/api\/v1$/i, '');
|
||||
return url;
|
||||
}
|
||||
|
||||
/** Fija la raíz del servidor para todas las peticiones siguientes. */
|
||||
export function setServerOrigin(url: string): void {
|
||||
serverOrigin = normalizeServerUrl(url);
|
||||
}
|
||||
|
||||
export function getServerOrigin(): string {
|
||||
return serverOrigin;
|
||||
}
|
||||
|
||||
/** Raíz de la API v1 vigente. */
|
||||
export function getBaseUrl(): string {
|
||||
return `${serverOrigin}/api/v1`;
|
||||
}
|
||||
|
||||
/** Convierte una url de media (posiblemente relativa) en absoluta. */
|
||||
export function absoluteUrl(url?: string | null): string | undefined {
|
||||
if (!url) return undefined;
|
||||
if (/^https?:\/\//i.test(url)) return url;
|
||||
return `${ORIGIN}${url.startsWith('/') ? '' : '/'}${url}`;
|
||||
return `${serverOrigin}${url.startsWith('/') ? '' : '/'}${url}`;
|
||||
}
|
||||
|
||||
/** Se envía en cada petición como cabecera X-App-Version (ver protocolo §7). */
|
||||
|
||||
Reference in New Issue
Block a user