Skip to content

Commit e994a15

Browse files
add param checking and removed default values
1 parent ae6dcaf commit e994a15

File tree

11 files changed

+1822
-1075
lines changed

11 files changed

+1822
-1075
lines changed

src/client.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { AppwriteException } from './exception.ts';
22

3-
export interface DocumentData {
3+
export interface Payload {
44
[key: string]: any;
55
}
66

77
export class Client {
8-
98
endpoint: string = 'https://appwrite.io/v1';
10-
headers: DocumentData = {
9+
headers: Payload = {
1110
'content-type': '',
12-
'x-sdk-version': 'appwrite:deno:0.2.1',
11+
'x-sdk-version': 'appwrite:deno:0.2.2',
1312
'X-Appwrite-Response-Format':'0.8.0',
1413
};
1514

@@ -92,32 +91,32 @@ export class Client {
9291
return this;
9392
}
9493

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;
10099
}, {})
101100
}
102101

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 };
105104

106105
let body;
107-
const url = new URL(this.endpoint + path)
106+
const url = new URL(this.endpoint + path);
108107
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;
111110
} 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);
115114
for (const key in flatParams) {
116115
formData.append(key, flatParams[key]);
117116
}
118-
body = formData
117+
body = formData;
119118
} else {
120-
body = JSON.stringify(params)
119+
body = JSON.stringify(params);
121120
}
122121

123122
const options = {
@@ -136,7 +135,7 @@ export class Client {
136135
throw new AppwriteException(res.message, res.status, res);
137136
}
138137

139-
return response.json()
138+
return response.json();
140139
} else {
141140
if(response.status >= 400) {
142141
let res = await response.text();
@@ -149,15 +148,15 @@ export class Client {
149148
}
150149
}
151150

152-
flatten(data: DocumentData, prefix = '') {
153-
let output: DocumentData = {};
151+
flatten(data: Payload, prefix = '') {
152+
let output: Payload = {};
154153

155154
for (const key in data) {
156155
let value = data[key];
157156
let finalKey = prefix ? prefix + '[' + key +']' : key;
158157

159158
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
161160
}
162161
else {
163162
output[finalKey] = value;

src/exception.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export class AppwriteException {
33
code: Number;
44
response: any;
55

6-
constructor(message: String, code: Number, response: any) {
6+
constructor(message: String, code: Number = 0, response: any = "") {
77
this.message = message;
88
this.code = code;
99
this.response = response;

0 commit comments

Comments
 (0)