@@ -60,24 +60,42 @@ const getImageUrlAvatarAPI = async (email: string) => {
6060 return null ;
6161 }
6262
63- const response = await fetch ( "https://avatarapi.com/v2/api.aspx" , {
64- method : "POST" ,
65- headers : {
66- "Content-Type" : "text/plain" ,
67- } ,
68- body : JSON . stringify ( {
69- username : process . env . AVATARAPI_USERNAME ,
70- password : process . env . AVATARAPI_PASSWORD ,
71- email : email ,
72- } ) ,
73- } ) ;
74-
75- const info = await response . json ( ) ;
63+ const controller = new AbortController ( ) ;
64+ const timeout = setTimeout ( ( ) => controller . abort ( ) , 10_000 ) ;
7665
77- if ( ! info . Success ) {
78- console . log ( "Error from avatar api: " , info . Error ) ;
66+ try {
67+ const response = await fetch ( "https://avatarapi.com/v2/api.aspx" , {
68+ method : "POST" ,
69+ headers : {
70+ "Content-Type" : "text/plain" ,
71+ } ,
72+ body : JSON . stringify ( {
73+ username : process . env . AVATARAPI_USERNAME ,
74+ password : process . env . AVATARAPI_PASSWORD ,
75+ email,
76+ } ) ,
77+ signal : controller . signal ,
78+ } ) ;
79+
80+ clearTimeout ( timeout ) ;
81+
82+ const info = await response . json ( ) ;
83+
84+ if ( ! info . Success ) {
85+ if ( info . Error === "Not found" ) {
86+ // Expected case: no avatar for this email
87+ return null ;
88+ }
89+ console . warn ( "Avatar API error:" , info . Error ) ;
90+ return null ;
91+ }
92+ return info . Image as string ;
93+ } catch ( error : unknown ) {
94+ if ( error instanceof DOMException && error . name === "AbortError" ) {
95+ console . warn ( "Avatar API request timed out" ) ;
96+ } else {
97+ console . error ( "Avatar API request failed:" , error ) ;
98+ }
7999 return null ;
80100 }
81-
82- return info . Image as string ;
83101} ;
0 commit comments