1
1
import { {{ spec .title | caseUcfirst }}Exception } 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
11
'x-sdk-version': '{{spec .title | caseDash }}:{{ language .name | caseLower }}:{{ sdk .version }}',
13
12
{% for key ,header in spec .global .defaultHeaders %}
@@ -55,32 +54,32 @@ export class Client {
55
54
return this;
56
55
}
57
56
58
- withoutHeader(key: string, headers: DocumentData ): DocumentData {
59
- return Object.keys(headers).reduce((acc: DocumentData , cv) => {
60
- if (cv == 'content-type') return acc
61
- acc[cv] = headers[cv]
62
- return acc
57
+ withoutHeader(key: string, headers: Payload ): Payload {
58
+ return Object.keys(headers).reduce((acc: Payload , cv) => {
59
+ if (cv == 'content-type') return acc;
60
+ acc[cv] = headers[cv];
61
+ return acc;
63
62
}, {})
64
63
}
65
64
66
- async call(method: string, path: string = '', headers: DocumentData = {}, params: DocumentData = {}) {
67
- headers = Object.assign( this.headers, headers) ;
65
+ async call(method: string, path: string = '', headers: Payload = {}, params: Payload = {}) {
66
+ headers = { ... this.headers, ... headers } ;
68
67
69
68
let body;
70
- const url = new URL(this.endpoint + path)
69
+ const url = new URL(this.endpoint + path);
71
70
if (method.toUpperCase() === 'GET') {
72
- url.search = new URLSearchParams(this.flatten(params)).toString()
73
- body = null
71
+ url.search = new URLSearchParams(this.flatten(params)).toString();
72
+ body = null;
74
73
} else if (headers['content-type'].toLowerCase().startsWith('multipart/form-data')) {
75
- headers = this.withoutHeader('content-type', headers)
76
- const formData = new FormData()
77
- const flatParams = this.flatten(params)
74
+ headers = this.withoutHeader('content-type', headers);
75
+ const formData = new FormData();
76
+ const flatParams = this.flatten(params);
78
77
for (const key in flatParams) {
79
78
formData.append(key, flatParams[key]);
80
79
}
81
- body = formData
80
+ body = formData;
82
81
} else {
83
- body = JSON.stringify(params)
82
+ body = JSON.stringify(params);
84
83
}
85
84
86
85
const options = {
@@ -99,7 +98,7 @@ export class Client {
99
98
throw new {{ spec .title | caseUcfirst }}Exception(res.message, res.status, res);
100
99
}
101
100
102
- return response.json()
101
+ return response.json();
103
102
} else {
104
103
if(response.status >= 400) {
105
104
let res = await response.text();
@@ -112,15 +111,15 @@ export class Client {
112
111
}
113
112
}
114
113
115
- flatten(data: DocumentData , prefix = '') {
116
- let output: DocumentData = {};
114
+ flatten(data: Payload , prefix = '') {
115
+ let output: Payload = {};
117
116
118
117
for (const key in data) {
119
118
let value = data[key];
120
119
let finalKey = prefix ? prefix + '[' + key +']' : key;
121
120
122
121
if (Array.isArray(value)) {
123
- output = Object.assign( output, this.flatten(value, finalKey)) ; // @todo: handle name collision here if needed
122
+ output = { ... output, ... this.flatten(value, finalKey) } ; // @todo: handle name collision here if needed
124
123
}
125
124
else {
126
125
output[finalKey] = value;
0 commit comments