Skip to content

Commit b72ade6

Browse files
Merge pull request #108 from appwrite/dev
Dev
2 parents ae7f9fe + 55a809c commit b72ade6

24 files changed

+5784
-2563
lines changed

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2023 Appwrite (https://appwrite.io) and individual contributors.
1+
Copyright (c) 2024 Appwrite (https://appwrite.io) and individual contributors.
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Appwrite Command Line SDK
22

33
![License](https://img.shields.io/github/license/appwrite/sdk-for-cli.svg?style=flat-square)
4-
![Version](https://img.shields.io/badge/api%20version-1.4.12-blue.svg?style=flat-square)
4+
![Version](https://img.shields.io/badge/api%20version-1.4.13-blue.svg?style=flat-square)
55
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
66
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite)
77
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)
@@ -29,7 +29,7 @@ Once the installation is complete, you can verify the install using
2929

3030
```sh
3131
$ appwrite -v
32-
4.2.0
32+
4.2.1
3333
```
3434

3535
### Install using prebuilt binaries
@@ -63,7 +63,7 @@ $ scoop install https://raw.githubusercontent.com/appwrite/sdk-for-cli/master/sc
6363
Once the installation completes, you can verify your install using
6464
```
6565
$ appwrite -v
66-
4.2.0
66+
4.2.1
6767
```
6868

6969
## Getting Started

install.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# You can use "View source" of this page to see the full script.
1414

1515
# REPO
16-
$GITHUB_x64_URL = "https://github.com/appwrite/sdk-for-cli/releases/download/4.2.0/appwrite-cli-win-x64.exe"
17-
$GITHUB_arm64_URL = "https://github.com/appwrite/sdk-for-cli/releases/download/4.2.0/appwrite-cli-win-arm64.exe"
16+
$GITHUB_x64_URL = "https://github.com/appwrite/sdk-for-cli/releases/download/4.2.1/appwrite-cli-win-x64.exe"
17+
$GITHUB_arm64_URL = "https://github.com/appwrite/sdk-for-cli/releases/download/4.2.1/appwrite-cli-win-arm64.exe"
1818

1919
$APPWRITE_BINARY_NAME = "appwrite.exe"
2020

install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ printSuccess() {
9797
downloadBinary() {
9898
echo "[2/4] Downloading executable for $OS ($ARCH) ..."
9999

100-
GITHUB_LATEST_VERSION="4.2.0"
100+
GITHUB_LATEST_VERSION="4.2.1"
101101
GITHUB_FILE="appwrite-cli-${OS}-${ARCH}"
102102
GITHUB_URL="https://github.com/$GITHUB_REPOSITORY_NAME/releases/download/$GITHUB_LATEST_VERSION/$GITHUB_FILE"
103103

lib/client.js

Lines changed: 61 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
const os = require('os');
22
const https = require("https");
3-
const axios = require("axios");
3+
const { fetch, FormData, Agent } = require("undici");
44
const JSONbig = require("json-bigint")({ storeAsString: false });
5-
const FormData = require("form-data");
65
const AppwriteException = require("./exception.js");
76
const { globalConfig } = require("./config.js");
87

98
class 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

Comments
 (0)