1
1
import { AppwriteException } from './exception.ts' ;
2
2
3
- export interface DocumentData {
3
+ export interface Payload {
4
4
[ key : string ] : any ;
5
5
}
6
6
7
7
export class Client {
8
-
9
8
endpoint : string = 'https://appwrite.io/v1' ;
10
- headers : DocumentData = {
9
+ headers : Payload = {
11
10
'content-type' : '' ,
12
- 'x-sdk-version' : 'appwrite:deno:0.2.1 ' ,
11
+ 'x-sdk-version' : 'appwrite:deno:0.2.2 ' ,
13
12
'X-Appwrite-Response-Format' :'0.8.0' ,
14
13
} ;
15
14
@@ -92,32 +91,32 @@ export class Client {
92
91
return this ;
93
92
}
94
93
95
- withoutHeader ( key : string , headers : DocumentData ) : DocumentData {
96
- return Object . keys ( headers ) . reduce ( ( acc : DocumentData , cv ) => {
97
- if ( cv == 'content-type' ) return acc
98
- acc [ cv ] = headers [ cv ]
99
- return acc
94
+ withoutHeader ( key : string , headers : Payload ) : Payload {
95
+ return Object . keys ( headers ) . reduce ( ( acc : Payload , cv ) => {
96
+ if ( cv == 'content-type' ) return acc ;
97
+ acc [ cv ] = headers [ cv ] ;
98
+ return acc ;
100
99
} , { } )
101
100
}
102
101
103
- async call ( method : string , path : string = '' , headers : DocumentData = { } , params : DocumentData = { } ) {
104
- headers = Object . assign ( this . headers , headers ) ;
102
+ async call ( method : string , path : string = '' , headers : Payload = { } , params : Payload = { } ) {
103
+ headers = { ... this . headers , ... headers } ;
105
104
106
105
let body ;
107
- const url = new URL ( this . endpoint + path )
106
+ const url = new URL ( this . endpoint + path ) ;
108
107
if ( method . toUpperCase ( ) === 'GET' ) {
109
- url . search = new URLSearchParams ( this . flatten ( params ) ) . toString ( )
110
- body = null
108
+ url . search = new URLSearchParams ( this . flatten ( params ) ) . toString ( ) ;
109
+ body = null ;
111
110
} else if ( headers [ 'content-type' ] . toLowerCase ( ) . startsWith ( 'multipart/form-data' ) ) {
112
- headers = this . withoutHeader ( 'content-type' , headers )
113
- const formData = new FormData ( )
114
- const flatParams = this . flatten ( params )
111
+ headers = this . withoutHeader ( 'content-type' , headers ) ;
112
+ const formData = new FormData ( ) ;
113
+ const flatParams = this . flatten ( params ) ;
115
114
for ( const key in flatParams ) {
116
115
formData . append ( key , flatParams [ key ] ) ;
117
116
}
118
- body = formData
117
+ body = formData ;
119
118
} else {
120
- body = JSON . stringify ( params )
119
+ body = JSON . stringify ( params ) ;
121
120
}
122
121
123
122
const options = {
@@ -136,7 +135,7 @@ export class Client {
136
135
throw new AppwriteException ( res . message , res . status , res ) ;
137
136
}
138
137
139
- return response . json ( )
138
+ return response . json ( ) ;
140
139
} else {
141
140
if ( response . status >= 400 ) {
142
141
let res = await response . text ( ) ;
@@ -149,15 +148,15 @@ export class Client {
149
148
}
150
149
}
151
150
152
- flatten ( data : DocumentData , prefix = '' ) {
153
- let output : DocumentData = { } ;
151
+ flatten ( data : Payload , prefix = '' ) {
152
+ let output : Payload = { } ;
154
153
155
154
for ( const key in data ) {
156
155
let value = data [ key ] ;
157
156
let finalKey = prefix ? prefix + '[' + key + ']' : key ;
158
157
159
158
if ( Array . isArray ( value ) ) {
160
- output = Object . assign ( output , this . flatten ( value , finalKey ) ) ; // @todo : handle name collision here if needed
159
+ output = { ... output , ... this . flatten ( value , finalKey ) } ; // @todo : handle name collision here if needed
161
160
}
162
161
else {
163
162
output [ finalKey ] = value ;
0 commit comments