11const os = require ( 'os' ) ;
22const https = require ( "https" ) ;
3- const axios = require ( "axios " ) ;
3+ const { fetch , FormData , Agent } = require ( "undici " ) ;
44const JSONbig = require ( "json-bigint" ) ( { storeAsString : false } ) ;
5- const FormData = require ( "form-data" ) ;
65const AppwriteException = require ( "./exception.js" ) ;
76const { globalConfig } = require ( "./config.js" ) ;
87
98class Client {
10- static CHUNK_SIZE = 5 * 1024 * 1024 ; // 5MB
9+ CHUNK_SIZE = 5 * 1024 * 1024 ; // 5MB
1110
1211 constructor ( ) {
1312 this . endpoint = 'https://HOSTNAME/v1' ;
@@ -16,8 +15,8 @@ class Client {
1615 'x-sdk-name' : 'Command Line' ,
1716 'x-sdk-platform' : 'console' ,
1817 'x-sdk-language' : 'cli' ,
19- 'x-sdk-version' : '4.2.0 ' ,
20- 'user-agent' : `AppwriteCLI/4.2.0 (${ os . type ( ) } ${ os . version ( ) } ; ${ os . arch ( ) } )` ,
18+ 'x-sdk-version' : '4.2.1 ' ,
19+ 'user-agent' : `AppwriteCLI/4.2.1 (${ os . type ( ) } ${ os . version ( ) } ; ${ os . arch ( ) } )` ,
2120 'X-Appwrite-Response-Format' : '1.4.0' ,
2221 } ;
2322 }
@@ -144,93 +143,81 @@ class Client {
144143 return this ;
145144 }
146145
147- async call (
148- method ,
149- path = "" ,
150- headers = { } ,
151- params = { } ,
152- responseType = "json"
153- ) {
154- headers = Object . assign ( { } , this . headers , headers ) ;
146+ async call ( method , path = "" , headers = { } , params = { } , responseType = "json" ) {
147+ headers = { ...this . headers , ...headers } ;
148+ const url = new URL ( this . endpoint + path ) ;
155149
156- let contentType = headers [ "content-type" ] . toLowerCase ( ) ;
150+ let body = undefined ;
157151
158- let formData = null ;
152+ if ( method . toUpperCase ( ) === "GET" ) {
153+ url . search = new URLSearchParams ( Client . flatten ( params ) ) . toString ( ) ;
154+ } else if ( headers [ "content-type" ] ?. toLowerCase ( ) . startsWith ( "multipart/form-data" ) ) {
155+ delete headers [ "content-type" ] ;
156+ const formData = new FormData ( ) ;
159157
160- if ( contentType . startsWith ( "multipart/form-data" ) ) {
161- const form = new FormData ( ) ;
158+ const flatParams = Client . flatten ( params ) ;
162159
163- let flatParams = Client . flatten ( params ) ;
164-
165- for ( const key in flatParams ) {
166- form . append ( key , flatParams [ key ] ) ;
160+ for ( const [ key , value ] of Object . entries ( flatParams ) ) {
161+ if ( value && value . type && value . type === "file" ) {
162+ formData . append ( key , value . file , value . filename ) ;
163+ } else {
164+ formData . append ( key , value ) ;
165+ }
167166 }
168167
169- headers = {
170- ... headers ,
171- ... form . getHeaders ( ) ,
172- } ;
168+ body = formData ;
169+ } else {
170+ body = JSON . stringify ( params ) ;
171+ }
173172
174- formData = form ;
173+ let response = undefined ;
174+ try {
175+ response = await fetch ( url . toString ( ) , {
176+ method : method . toUpperCase ( ) ,
177+ headers,
178+ body,
179+ dispatcher : new Agent ( {
180+ connect : {
181+ rejectUnauthorized : ! this . selfSigned ,
182+ } ,
183+ } ) ,
184+ } ) ;
185+ } catch ( error ) {
186+ throw new AppwriteException ( error . message ) ;
175187 }
176188
177- let options = {
178- method : method . toUpperCase ( ) ,
179- url : this . endpoint + path ,
180- params : method . toUpperCase ( ) === "GET" ? params : { } ,
181- headers : headers ,
182- data :
183- method . toUpperCase ( ) === "GET" || contentType . startsWith ( "multipart/form-data" ) ? formData : params ,
184- json : contentType . startsWith ( "application/json" ) ,
185- transformRequest : method . toUpperCase ( ) === "GET" || contentType . startsWith ( "multipart/form-data" ) ? undefined : ( data ) => JSONbig . stringify ( data ) ,
186- transformResponse : [ ( data ) => data ? JSONbig . parse ( data ) : data ] ,
187- responseType : responseType ,
188- } ;
189- if ( this . selfSigned == true ) {
190- // Allow self signed requests
191- options . httpsAgent = new https . Agent ( { rejectUnauthorized : false } ) ;
189+ if ( response . status >= 400 ) {
190+ const text = await response . text ( ) ;
191+ let json = undefined ;
192+ try {
193+ json = JSON . parse ( text ) ;
194+ } catch ( error ) {
195+ throw new AppwriteException ( text , response . status , "" , text ) ;
196+ }
197+ throw new AppwriteException ( json . message , json . code , json . type , json ) ;
192198 }
199+
200+ if ( responseType === "arraybuffer" ) {
201+ const data = await response . arrayBuffer ( ) ;
202+ return data ;
203+ }
204+
205+ const text = await response . text ( ) ;
206+ let json = undefined ;
193207 try {
194- let response = await axios ( options ) ;
195- if ( response . headers [ "set-cookie" ] ) {
196- globalConfig . setCookie ( response . headers [ "set-cookie" ] [ 0 ] ) ;
197- }
198- return response . data ;
208+ json = JSON . parse ( text ) ;
199209 } catch ( error ) {
200- if ( "response" in error && error . response !== undefined ) {
201- if ( error . response && "data" in error . response ) {
202- if ( typeof error . response . data === "string" ) {
203- throw new AppwriteException (
204- error . response . data ,
205- error . response . status ,
206- error . response . data
207- ) ;
208- } else {
209- throw new AppwriteException (
210- error . response . data . message ,
211- error . response . status ,
212- error . response . data
213- ) ;
214- }
215- } else {
216- throw new AppwriteException (
217- error . response . statusText ,
218- error . response . status ,
219- error . response . data
220- ) ;
221- }
222- } else {
223- throw new AppwriteException ( error . message ) ;
224- }
210+ return text ;
225211 }
212+ return json ;
226213 }
227214
228- static flatten ( data , prefix = "" ) {
215+ static flatten ( data , prefix = '' ) {
229216 let output = { } ;
230217
231218 for ( const key in data ) {
232219 let value = data [ key ] ;
233- let finalKey = prefix ? prefix + "[" + key + "]" : key ;
220+ let finalKey = prefix ? prefix + '[' + key + ']' : key ;
234221
235222 if ( Array . isArray ( value ) ) {
236223 output = Object . assign ( output , Client . flatten ( value , finalKey ) ) ; // @todo : handle name collision here if needed
@@ -243,4 +230,4 @@ class Client {
243230 }
244231}
245232
246- module . exports = Client ;
233+ module . exports = Client ;
0 commit comments