@@ -11,21 +11,17 @@ interface SignatureResponse {
1111export const request = ( formData : FormData , options : ImageKitOptions & { authenticationEndpoint : string } , callback ?: ( err : Error | null , response : UploadResponse | null ) => void ) => {
1212 generateSignatureToken ( options , ( err , signaturObj ) => {
1313 if ( err ) {
14- if ( typeof callback != "function" ) return ;
15- callback ( err , null ) ;
16- return ;
14+ return respond ( true , err , callback )
1715 } else {
1816 formData . append ( "signature" , signaturObj ?. signature || "" ) ;
1917 formData . append ( "expire" , String ( signaturObj ?. expire || 0 ) ) ;
2018 formData . append ( "token" , signaturObj ?. token || "" ) ;
2119
2220 uploadFile ( formData , ( err , responseSucessText ) => {
23- if ( typeof callback != "function" ) return ;
2421 if ( err ) {
25- callback ( err , null ) ;
26- } else {
27- callback ( null , responseSucessText ! ) ;
22+ return respond ( true , err , callback )
2823 }
24+ return respond ( false , responseSucessText ! , callback )
2925 } ) ;
3026 }
3127 } ) ;
@@ -36,13 +32,10 @@ export const generateSignatureToken = (options: ImageKitOptions & { authenticati
3632 xhr . timeout = 60000 ;
3733 xhr . open ( 'GET' , options . authenticationEndpoint ) ;
3834 xhr . ontimeout = function ( e ) {
39- if ( typeof callback != "function" ) return ;
4035 respond ( true , errorMessages . AUTH_ENDPOINT_TIMEOUT , callback ) ;
41- return ;
4236 } ;
4337 xhr . onerror = function ( ) {
4438 respond ( true , errorMessages . AUTH_ENDPOINT_NETWORK_ERROR , callback ) ;
45- return ;
4639 }
4740 xhr . onload = function ( ) {
4841 if ( xhr . status === 200 ) {
@@ -53,20 +46,16 @@ export const generateSignatureToken = (options: ImageKitOptions & { authenticati
5346 expire : body . expire ,
5447 token : body . token
5548 }
56- callback ( null , obj ) ;
57- return ;
49+ respond ( false , obj , callback )
5850 } catch ( ex ) {
59- if ( typeof callback != "function" ) return ;
60- callback ( ex , null ) ;
51+ respond ( true , ex , callback )
6152 }
6253 } else {
6354 try {
6455 var error = JSON . parse ( xhr . responseText ) ;
65- if ( typeof callback != "function" ) return ;
66- callback ( error , null ) ;
56+ respond ( true , error , callback ) ;
6757 } catch ( ex ) {
68- if ( typeof callback != "function" ) return ;
69- callback ( ex , null ) ;
58+ respond ( true , ex , callback ) ;
7059 }
7160 }
7261 } ;
@@ -83,12 +72,10 @@ export const uploadFile = (formData: FormData, callback: (err: Error | null, res
8372 }
8473 uploadFileXHR . onload = function ( ) {
8574 if ( uploadFileXHR . status === 200 ) {
86- if ( typeof callback != "function" ) return ;
8775 var uploadResponse = JSON . parse ( uploadFileXHR . responseText ) ;
8876 callback ( null , uploadResponse ) ;
8977 }
9078 else if ( uploadFileXHR . status !== 200 ) {
91- if ( typeof callback != "function" ) return ;
9279 try {
9380 callback ( JSON . parse ( uploadFileXHR . responseText ) , null ) ;
9481 } catch ( ex ) {
0 commit comments