Skip to content

Commit

Permalink
Merge pull request #108 from appwrite/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
christyjacob4 authored Feb 14, 2024
2 parents ae7f9fe + 55a809c commit b72ade6
Show file tree
Hide file tree
Showing 24 changed files with 5,784 additions and 2,563 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2023 Appwrite (https://appwrite.io) and individual contributors.
Copyright (c) 2024 Appwrite (https://appwrite.io) and individual contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Appwrite Command Line SDK

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

```sh
$ appwrite -v
4.2.0
4.2.1
```

### Install using prebuilt binaries
Expand Down Expand Up @@ -63,7 +63,7 @@ $ scoop install https://raw.githubusercontent.com/appwrite/sdk-for-cli/master/sc
Once the installation completes, you can verify your install using
```
$ appwrite -v
4.2.0
4.2.1
```

## Getting Started
Expand Down
4 changes: 2 additions & 2 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# You can use "View source" of this page to see the full script.

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

$APPWRITE_BINARY_NAME = "appwrite.exe"

Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ printSuccess() {
downloadBinary() {
echo "[2/4] Downloading executable for $OS ($ARCH) ..."

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

Expand Down
135 changes: 61 additions & 74 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const os = require('os');
const https = require("https");
const axios = require("axios");
const { fetch, FormData, Agent } = require("undici");
const JSONbig = require("json-bigint")({ storeAsString: false });
const FormData = require("form-data");
const AppwriteException = require("./exception.js");
const { globalConfig } = require("./config.js");

class Client {
static CHUNK_SIZE = 5*1024*1024; // 5MB
CHUNK_SIZE = 5*1024*1024; // 5MB

constructor() {
this.endpoint = 'https://HOSTNAME/v1';
Expand All @@ -16,8 +15,8 @@ class Client {
'x-sdk-name': 'Command Line',
'x-sdk-platform': 'console',
'x-sdk-language': 'cli',
'x-sdk-version': '4.2.0',
'user-agent' : `AppwriteCLI/4.2.0 (${os.type()} ${os.version()}; ${os.arch()})`,
'x-sdk-version': '4.2.1',
'user-agent' : `AppwriteCLI/4.2.1 (${os.type()} ${os.version()}; ${os.arch()})`,
'X-Appwrite-Response-Format' : '1.4.0',
};
}
Expand Down Expand Up @@ -144,93 +143,81 @@ class Client {
return this;
}

async call(
method,
path = "",
headers = {},
params = {},
responseType = "json"
) {
headers = Object.assign({}, this.headers, headers);
async call(method, path = "", headers = {}, params = {}, responseType = "json") {
headers = {...this.headers, ...headers};
const url = new URL(this.endpoint + path);

let contentType = headers["content-type"].toLowerCase();
let body = undefined;

let formData = null;
if (method.toUpperCase() === "GET") {
url.search = new URLSearchParams(Client.flatten(params)).toString();
} else if (headers["content-type"]?.toLowerCase().startsWith("multipart/form-data")) {
delete headers["content-type"];
const formData = new FormData();

if (contentType.startsWith("multipart/form-data")) {
const form = new FormData();
const flatParams = Client.flatten(params);

let flatParams = Client.flatten(params);

for (const key in flatParams) {
form.append(key, flatParams[key]);
for (const [key, value] of Object.entries(flatParams)) {
if (value && value.type && value.type === "file") {
formData.append(key, value.file, value.filename);
} else {
formData.append(key, value);
}
}

headers = {
...headers,
...form.getHeaders(),
};
body = formData;
} else {
body = JSON.stringify(params);
}

formData = form;
let response = undefined;
try {
response = await fetch(url.toString(), {
method: method.toUpperCase(),
headers,
body,
dispatcher: new Agent({
connect: {
rejectUnauthorized: !this.selfSigned,
},
}),
});
} catch (error) {
throw new AppwriteException(error.message);
}

let options = {
method: method.toUpperCase(),
url: this.endpoint + path,
params: method.toUpperCase() === "GET" ? params : {},
headers: headers,
data:
method.toUpperCase() === "GET" || contentType.startsWith("multipart/form-data") ? formData : params,
json: contentType.startsWith("application/json"),
transformRequest: method.toUpperCase() === "GET" || contentType.startsWith("multipart/form-data") ? undefined : (data) => JSONbig.stringify(data),
transformResponse: [ (data) => data ? JSONbig.parse(data) : data ],
responseType: responseType,
};
if (this.selfSigned == true) {
// Allow self signed requests
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
if (response.status >= 400) {
const text = await response.text();
let json = undefined;
try {
json = JSON.parse(text);
} catch (error) {
throw new AppwriteException(text, response.status, "", text);
}
throw new AppwriteException(json.message, json.code, json.type, json);
}

if (responseType === "arraybuffer") {
const data = await response.arrayBuffer();
return data;
}

const text = await response.text();
let json = undefined;
try {
let response = await axios(options);
if (response.headers["set-cookie"]) {
globalConfig.setCookie(response.headers["set-cookie"][0]);
}
return response.data;
json = JSON.parse(text);
} catch (error) {
if ("response" in error && error.response !== undefined) {
if (error.response && "data" in error.response) {
if (typeof error.response.data === "string") {
throw new AppwriteException(
error.response.data,
error.response.status,
error.response.data
);
} else {
throw new AppwriteException(
error.response.data.message,
error.response.status,
error.response.data
);
}
} else {
throw new AppwriteException(
error.response.statusText,
error.response.status,
error.response.data
);
}
} else {
throw new AppwriteException(error.message);
}
return text;
}
return json;
}

static flatten(data, prefix = "") {
static flatten(data, prefix = '') {
let output = {};

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

if (Array.isArray(value)) {
output = Object.assign(output, Client.flatten(value, finalKey)); // @todo: handle name collision here if needed
Expand All @@ -243,4 +230,4 @@ class Client {
}
}

module.exports = Client;
module.exports = Client;
Loading

0 comments on commit b72ade6

Please sign in to comment.