11/* eslint-disable @typescript-eslint/no-explicit-any */
22
3- function normalizeBaseUrl ( value : string | undefined ) : string {
4- return typeof value === "string" ? value . trim ( ) . replace ( / \/ + $ / , "" ) : "" ;
5- }
3+ function getBaseUrl ( ) : string {
4+ const base = import . meta. env . VITE_API_BASE_URL ;
65
7- function getApiBaseUrl ( ) : string {
8- const configuredBaseUrl = normalizeBaseUrl ( import . meta. env . VITE_API_BASE_URL ) ;
9- if ( configuredBaseUrl ) {
10- console . log ( "Usando VITE_API_BASE_URL:" , configuredBaseUrl ) ;
11- return configuredBaseUrl ;
6+ if ( base && base . trim ( ) . length > 0 ) {
7+ return base . replace ( / \/ + $ / , "" ) ;
128 }
13- console . log ( " Sem VITE_API_BASE_URL, usando caminho relativo" ) ;
9+
1410 return "" ;
1511}
1612
17- function buildApiUrl ( path : string ) : string {
13+ function buildUrl ( path : string ) : string {
1814 const normalizedPath = path . startsWith ( "/" ) ? path : `/${ path } ` ;
19- const baseUrl = getApiBaseUrl ( ) ;
20- const fullUrl = baseUrl ? `${ baseUrl } ${ normalizedPath } ` : normalizedPath ;
21- console . log ( " Construindo URL:" , { path, baseUrl, fullUrl } ) ;
22- return fullUrl ;
15+ const base = getBaseUrl ( ) ;
16+
17+ return base ? `${ base } ${ normalizedPath } ` : normalizedPath ;
2318}
2419
25- async function readPayload ( response : Response ) : Promise < Record < string , unknown > > {
26- const contentType = response . headers ?. get ?.( "content-type" ) ?? "" ;
27- console . log ( " Content-Type:" , contentType ) ;
28-
29- if ( ! contentType || contentType . includes ( "application/json" ) ) {
30- try {
31- const payload = await response . json ( ) ;
32- console . log ( " Payload JSON:" , payload ) ;
33- return payload && typeof payload === "object"
34- ? ( payload as Record < string , unknown > )
35- : { } ;
36- } catch ( error ) {
37- console . log ( " Erro ao parsear JSON:" , error ) ;
38- return { } ;
20+ async function parseResponse ( response : Response ) {
21+ const contentType = response . headers . get ( "content-type" ) ?? "" ;
22+
23+ try {
24+ if ( contentType . includes ( "application/json" ) ) {
25+ return await response . json ( ) ;
3926 }
40- }
41-
42- if ( typeof response . text === "function" ) {
27+
4328 const text = await response . text ( ) ;
44- console . log ( "Resposta texto:" , text ) ;
4529 return text ? { message : text } : { } ;
30+ } catch {
31+ return { } ;
4632 }
47-
48- return { } ;
4933}
5034
51- function buildError ( message : unknown , fallback : string ) : Error {
52- const errorMsg = typeof message === "string" && message ? message : fallback ;
53- console . error ( "Erro construído:" , errorMsg ) ;
54- return new Error ( errorMsg ) ;
35+ function extractMessage ( payload : any ) : string | undefined {
36+ return payload ?. message && typeof payload . message === "string"
37+ ? payload . message
38+ : undefined ;
5539}
5640
57- function readMessage ( payload : unknown ) : string | undefined {
58- if ( payload && typeof payload === "object" && "message" in payload ) {
59- const message = ( payload as { message ?: unknown } ) . message ;
60- if ( typeof message === "string" ) return message ;
61- }
62- return undefined ;
41+ function createError ( payload : any , fallback : string ) {
42+ return new Error ( extractMessage ( payload ) || fallback ) ;
6343}
6444
6545export interface LoginCredentials {
@@ -75,113 +55,71 @@ export interface RegisterData {
7555 cpf ?: string ;
7656}
7757
78- export async function login ( credentials : LoginCredentials ) : Promise < { token ?: string ; user ?: any ; message ?: string } > {
79- console . log ( "Iniciando login com:" , { email : credentials . email } ) ;
80- const url = buildApiUrl ( "/api/auth/login" ) ;
81- console . log ( "URL:" , url ) ;
82- console . log ( "Método: POST" ) ;
83-
84- const response = await fetch ( url , {
58+ export async function login ( credentials : LoginCredentials ) {
59+ const response = await fetch ( buildUrl ( "/api/auth/login" ) , {
8560 method : "POST" ,
86- headers : {
87- "Content-Type" : "application/json" ,
88- } ,
61+ headers : { "Content-Type" : "application/json" } ,
8962 body : JSON . stringify ( credentials ) ,
9063 credentials : "include" ,
9164 } ) ;
92-
93- console . log ( "Status da resposta:" , response . status ) ;
94- console . log ( "Status text:" , response . statusText ) ;
95-
96- const payload = await readPayload ( response ) ;
97-
65+
66+ const payload = await parseResponse ( response ) ;
67+
9868 if ( ! response . ok ) {
99- console . error ( "Login falhou:" , payload ) ;
100- throw buildError ( readMessage ( payload ) , "Falha ao fazer login." ) ;
69+ throw createError ( payload , "Falha ao fazer login." ) ;
10170 }
102-
103- console . log ( "Login bem sucedido:" , payload ) ;
71+
10472 return payload ;
10573}
10674
107- export async function register ( userData : RegisterData ) : Promise < { message ?: string ; user ?: any } > {
108- console . log ( "Iniciando registro com:" , { email : userData . email , name : userData . name } ) ;
109- const url = buildApiUrl ( "/api/auth/register" ) ;
110- console . log ( "URL:" , url ) ;
111- console . log ( "Método: POST" ) ;
112-
113- const bodyData = {
114- email : userData . email ,
115- password : userData . password ,
116- name : userData . name ,
117- } ;
118- console . log ( "Body enviado:" , bodyData ) ;
119-
120- const response = await fetch ( url , {
75+ export async function register ( userData : RegisterData ) {
76+ const response = await fetch ( buildUrl ( "/api/auth/register" ) , {
12177 method : "POST" ,
122- headers : {
123- "Content-Type" : "application/json" ,
124- } ,
125- body : JSON . stringify ( bodyData ) ,
78+ headers : { "Content-Type" : "application/json" } ,
79+ body : JSON . stringify ( {
80+ email : userData . email ,
81+ password : userData . password ,
82+ name : userData . name ,
83+ phone : userData . phone ,
84+ cpf : userData . cpf ,
85+ } ) ,
12686 credentials : "include" ,
12787 } ) ;
128-
129- console . log ( "Status da resposta:" , response . status ) ;
130- console . log ( "Status text:" , response . statusText ) ;
131- console . log ( "Headers:" , {
132- contentType : response . headers . get ( "content-type" ) ,
133- cors : response . headers . get ( "access-control-allow-origin" ) ,
134- } ) ;
135-
136- const payload = await readPayload ( response ) ;
137-
88+
89+ const payload = await parseResponse ( response ) ;
90+
13891 if ( ! response . ok ) {
139- console . error ( " Registro falhou:" , { status : response . status , payload } ) ;
140- throw buildError ( readMessage ( payload ) , `Falha ao cadastrar: ${ response . status } ${ response . statusText } ` ) ;
92+ throw createError ( payload , "Falha ao cadastrar." ) ;
14193 }
142-
143- console . log ( " Registro bem sucedido:" , payload ) ;
94+
14495 return payload ;
14596}
14697
147- export async function logout ( ) : Promise < void > {
148- console . log ( " Iniciando logout" ) ;
149- const url = buildApiUrl ( "/api/auth/logout" ) ;
150- console . log ( " URL:" , url ) ;
151-
152- const response = await fetch ( url , {
98+ export async function logout ( ) {
99+ const response = await fetch ( buildUrl ( "/api/auth/logout" ) , {
153100 method : "POST" ,
154101 credentials : "include" ,
155102 } ) ;
156-
157- console . log ( " Status:" , response . status ) ;
158-
103+
104+ const payload = await parseResponse ( response ) ;
105+
159106 if ( ! response . ok ) {
160- const payload = await readPayload ( response ) ;
161- console . error ( " Logout falhou:" , payload ) ;
162- throw buildError ( readMessage ( payload ) , "Falha ao fazer logout." ) ;
107+ throw createError ( payload , "Falha ao fazer logout." ) ;
163108 }
164-
165- console . log ( "Logout bem sucedido" ) ;
109+
110+ return payload ;
166111}
167112
168- export async function getCurrentUser ( ) : Promise < any > {
169- console . log ( "🚀 Buscando usuário atual" ) ;
170- const url = buildApiUrl ( "/api/auth/me" ) ;
171- console . log ( "URL:" , url ) ;
172-
173- const response = await fetch ( url , {
113+ export async function getCurrentUser ( ) {
114+ const response = await fetch ( buildUrl ( "/api/auth/me" ) , {
174115 credentials : "include" ,
175116 } ) ;
176-
177- console . log ( "📡 Status:" , response . status ) ;
178- const payload = await readPayload ( response ) ;
179-
117+
118+ const payload = await parseResponse ( response ) ;
119+
180120 if ( ! response . ok ) {
181- console . error ( " Busca falhou:" , payload ) ;
182- throw buildError ( readMessage ( payload ) , "Falha ao carregar usuário." ) ;
121+ throw createError ( payload , "Falha ao carregar usuário." ) ;
183122 }
184-
185- console . log ( " Usuário encontrado:" , payload ) ;
123+
186124 return payload ;
187125}
0 commit comments