11import { {{ spec .title | caseUcfirst }}Exception } from './exception.ts';
22
3- export interface DocumentData {
3+ export interface Payload {
44 [key: string]: any;
55}
66
77export class Client {
8-
98 endpoint: string = 'https://appwrite.io/v1';
10- headers: DocumentData = {
9+ headers: Payload = {
1110 'content-type': '',
1211 'x-sdk-version': '{{spec .title | caseDash }}:{{ language .name | caseLower }}:{{ sdk .version }}',
1312{% for key ,header in spec .global .defaultHeaders %}
@@ -55,32 +54,32 @@ export class Client {
5554 return this;
5655 }
5756
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;
6362 }, {})
6463 }
6564
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 } ;
6867
6968 let body;
70- const url = new URL(this.endpoint + path)
69+ const url = new URL(this.endpoint + path);
7170 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;
7473 } 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);
7877 for (const key in flatParams) {
7978 formData.append(key, flatParams[key]);
8079 }
81- body = formData
80+ body = formData;
8281 } else {
83- body = JSON.stringify(params)
82+ body = JSON.stringify(params);
8483 }
8584
8685 const options = {
@@ -99,7 +98,7 @@ export class Client {
9998 throw new {{ spec .title | caseUcfirst }}Exception(res.message, res.status, res);
10099 }
101100
102- return response.json()
101+ return response.json();
103102 } else {
104103 if(response.status >= 400) {
105104 let res = await response.text();
@@ -112,15 +111,15 @@ export class Client {
112111 }
113112 }
114113
115- flatten(data: DocumentData , prefix = '') {
116- let output: DocumentData = {};
114+ flatten(data: Payload , prefix = '') {
115+ let output: Payload = {};
117116
118117 for (const key in data) {
119118 let value = data[key];
120119 let finalKey = prefix ? prefix + '[' + key +']' : key;
121120
122121 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
124123 }
125124 else {
126125 output[finalKey] = value;
0 commit comments