Sesiones correctas
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
/**
|
||||
* Cliente HTTP centralizado para frontend-client.
|
||||
* Usa cookies HttpOnly (client_access_token) como fuente primaria de auth,
|
||||
* con Bearer token como complemento cuando está disponible en memoria.
|
||||
*/
|
||||
import { auth } from '$lib/stores/auth';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
const API_BASE = '/api/v1';
|
||||
const TENANT_SLUG = 'aduanasoft';
|
||||
|
||||
interface RequestOptions extends RequestInit {
|
||||
params?: Record<string, string>;
|
||||
@@ -20,7 +16,6 @@ async function request<T>(endpoint: string, options: RequestOptions = {}): Promi
|
||||
const filteredParams = Object.entries(params)
|
||||
.filter(([, value]) => value !== undefined && value !== null && value !== '')
|
||||
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
|
||||
|
||||
if (Object.keys(filteredParams).length > 0) {
|
||||
url += `?${new URLSearchParams(filteredParams).toString()}`;
|
||||
}
|
||||
@@ -29,7 +24,6 @@ async function request<T>(endpoint: string, options: RequestOptions = {}): Promi
|
||||
const authState = get(auth);
|
||||
const headers = new Headers(init.headers);
|
||||
|
||||
// Bearer header cuando el token está en memoria (sesión activa sin reload)
|
||||
if (authState.token) {
|
||||
headers.set('Authorization', `Bearer ${authState.token}`);
|
||||
}
|
||||
@@ -39,8 +33,8 @@ async function request<T>(endpoint: string, options: RequestOptions = {}): Promi
|
||||
if (!headers.has('Content-Type')) {
|
||||
headers.set('Content-Type', 'application/json');
|
||||
}
|
||||
// Identifica este frontend para que el backend use client_access_token
|
||||
headers.set('X-App', 'client');
|
||||
headers.set('X-Tenant-Slug', TENANT_SLUG);
|
||||
|
||||
const response = await fetch(url, {
|
||||
...init,
|
||||
@@ -78,6 +72,7 @@ async function downloadFile(endpoint: string, filename: string): Promise<void> {
|
||||
headers.set('X-Tenant-ID', authState.user.tenant_id);
|
||||
}
|
||||
headers.set('X-App', 'client');
|
||||
headers.set('X-Tenant-Slug', TENANT_SLUG);
|
||||
|
||||
const response = await fetch(`${API_BASE}${endpoint}`, {
|
||||
method: 'GET',
|
||||
@@ -108,19 +103,14 @@ async function downloadFile(endpoint: string, filename: string): Promise<void> {
|
||||
export const api = {
|
||||
get: <T>(endpoint: string, params?: Record<string, string>) =>
|
||||
request<T>(endpoint, { method: 'GET', params }),
|
||||
|
||||
post: <T>(endpoint: string, body?: any) =>
|
||||
request<T>(endpoint, { method: 'POST', body: body !== undefined ? JSON.stringify(body) : undefined }),
|
||||
|
||||
put: <T>(endpoint: string, body?: any) =>
|
||||
request<T>(endpoint, { method: 'PUT', body: body !== undefined ? JSON.stringify(body) : undefined }),
|
||||
|
||||
patch: <T>(endpoint: string, body?: any) =>
|
||||
request<T>(endpoint, { method: 'PATCH', body: body !== undefined ? JSON.stringify(body) : undefined }),
|
||||
|
||||
delete: <T>(endpoint: string) =>
|
||||
request<T>(endpoint, { method: 'DELETE' }),
|
||||
|
||||
downloadFile: (endpoint: string, filename: string) =>
|
||||
downloadFile(endpoint, filename)
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user