From 85f8b1946a9cc2cde9ff3c480e3b233e19d69ea5 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 28 Jun 2022 13:33:34 +0200 Subject: [PATCH] feat: introduce 0.15.x support --- README.md | 4 +- .../account/create-phone-verification.md | 21 ++ .../account/update-phone-verification.md | 21 ++ docs/examples/account/update-phone.md | 21 ++ .../create-boolean-attribute.md | 4 +- docs/examples/databases/create-collection.md | 21 ++ .../create-document.md | 4 +- .../create-email-attribute.md | 4 +- .../create-enum-attribute.md | 4 +- .../create-float-attribute.md | 4 +- .../{database => databases}/create-index.md | 4 +- .../create-integer-attribute.md | 4 +- .../create-ip-attribute.md | 4 +- .../create-string-attribute.md | 4 +- .../create-url-attribute.md | 4 +- .../create.md} | 4 +- .../delete-attribute.md | 4 +- .../delete-collection.md | 4 +- .../delete-document.md | 4 +- .../{database => databases}/delete-index.md | 4 +- .../delete.md} | 4 +- .../{database => databases}/get-attribute.md | 4 +- .../{database => databases}/get-collection.md | 4 +- .../{database => databases}/get-document.md | 4 +- .../{database => databases}/get-index.md | 4 +- docs/examples/databases/get.md | 21 ++ .../list-attributes.md | 4 +- .../list-collections.md | 4 +- .../{database => databases}/list-documents.md | 4 +- .../{database => databases}/list-indexes.md | 4 +- docs/examples/databases/list.md | 21 ++ docs/examples/databases/update-collection.md | 21 ++ .../update-document.md | 4 +- docs/examples/databases/update.md | 21 ++ .../users/update-email-verification.md | 21 ++ .../users/update-phone-verification.md | 21 ++ ...update-verification.md => update-phone.md} | 2 +- mod.ts | 6 +- src/client.ts | 12 +- src/inputFile.ts | 42 +++ src/models.d.ts | 158 ++++++++--- src/services/account.ts | 97 ++++++- src/services/avatars.ts | 1 + src/services/{database.ts => databases.ts} | 249 ++++++++++++------ src/services/functions.ts | 143 +++++----- src/services/health.ts | 1 + src/services/locale.ts | 1 + src/services/storage.ts | 155 +++++------ src/services/teams.ts | 1 + src/services/users.ts | 61 ++++- 50 files changed, 917 insertions(+), 326 deletions(-) create mode 100644 docs/examples/account/create-phone-verification.md create mode 100644 docs/examples/account/update-phone-verification.md create mode 100644 docs/examples/account/update-phone.md rename docs/examples/{database => databases}/create-boolean-attribute.md (75%) create mode 100644 docs/examples/databases/create-collection.md rename docs/examples/{database => databases}/create-document.md (75%) rename docs/examples/{database => databases}/create-email-attribute.md (75%) rename docs/examples/{database => databases}/create-enum-attribute.md (75%) rename docs/examples/{database => databases}/create-float-attribute.md (75%) rename docs/examples/{database => databases}/create-index.md (76%) rename docs/examples/{database => databases}/create-integer-attribute.md (75%) rename docs/examples/{database => databases}/create-ip-attribute.md (76%) rename docs/examples/{database => databases}/create-string-attribute.md (75%) rename docs/examples/{database => databases}/create-url-attribute.md (75%) rename docs/examples/{database/update-collection.md => databases/create.md} (77%) rename docs/examples/{database => databases}/delete-attribute.md (77%) rename docs/examples/{database => databases}/delete-collection.md (77%) rename docs/examples/{database => databases}/delete-document.md (75%) rename docs/examples/{database => databases}/delete-index.md (77%) rename docs/examples/{database/create-collection.md => databases/delete.md} (73%) rename docs/examples/{database => databases}/get-attribute.md (77%) rename docs/examples/{database => databases}/get-collection.md (78%) rename docs/examples/{database => databases}/get-document.md (76%) rename docs/examples/{database => databases}/get-index.md (78%) create mode 100644 docs/examples/databases/get.md rename docs/examples/{database => databases}/list-attributes.md (78%) rename docs/examples/{database => databases}/list-collections.md (80%) rename docs/examples/{database => databases}/list-documents.md (78%) rename docs/examples/{database => databases}/list-indexes.md (78%) create mode 100644 docs/examples/databases/list.md create mode 100644 docs/examples/databases/update-collection.md rename docs/examples/{database => databases}/update-document.md (75%) create mode 100644 docs/examples/databases/update.md create mode 100644 docs/examples/users/update-email-verification.md create mode 100644 docs/examples/users/update-phone-verification.md rename docs/examples/users/{update-verification.md => update-phone.md} (88%) create mode 100644 src/inputFile.ts rename src/services/{database.ts => databases.ts} (78%) diff --git a/README.md b/README.md index 1672f1d..d93d7a5 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Appwrite Deno SDK ![License](https://img.shields.io/github/license/appwrite/sdk-for-deno.svg?style=flat-square) -![Version](https://img.shields.io/badge/api%20version-0.14.0-blue.svg?style=flat-square) +![Version](https://img.shields.io/badge/api%20version-0.15.0-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) -**This SDK is compatible with Appwrite server version 0.14.x. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-deno/releases).** +**This SDK is compatible with Appwrite server version 0.15.x. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-deno/releases).** Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Deno SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to [https://appwrite.io/docs](https://appwrite.io/docs) diff --git a/docs/examples/account/create-phone-verification.md b/docs/examples/account/create-phone-verification.md new file mode 100644 index 0000000..a095d63 --- /dev/null +++ b/docs/examples/account/create-phone-verification.md @@ -0,0 +1,21 @@ +import * as sdk from "https://deno.land/x/appwrite/mod.ts"; + +// Init SDK +let client = new sdk.Client(); + +let account = new sdk.Account(client); + +client + .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint + .setProject('5df5acd0d48c2') // Your project ID + .setJWT('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ...') // Your secret JSON Web Token +; + + +let promise = account.createPhoneVerification(); + +promise.then(function (response) { + console.log(response); +}, function (error) { + console.log(error); +}); \ No newline at end of file diff --git a/docs/examples/account/update-phone-verification.md b/docs/examples/account/update-phone-verification.md new file mode 100644 index 0000000..8f6a264 --- /dev/null +++ b/docs/examples/account/update-phone-verification.md @@ -0,0 +1,21 @@ +import * as sdk from "https://deno.land/x/appwrite/mod.ts"; + +// Init SDK +let client = new sdk.Client(); + +let account = new sdk.Account(client); + +client + .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint + .setProject('5df5acd0d48c2') // Your project ID + .setJWT('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ...') // Your secret JSON Web Token +; + + +let promise = account.updatePhoneVerification('[USER_ID]', '[SECRET]'); + +promise.then(function (response) { + console.log(response); +}, function (error) { + console.log(error); +}); \ No newline at end of file diff --git a/docs/examples/account/update-phone.md b/docs/examples/account/update-phone.md new file mode 100644 index 0000000..1742000 --- /dev/null +++ b/docs/examples/account/update-phone.md @@ -0,0 +1,21 @@ +import * as sdk from "https://deno.land/x/appwrite/mod.ts"; + +// Init SDK +let client = new sdk.Client(); + +let account = new sdk.Account(client); + +client + .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint + .setProject('5df5acd0d48c2') // Your project ID + .setJWT('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ...') // Your secret JSON Web Token +; + + +let promise = account.updatePhone('', 'password'); + +promise.then(function (response) { + console.log(response); +}, function (error) { + console.log(error); +}); \ No newline at end of file diff --git a/docs/examples/database/create-boolean-attribute.md b/docs/examples/databases/create-boolean-attribute.md similarity index 75% rename from docs/examples/database/create-boolean-attribute.md rename to docs/examples/databases/create-boolean-attribute.md index b332758..0c34b34 100644 --- a/docs/examples/database/create-boolean-attribute.md +++ b/docs/examples/databases/create-boolean-attribute.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.createBooleanAttribute('[COLLECTION_ID]', '', false); +let promise = databases.createBooleanAttribute('[COLLECTION_ID]', '', false); promise.then(function (response) { console.log(response); diff --git a/docs/examples/databases/create-collection.md b/docs/examples/databases/create-collection.md new file mode 100644 index 0000000..40968bf --- /dev/null +++ b/docs/examples/databases/create-collection.md @@ -0,0 +1,21 @@ +import * as sdk from "https://deno.land/x/appwrite/mod.ts"; + +// Init SDK +let client = new sdk.Client(); + +let databases = new sdk.Databases(client, '[DATABASE_ID]'); + +client + .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint + .setProject('5df5acd0d48c2') // Your project ID + .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key +; + + +let promise = databases.createCollection('[COLLECTION_ID]', '[NAME]', 'document', ["role:all"], ["role:all"]); + +promise.then(function (response) { + console.log(response); +}, function (error) { + console.log(error); +}); \ No newline at end of file diff --git a/docs/examples/database/create-document.md b/docs/examples/databases/create-document.md similarity index 75% rename from docs/examples/database/create-document.md rename to docs/examples/databases/create-document.md index 5180caf..3cba61a 100644 --- a/docs/examples/database/create-document.md +++ b/docs/examples/databases/create-document.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.createDocument('[COLLECTION_ID]', '[DOCUMENT_ID]', {}); +let promise = databases.createDocument('[COLLECTION_ID]', '[DOCUMENT_ID]', {}); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/create-email-attribute.md b/docs/examples/databases/create-email-attribute.md similarity index 75% rename from docs/examples/database/create-email-attribute.md rename to docs/examples/databases/create-email-attribute.md index 0b192f6..9c181a0 100644 --- a/docs/examples/database/create-email-attribute.md +++ b/docs/examples/databases/create-email-attribute.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.createEmailAttribute('[COLLECTION_ID]', '', false); +let promise = databases.createEmailAttribute('[COLLECTION_ID]', '', false); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/create-enum-attribute.md b/docs/examples/databases/create-enum-attribute.md similarity index 75% rename from docs/examples/database/create-enum-attribute.md rename to docs/examples/databases/create-enum-attribute.md index 7b30a61..f1d7ec9 100644 --- a/docs/examples/database/create-enum-attribute.md +++ b/docs/examples/databases/create-enum-attribute.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.createEnumAttribute('[COLLECTION_ID]', '', [], false); +let promise = databases.createEnumAttribute('[COLLECTION_ID]', '', [], false); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/create-float-attribute.md b/docs/examples/databases/create-float-attribute.md similarity index 75% rename from docs/examples/database/create-float-attribute.md rename to docs/examples/databases/create-float-attribute.md index 75a8afc..5f5235e 100644 --- a/docs/examples/database/create-float-attribute.md +++ b/docs/examples/databases/create-float-attribute.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.createFloatAttribute('[COLLECTION_ID]', '', false); +let promise = databases.createFloatAttribute('[COLLECTION_ID]', '', false); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/create-index.md b/docs/examples/databases/create-index.md similarity index 76% rename from docs/examples/database/create-index.md rename to docs/examples/databases/create-index.md index f519d1f..2c8ddef 100644 --- a/docs/examples/database/create-index.md +++ b/docs/examples/databases/create-index.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.createIndex('[COLLECTION_ID]', '', 'key', []); +let promise = databases.createIndex('[COLLECTION_ID]', '', 'key', []); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/create-integer-attribute.md b/docs/examples/databases/create-integer-attribute.md similarity index 75% rename from docs/examples/database/create-integer-attribute.md rename to docs/examples/databases/create-integer-attribute.md index ef09167..5243071 100644 --- a/docs/examples/database/create-integer-attribute.md +++ b/docs/examples/databases/create-integer-attribute.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.createIntegerAttribute('[COLLECTION_ID]', '', false); +let promise = databases.createIntegerAttribute('[COLLECTION_ID]', '', false); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/create-ip-attribute.md b/docs/examples/databases/create-ip-attribute.md similarity index 76% rename from docs/examples/database/create-ip-attribute.md rename to docs/examples/databases/create-ip-attribute.md index c4daa55..f583be8 100644 --- a/docs/examples/database/create-ip-attribute.md +++ b/docs/examples/databases/create-ip-attribute.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.createIpAttribute('[COLLECTION_ID]', '', false); +let promise = databases.createIpAttribute('[COLLECTION_ID]', '', false); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/create-string-attribute.md b/docs/examples/databases/create-string-attribute.md similarity index 75% rename from docs/examples/database/create-string-attribute.md rename to docs/examples/databases/create-string-attribute.md index 57b3101..17ae2d8 100644 --- a/docs/examples/database/create-string-attribute.md +++ b/docs/examples/databases/create-string-attribute.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.createStringAttribute('[COLLECTION_ID]', '', 1, false); +let promise = databases.createStringAttribute('[COLLECTION_ID]', '', 1, false); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/create-url-attribute.md b/docs/examples/databases/create-url-attribute.md similarity index 75% rename from docs/examples/database/create-url-attribute.md rename to docs/examples/databases/create-url-attribute.md index f298e07..f87448c 100644 --- a/docs/examples/database/create-url-attribute.md +++ b/docs/examples/databases/create-url-attribute.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.createUrlAttribute('[COLLECTION_ID]', '', false); +let promise = databases.createUrlAttribute('[COLLECTION_ID]', '', false); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/update-collection.md b/docs/examples/databases/create.md similarity index 77% rename from docs/examples/database/update-collection.md rename to docs/examples/databases/create.md index c105fe1..1d0ab39 100644 --- a/docs/examples/database/update-collection.md +++ b/docs/examples/databases/create.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.updateCollection('[COLLECTION_ID]', '[NAME]', 'document'); +let promise = databases.create('[NAME]'); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/delete-attribute.md b/docs/examples/databases/delete-attribute.md similarity index 77% rename from docs/examples/database/delete-attribute.md rename to docs/examples/databases/delete-attribute.md index e817850..e109532 100644 --- a/docs/examples/database/delete-attribute.md +++ b/docs/examples/databases/delete-attribute.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.deleteAttribute('[COLLECTION_ID]', ''); +let promise = databases.deleteAttribute('[COLLECTION_ID]', ''); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/delete-collection.md b/docs/examples/databases/delete-collection.md similarity index 77% rename from docs/examples/database/delete-collection.md rename to docs/examples/databases/delete-collection.md index d62c8d0..acb5e27 100644 --- a/docs/examples/database/delete-collection.md +++ b/docs/examples/databases/delete-collection.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.deleteCollection('[COLLECTION_ID]'); +let promise = databases.deleteCollection('[COLLECTION_ID]'); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/delete-document.md b/docs/examples/databases/delete-document.md similarity index 75% rename from docs/examples/database/delete-document.md rename to docs/examples/databases/delete-document.md index 558a4bb..449d3f1 100644 --- a/docs/examples/database/delete-document.md +++ b/docs/examples/databases/delete-document.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.deleteDocument('[COLLECTION_ID]', '[DOCUMENT_ID]'); +let promise = databases.deleteDocument('[COLLECTION_ID]', '[DOCUMENT_ID]'); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/delete-index.md b/docs/examples/databases/delete-index.md similarity index 77% rename from docs/examples/database/delete-index.md rename to docs/examples/databases/delete-index.md index 87bf839..518efa6 100644 --- a/docs/examples/database/delete-index.md +++ b/docs/examples/databases/delete-index.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.deleteIndex('[COLLECTION_ID]', ''); +let promise = databases.deleteIndex('[COLLECTION_ID]', ''); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/create-collection.md b/docs/examples/databases/delete.md similarity index 73% rename from docs/examples/database/create-collection.md rename to docs/examples/databases/delete.md index 9646b8c..ee3af46 100644 --- a/docs/examples/database/create-collection.md +++ b/docs/examples/databases/delete.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.createCollection('[COLLECTION_ID]', '[NAME]', 'document', ["role:all"], ["role:all"]); +let promise = databases.delete(); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/get-attribute.md b/docs/examples/databases/get-attribute.md similarity index 77% rename from docs/examples/database/get-attribute.md rename to docs/examples/databases/get-attribute.md index fc2e622..b0a3e74 100644 --- a/docs/examples/database/get-attribute.md +++ b/docs/examples/databases/get-attribute.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.getAttribute('[COLLECTION_ID]', ''); +let promise = databases.getAttribute('[COLLECTION_ID]', ''); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/get-collection.md b/docs/examples/databases/get-collection.md similarity index 78% rename from docs/examples/database/get-collection.md rename to docs/examples/databases/get-collection.md index 0ca917a..279b3e2 100644 --- a/docs/examples/database/get-collection.md +++ b/docs/examples/databases/get-collection.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.getCollection('[COLLECTION_ID]'); +let promise = databases.getCollection('[COLLECTION_ID]'); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/get-document.md b/docs/examples/databases/get-document.md similarity index 76% rename from docs/examples/database/get-document.md rename to docs/examples/databases/get-document.md index 59ec5e4..b7bad7f 100644 --- a/docs/examples/database/get-document.md +++ b/docs/examples/databases/get-document.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.getDocument('[COLLECTION_ID]', '[DOCUMENT_ID]'); +let promise = databases.getDocument('[COLLECTION_ID]', '[DOCUMENT_ID]'); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/get-index.md b/docs/examples/databases/get-index.md similarity index 78% rename from docs/examples/database/get-index.md rename to docs/examples/databases/get-index.md index af51b34..8e6f1ba 100644 --- a/docs/examples/database/get-index.md +++ b/docs/examples/databases/get-index.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.getIndex('[COLLECTION_ID]', ''); +let promise = databases.getIndex('[COLLECTION_ID]', ''); promise.then(function (response) { console.log(response); diff --git a/docs/examples/databases/get.md b/docs/examples/databases/get.md new file mode 100644 index 0000000..b59b541 --- /dev/null +++ b/docs/examples/databases/get.md @@ -0,0 +1,21 @@ +import * as sdk from "https://deno.land/x/appwrite/mod.ts"; + +// Init SDK +let client = new sdk.Client(); + +let databases = new sdk.Databases(client, '[DATABASE_ID]'); + +client + .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint + .setProject('5df5acd0d48c2') // Your project ID + .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key +; + + +let promise = databases.get(); + +promise.then(function (response) { + console.log(response); +}, function (error) { + console.log(error); +}); \ No newline at end of file diff --git a/docs/examples/database/list-attributes.md b/docs/examples/databases/list-attributes.md similarity index 78% rename from docs/examples/database/list-attributes.md rename to docs/examples/databases/list-attributes.md index 65ad1b1..b0170df 100644 --- a/docs/examples/database/list-attributes.md +++ b/docs/examples/databases/list-attributes.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.listAttributes('[COLLECTION_ID]'); +let promise = databases.listAttributes('[COLLECTION_ID]'); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/list-collections.md b/docs/examples/databases/list-collections.md similarity index 80% rename from docs/examples/database/list-collections.md rename to docs/examples/databases/list-collections.md index 0bb6bf1..2ecfe3d 100644 --- a/docs/examples/database/list-collections.md +++ b/docs/examples/databases/list-collections.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.listCollections(); +let promise = databases.listCollections(); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/list-documents.md b/docs/examples/databases/list-documents.md similarity index 78% rename from docs/examples/database/list-documents.md rename to docs/examples/databases/list-documents.md index ead202a..85d1598 100644 --- a/docs/examples/database/list-documents.md +++ b/docs/examples/databases/list-documents.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.listDocuments('[COLLECTION_ID]'); +let promise = databases.listDocuments('[COLLECTION_ID]'); promise.then(function (response) { console.log(response); diff --git a/docs/examples/database/list-indexes.md b/docs/examples/databases/list-indexes.md similarity index 78% rename from docs/examples/database/list-indexes.md rename to docs/examples/databases/list-indexes.md index aff6da5..9ab7847 100644 --- a/docs/examples/database/list-indexes.md +++ b/docs/examples/databases/list-indexes.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.listIndexes('[COLLECTION_ID]'); +let promise = databases.listIndexes('[COLLECTION_ID]'); promise.then(function (response) { console.log(response); diff --git a/docs/examples/databases/list.md b/docs/examples/databases/list.md new file mode 100644 index 0000000..4862fd0 --- /dev/null +++ b/docs/examples/databases/list.md @@ -0,0 +1,21 @@ +import * as sdk from "https://deno.land/x/appwrite/mod.ts"; + +// Init SDK +let client = new sdk.Client(); + +let databases = new sdk.Databases(client, '[DATABASE_ID]'); + +client + .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint + .setProject('5df5acd0d48c2') // Your project ID + .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key +; + + +let promise = databases.list(); + +promise.then(function (response) { + console.log(response); +}, function (error) { + console.log(error); +}); \ No newline at end of file diff --git a/docs/examples/databases/update-collection.md b/docs/examples/databases/update-collection.md new file mode 100644 index 0000000..03ebe68 --- /dev/null +++ b/docs/examples/databases/update-collection.md @@ -0,0 +1,21 @@ +import * as sdk from "https://deno.land/x/appwrite/mod.ts"; + +// Init SDK +let client = new sdk.Client(); + +let databases = new sdk.Databases(client, '[DATABASE_ID]'); + +client + .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint + .setProject('5df5acd0d48c2') // Your project ID + .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key +; + + +let promise = databases.updateCollection('[COLLECTION_ID]', '[NAME]', 'document'); + +promise.then(function (response) { + console.log(response); +}, function (error) { + console.log(error); +}); \ No newline at end of file diff --git a/docs/examples/database/update-document.md b/docs/examples/databases/update-document.md similarity index 75% rename from docs/examples/database/update-document.md rename to docs/examples/databases/update-document.md index de19da7..25f46d5 100644 --- a/docs/examples/database/update-document.md +++ b/docs/examples/databases/update-document.md @@ -3,7 +3,7 @@ import * as sdk from "https://deno.land/x/appwrite/mod.ts"; // Init SDK let client = new sdk.Client(); -let database = new sdk.Database(client); +let databases = new sdk.Databases(client, '[DATABASE_ID]'); client .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint @@ -12,7 +12,7 @@ client ; -let promise = database.updateDocument('[COLLECTION_ID]', '[DOCUMENT_ID]', {}); +let promise = databases.updateDocument('[COLLECTION_ID]', '[DOCUMENT_ID]'); promise.then(function (response) { console.log(response); diff --git a/docs/examples/databases/update.md b/docs/examples/databases/update.md new file mode 100644 index 0000000..10970c2 --- /dev/null +++ b/docs/examples/databases/update.md @@ -0,0 +1,21 @@ +import * as sdk from "https://deno.land/x/appwrite/mod.ts"; + +// Init SDK +let client = new sdk.Client(); + +let databases = new sdk.Databases(client, '[DATABASE_ID]'); + +client + .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint + .setProject('5df5acd0d48c2') // Your project ID + .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key +; + + +let promise = databases.update('[NAME]'); + +promise.then(function (response) { + console.log(response); +}, function (error) { + console.log(error); +}); \ No newline at end of file diff --git a/docs/examples/users/update-email-verification.md b/docs/examples/users/update-email-verification.md new file mode 100644 index 0000000..caf02ab --- /dev/null +++ b/docs/examples/users/update-email-verification.md @@ -0,0 +1,21 @@ +import * as sdk from "https://deno.land/x/appwrite/mod.ts"; + +// Init SDK +let client = new sdk.Client(); + +let users = new sdk.Users(client); + +client + .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint + .setProject('5df5acd0d48c2') // Your project ID + .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key +; + + +let promise = users.updateEmailVerification('[USER_ID]', false); + +promise.then(function (response) { + console.log(response); +}, function (error) { + console.log(error); +}); \ No newline at end of file diff --git a/docs/examples/users/update-phone-verification.md b/docs/examples/users/update-phone-verification.md new file mode 100644 index 0000000..ecc8202 --- /dev/null +++ b/docs/examples/users/update-phone-verification.md @@ -0,0 +1,21 @@ +import * as sdk from "https://deno.land/x/appwrite/mod.ts"; + +// Init SDK +let client = new sdk.Client(); + +let users = new sdk.Users(client); + +client + .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint + .setProject('5df5acd0d48c2') // Your project ID + .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key +; + + +let promise = users.updatePhoneVerification('[USER_ID]', false); + +promise.then(function (response) { + console.log(response); +}, function (error) { + console.log(error); +}); \ No newline at end of file diff --git a/docs/examples/users/update-verification.md b/docs/examples/users/update-phone.md similarity index 88% rename from docs/examples/users/update-verification.md rename to docs/examples/users/update-phone.md index 4859b2c..ce5d356 100644 --- a/docs/examples/users/update-verification.md +++ b/docs/examples/users/update-phone.md @@ -12,7 +12,7 @@ client ; -let promise = users.updateVerification('[USER_ID]', false); +let promise = users.updatePhone('[USER_ID]', ''); promise.then(function (response) { console.log(response); diff --git a/mod.ts b/mod.ts index 21a6052..0c37187 100644 --- a/mod.ts +++ b/mod.ts @@ -1,9 +1,10 @@ import { Client } from "./src/client.ts"; import { Query } from "./src/query.ts"; +import { InputFile } from "./src/inputFile.ts"; import { AppwriteException } from "./src/exception.ts"; import { Account } from "./src/services/account.ts"; import { Avatars } from "./src/services/avatars.ts"; -import { Database } from "./src/services/database.ts"; +import { Databases } from "./src/services/databases.ts"; import { Functions } from "./src/services/functions.ts"; import { Health } from "./src/services/health.ts"; import { Locale } from "./src/services/locale.ts"; @@ -14,10 +15,11 @@ import { Users } from "./src/services/users.ts"; export { Client, Query, + InputFile, AppwriteException, Account, Avatars, - Database, + Databases, Functions, Health, Locale, diff --git a/src/client.ts b/src/client.ts index 2e4519b..fd918e7 100644 --- a/src/client.ts +++ b/src/client.ts @@ -11,8 +11,8 @@ export class Client { endpoint: string = 'https://appwrite.io/v1'; headers: Payload = { 'content-type': '', - 'x-sdk-version': 'appwrite:deno:4.0.0', - 'X-Appwrite-Response-Format':'0.14.0', + 'x-sdk-version': 'appwrite:deno:5.0.0', + 'X-Appwrite-Response-Format':'0.15.0', }; /** @@ -115,7 +115,13 @@ export class Client { const formData = new FormData(); const flatParams = this.flatten(params); for (const key in flatParams) { - formData.append(key, flatParams[key]); + const value = flatParams[key]; + + if(value && value.type && value.type === 'file') { + formData.append(key, value.file, value.filename); + } else { + formData.append(key, flatParams[key]); + } } body = formData; } else { diff --git a/src/inputFile.ts b/src/inputFile.ts new file mode 100644 index 0000000..7863a5a --- /dev/null +++ b/src/inputFile.ts @@ -0,0 +1,42 @@ +import { readableStreamFromReader } from "https://deno.land/std/streams/conversion.ts"; + +const _bufferToString = (buffer: Uint8Array): ReadableStream => { + return new ReadableStream({ + start(controller) { + controller.enqueue(buffer); + controller.close(); + } + }); +}; + +export class InputFile { + stream: ReadableStream; // Content of file as a stream + size: number; // Total final size of the file content + filename: string; // File name + + static fromPath = (filePath: string, filename: string): InputFile => { + const file = Deno.openSync(filePath); + const stream = readableStreamFromReader(file); + const size = Deno.statSync(filePath).size; + return new InputFile(stream, filename, size); + }; + + static fromBuffer = (buffer: Uint8Array, filename: string): InputFile => { + const stream = _bufferToString(buffer); + const size = buffer.byteLength; + return new InputFile(stream, filename, size); + }; + + static fromPlainText = (content: string, filename: string): InputFile => { + const buffer = new TextEncoder().encode(content); + const stream = _bufferToString(buffer); + const size = buffer.byteLength; + return new InputFile(stream, filename, size); + }; + + constructor(stream: ReadableStream, filename: string, size: number) { + this.stream = stream; + this.filename = filename; + this.size = size; + } +} \ No newline at end of file diff --git a/src/models.d.ts b/src/models.d.ts index 2ee2146..2e4ced0 100644 --- a/src/models.d.ts +++ b/src/models.d.ts @@ -25,6 +25,19 @@ export namespace Models { */ collections: Collection[]; } + /** + * Databases List + */ + export type DatabaseList = { + /** + * Total number of databases documents that matched your query. + */ + total: number; + /** + * List of databases. + */ + databases: Database[]; + } /** * Indexes List */ @@ -246,6 +259,19 @@ export namespace Models { */ phones: Phone[]; } + /** + * Database + */ + export type Database = { + /** + * Database ID. + */ + $id: string; + /** + * Database name. + */ + name: string; + } /** * Collection */ @@ -254,6 +280,14 @@ export namespace Models { * Collection ID. */ $id: string; + /** + * Collection creation date in Unix timestamp. + */ + $createdAt: number; + /** + * Collection update date in Unix timestamp. + */ + $updatedAt: number; /** * Collection read permissions. */ @@ -262,6 +296,10 @@ export namespace Models { * Collection write permissions. */ $write: string[]; + /** + * Database ID. + */ + databaseId: string; /** * Collection name. */ @@ -605,6 +643,14 @@ export namespace Models { * Collection ID. */ $collection: string; + /** + * Document creation date in Unix timestamp. + */ + $createdAt: number; + /** + * Document update date in Unix timestamp. + */ + $updatedAt: number; /** * Document read permissions. */ @@ -711,6 +757,14 @@ export namespace Models { * User ID. */ $id: string; + /** + * User creation date in Unix timestamp. + */ + $createdAt: number; + /** + * User update date in Unix timestamp. + */ + $updatedAt: number; /** * User name. */ @@ -731,10 +785,18 @@ export namespace Models { * User email address. */ email: string; + /** + * User phone number in E.164 format. + */ + phone: string; /** * Email verification status. */ emailVerification: boolean; + /** + * Phone verification status. + */ + phoneVerification: boolean; /** * User preferences as a key-value object */ @@ -753,6 +815,10 @@ export namespace Models { * Session ID. */ $id: string; + /** + * Session creation date in Unix timestamp. + */ + $createdAt: number; /** * User ID. */ @@ -854,6 +920,10 @@ export namespace Models { * Token ID. */ $id: string; + /** + * Token creation date in Unix timestamp. + */ + $createdAt: number; /** * User ID. */ @@ -912,6 +982,14 @@ export namespace Models { * Bucket ID. */ bucketId: string; + /** + * File creation date in Unix timestamp. + */ + $createdAt: number; + /** + * File update date in Unix timestamp. + */ + $updatedAt: number; /** * File read permissions. */ @@ -924,10 +1002,6 @@ export namespace Models { * File name. */ name: string; - /** - * File creation date in Unix timestamp. - */ - dateCreated: number; /** * File MD5 signature. */ @@ -957,6 +1031,14 @@ export namespace Models { * Bucket ID. */ $id: string; + /** + * Bucket creation date in Unix timestamp. + */ + $createdAt: number; + /** + * Bucket update date in Unix timestamp. + */ + $updatedAt: number; /** * File read permissions. */ @@ -969,14 +1051,6 @@ export namespace Models { * Bucket permission model. Possible values: `bucket` or `file` */ permission: string; - /** - * Bucket creation date in Unix timestamp. - */ - dateCreated: number; - /** - * Bucket update date in Unix timestamp. - */ - dateUpdated: number; /** * Bucket name. */ @@ -1011,13 +1085,17 @@ export namespace Models { */ $id: string; /** - * Team name. + * Team creation date in Unix timestamp. */ - name: string; + $createdAt: number; /** - * Team creation date in Unix timestamp. + * Team update date in Unix timestamp. + */ + $updatedAt: number; + /** + * Team name. */ - dateCreated: number; + name: string; /** * Total number of team members. */ @@ -1031,6 +1109,14 @@ export namespace Models { * Membership ID. */ $id: string; + /** + * Membership creation date in Unix timestamp. + */ + $createdAt: number; + /** + * Membership update date in Unix timestamp. + */ + $updatedAt: number; /** * User ID. */ @@ -1077,21 +1163,21 @@ export namespace Models { */ $id: string; /** - * Execution permissions. + * Function creation date in Unix timestamp. */ - execute: string[]; + $createdAt: number; /** - * Function name. + * Function update date in Unix timestamp. */ - name: string; + $updatedAt: number; /** - * Function creation date in Unix timestamp. + * Execution permissions. */ - dateCreated: number; + execute: string[]; /** - * Function update date in Unix timestamp. + * Function name. */ - dateUpdated: number; + name: string; /** * Function status. Possible values: `disabled`, `enabled` */ @@ -1170,6 +1256,14 @@ export namespace Models { * Deployment ID. */ $id: string; + /** + * Deployment creation date in Unix timestamp. + */ + $createdAt: number; + /** + * Deployment update date in Unix timestamp. + */ + $updatedAt: number; /** * Resource ID. */ @@ -1178,10 +1272,6 @@ export namespace Models { * Resource type. */ resourceType: string; - /** - * The deployment creation date in Unix timestamp. - */ - dateCreated: number; /** * The entrypoint file to use to execute the deployment code. */ @@ -1219,6 +1309,14 @@ export namespace Models { * Execution ID. */ $id: string; + /** + * Execution creation date in Unix timestamp. + */ + $createdAt: number; + /** + * Execution update date in Unix timestamp. + */ + $updatedAt: number; /** * Execution read permissions. */ @@ -1227,10 +1325,6 @@ export namespace Models { * Function ID. */ functionId: string; - /** - * The execution creation date in Unix timestamp. - */ - dateCreated: number; /** * The trigger that caused the function to execute. Possible values can be: `http`, `schedule`, or `event`. */ diff --git a/src/services/account.ts b/src/services/account.ts index bc8d0df..b96ff47 100644 --- a/src/services/account.ts +++ b/src/services/account.ts @@ -1,6 +1,7 @@ import { basename } from "https://deno.land/std@0.122.0/path/mod.ts"; import { Service } from '../service.ts'; import { Payload, Client } from '../client.ts'; +import { InputFile } from '../inputFile.ts'; import { AppwriteException } from '../exception.ts'; import type { Models } from '../models.d.ts'; @@ -149,6 +150,41 @@ export class Account extends Service { 'content-type': 'application/json', }, payload); } + /** + * Update Account Phone + * + * Update currently logged in user account phone number. After changing phone + * number, the user confirmation status will get reset. A new confirmation SMS + * is not sent automatically however you can use the phone confirmation + * endpoint again to send the confirmation SMS. + * + * @param {string} number + * @param {string} password + * @throws {AppwriteException} + * @returns {Promise} + */ + async updatePhone(number: string, password: string): Promise> { + if (typeof number === 'undefined') { + throw new AppwriteException('Missing required parameter: "number"'); + } + + if (typeof password === 'undefined') { + throw new AppwriteException('Missing required parameter: "password"'); + } + + let path = '/account/phone'; + let payload: Payload = {}; + + if (typeof number !== 'undefined') { + payload['number'] = number; + } + if (typeof password !== 'undefined') { + payload['password'] = password; + } + return await this.client.call('patch', path, { + 'content-type': 'application/json', + }, payload); + } /** * Get Account Preferences * @@ -417,8 +453,8 @@ export class Account extends Service { * should redirect the user back to your app and allow you to complete the * verification process by verifying both the **userId** and **secret** * parameters. Learn more about how to [complete the verification - * process](/docs/client/account#accountUpdateVerification). The verification - * link sent to the user's email address is valid for 7 days. + * process](/docs/client/account#accountUpdateEmailVerification). The + * verification link sent to the user's email address is valid for 7 days. * * Please note that in order to avoid a [Redirect * Attack](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.md), @@ -470,6 +506,63 @@ export class Account extends Service { let path = '/account/verification'; let payload: Payload = {}; + if (typeof userId !== 'undefined') { + payload['userId'] = userId; + } + if (typeof secret !== 'undefined') { + payload['secret'] = secret; + } + return await this.client.call('put', path, { + 'content-type': 'application/json', + }, payload); + } + /** + * Create Phone Verification + * + * Use this endpoint to send a verification message to your user's phone + * number to confirm they are the valid owners of that address. The provided + * secret should allow you to complete the verification process by verifying + * both the **userId** and **secret** parameters. Learn more about how to + * [complete the verification + * process](/docs/client/account#accountUpdatePhoneVerification). The + * verification link sent to the user's phone number is valid for 15 minutes. + * + * @throws {AppwriteException} + * @returns {Promise} + */ + async createPhoneVerification(): Promise { + let path = '/account/verification/phone'; + let payload: Payload = {}; + + return await this.client.call('post', path, { + 'content-type': 'application/json', + }, payload); + } + /** + * Create Phone Verification (confirmation) + * + * Use this endpoint to complete the user phone verification process. Use the + * **userId** and **secret** that were sent to your user's phone number to + * verify the user email ownership. If confirmed this route will return a 200 + * status code. + * + * @param {string} userId + * @param {string} secret + * @throws {AppwriteException} + * @returns {Promise} + */ + async updatePhoneVerification(userId: string, secret: string): Promise { + if (typeof userId === 'undefined') { + throw new AppwriteException('Missing required parameter: "userId"'); + } + + if (typeof secret === 'undefined') { + throw new AppwriteException('Missing required parameter: "secret"'); + } + + let path = '/account/verification/phone'; + let payload: Payload = {}; + if (typeof userId !== 'undefined') { payload['userId'] = userId; } diff --git a/src/services/avatars.ts b/src/services/avatars.ts index c7f555a..bfc1abf 100644 --- a/src/services/avatars.ts +++ b/src/services/avatars.ts @@ -1,6 +1,7 @@ import { basename } from "https://deno.land/std@0.122.0/path/mod.ts"; import { Service } from '../service.ts'; import { Payload, Client } from '../client.ts'; +import { InputFile } from '../inputFile.ts'; import { AppwriteException } from '../exception.ts'; import type { Models } from '../models.d.ts'; diff --git a/src/services/database.ts b/src/services/databases.ts similarity index 78% rename from src/services/database.ts rename to src/services/databases.ts index 91e5ccc..d93419c 100644 --- a/src/services/database.ts +++ b/src/services/databases.ts @@ -1,6 +1,7 @@ import { basename } from "https://deno.land/std@0.122.0/path/mod.ts"; import { Service } from '../service.ts'; import { Payload, Client } from '../client.ts'; +import { InputFile } from '../inputFile.ts'; import { AppwriteException } from '../exception.ts'; import type { Models } from '../models.d.ts'; @@ -12,14 +13,143 @@ export type UploadProgress = { chunksUploaded: number; } -export class Database extends Service { +export class Databases extends Service { + protected databaseId: string; + public setDatabaseId(databaseId: string): void + { + this.databaseId = databaseId; + } + public getDatabaseId(databaseId: string): string + { + return this.databaseId; + } + constructor(client: Client, databaseId:string) + { + super(client); + + this.databaseId = databaseId; + } /** - * List Collections + * List Databases + * + * @param {string} search + * @param {number} limit + * @param {number} offset + * @param {string} cursor + * @param {string} cursorDirection + * @param {string} orderType + * @throws {AppwriteException} + * @returns {Promise} + */ + async list(search?: string, limit?: number, offset?: number, cursor?: string, cursorDirection?: string, orderType?: string): Promise { + let path = '/databases'; + let payload: Payload = {}; + + if (typeof search !== 'undefined') { + payload['search'] = search; + } + + if (typeof limit !== 'undefined') { + payload['limit'] = limit; + } + + if (typeof offset !== 'undefined') { + payload['offset'] = offset; + } + + if (typeof cursor !== 'undefined') { + payload['cursor'] = cursor; + } + + if (typeof cursorDirection !== 'undefined') { + payload['cursorDirection'] = cursorDirection; + } + + if (typeof orderType !== 'undefined') { + payload['orderType'] = orderType; + } + + return await this.client.call('get', path, { + 'content-type': 'application/json', + }, payload); + } + /** + * Create Database * - * Get a list of all the user collections. You can use the query params to - * filter your results. On admin mode, this endpoint will return a list of all - * of the project's collections. [Learn more about different API - * modes](/docs/admin). + * @param {string} name + * @throws {AppwriteException} + * @returns {Promise} + */ + async create(name: string): Promise { + if (typeof name === 'undefined') { + throw new AppwriteException('Missing required parameter: "name"'); + } + + let path = '/databases'; + let payload: Payload = {}; + + if (typeof this.databaseId !== 'undefined') { + payload['databaseId'] = this.databaseId; + } + if (typeof name !== 'undefined') { + payload['name'] = name; + } + return await this.client.call('post', path, { + 'content-type': 'application/json', + }, payload); + } + /** + * Get Database + * + * @throws {AppwriteException} + * @returns {Promise} + */ + async get(): Promise { + let path = '/databases/{databaseId}'.replace('{databaseId}', this.databaseId); + let payload: Payload = {}; + + return await this.client.call('get', path, { + 'content-type': 'application/json', + }, payload); + } + /** + * Update Database + * + * @param {string} name + * @throws {AppwriteException} + * @returns {Promise} + */ + async update(name: string): Promise { + if (typeof name === 'undefined') { + throw new AppwriteException('Missing required parameter: "name"'); + } + + let path = '/databases/{databaseId}'.replace('{databaseId}', this.databaseId); + let payload: Payload = {}; + + if (typeof name !== 'undefined') { + payload['name'] = name; + } + return await this.client.call('put', path, { + 'content-type': 'application/json', + }, payload); + } + /** + * Delete Database + * + * @throws {AppwriteException} + * @returns {Promise} + */ + async delete(): Promise { + let path = '/databases/{databaseId}'.replace('{databaseId}', this.databaseId); + let payload: Payload = {}; + + return await this.client.call('delete', path, { + 'content-type': 'application/json', + }, payload); + } + /** + * List Collections * * @param {string} search * @param {number} limit @@ -31,7 +161,7 @@ export class Database extends Service { * @returns {Promise} */ async listCollections(search?: string, limit?: number, offset?: number, cursor?: string, cursorDirection?: string, orderType?: string): Promise { - let path = '/database/collections'; + let path = '/databases/{databaseId}/collections'.replace('{databaseId}', this.databaseId); let payload: Payload = {}; if (typeof search !== 'undefined') { @@ -65,8 +195,6 @@ export class Database extends Service { /** * Create Collection * - * Create a new Collection. - * * @param {string} collectionId * @param {string} name * @param {string} permission @@ -96,7 +224,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "write"'); } - let path = '/database/collections'; + let path = '/databases/{databaseId}/collections'.replace('{databaseId}', this.databaseId); let payload: Payload = {}; if (typeof collectionId !== 'undefined') { @@ -121,9 +249,6 @@ export class Database extends Service { /** * Get Collection * - * Get a collection by its unique ID. This endpoint response returns a JSON - * object with the collection metadata. - * * @param {string} collectionId * @throws {AppwriteException} * @returns {Promise} @@ -133,7 +258,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "collectionId"'); } - let path = '/database/collections/{collectionId}'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; return await this.client.call('get', path, { @@ -143,8 +268,6 @@ export class Database extends Service { /** * Update Collection * - * Update a collection by its unique ID. - * * @param {string} collectionId * @param {string} name * @param {string} permission @@ -167,7 +290,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "permission"'); } - let path = '/database/collections/{collectionId}'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof name !== 'undefined') { @@ -192,9 +315,6 @@ export class Database extends Service { /** * Delete Collection * - * Delete a collection by its unique ID. Only users with write permissions - * have access to delete this resource. - * * @param {string} collectionId * @throws {AppwriteException} * @returns {Promise} @@ -204,7 +324,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "collectionId"'); } - let path = '/database/collections/{collectionId}'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; return await this.client.call('delete', path, { @@ -223,7 +343,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "collectionId"'); } - let path = '/database/collections/{collectionId}/attributes'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/attributes'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; return await this.client.call('get', path, { @@ -233,9 +353,6 @@ export class Database extends Service { /** * Create Boolean Attribute * - * Create a boolean attribute. - * - * * @param {string} collectionId * @param {string} key * @param {boolean} required @@ -257,7 +374,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "required"'); } - let path = '/database/collections/{collectionId}/attributes/boolean'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/attributes/boolean'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof key !== 'undefined') { @@ -279,9 +396,6 @@ export class Database extends Service { /** * Create Email Attribute * - * Create an email attribute. - * - * * @param {string} collectionId * @param {string} key * @param {boolean} required @@ -303,7 +417,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "required"'); } - let path = '/database/collections/{collectionId}/attributes/email'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/attributes/email'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof key !== 'undefined') { @@ -351,7 +465,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "required"'); } - let path = '/database/collections/{collectionId}/attributes/enum'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/attributes/enum'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof key !== 'undefined') { @@ -376,10 +490,6 @@ export class Database extends Service { /** * Create Float Attribute * - * Create a float attribute. Optionally, minimum and maximum values can be - * provided. - * - * * @param {string} collectionId * @param {string} key * @param {boolean} required @@ -403,7 +513,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "required"'); } - let path = '/database/collections/{collectionId}/attributes/float'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/attributes/float'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof key !== 'undefined') { @@ -431,10 +541,6 @@ export class Database extends Service { /** * Create Integer Attribute * - * Create an integer attribute. Optionally, minimum and maximum values can be - * provided. - * - * * @param {string} collectionId * @param {string} key * @param {boolean} required @@ -458,7 +564,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "required"'); } - let path = '/database/collections/{collectionId}/attributes/integer'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/attributes/integer'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof key !== 'undefined') { @@ -486,9 +592,6 @@ export class Database extends Service { /** * Create IP Address Attribute * - * Create IP address attribute. - * - * * @param {string} collectionId * @param {string} key * @param {boolean} required @@ -510,7 +613,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "required"'); } - let path = '/database/collections/{collectionId}/attributes/ip'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/attributes/ip'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof key !== 'undefined') { @@ -532,9 +635,6 @@ export class Database extends Service { /** * Create String Attribute * - * Create a string attribute. - * - * * @param {string} collectionId * @param {string} key * @param {number} size @@ -561,7 +661,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "required"'); } - let path = '/database/collections/{collectionId}/attributes/string'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/attributes/string'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof key !== 'undefined') { @@ -586,9 +686,6 @@ export class Database extends Service { /** * Create URL Attribute * - * Create a URL attribute. - * - * * @param {string} collectionId * @param {string} key * @param {boolean} required @@ -610,7 +707,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "required"'); } - let path = '/database/collections/{collectionId}/attributes/url'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/attributes/url'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof key !== 'undefined') { @@ -646,7 +743,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "key"'); } - let path = '/database/collections/{collectionId}/attributes/{key}'.replace('{collectionId}', collectionId).replace('{key}', key); + let path = '/databases/{databaseId}/collections/{collectionId}/attributes/{key}'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId).replace('{key}', key); let payload: Payload = {}; return await this.client.call('get', path, { @@ -670,7 +767,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "key"'); } - let path = '/database/collections/{collectionId}/attributes/{key}'.replace('{collectionId}', collectionId).replace('{key}', key); + let path = '/databases/{databaseId}/collections/{collectionId}/attributes/{key}'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId).replace('{key}', key); let payload: Payload = {}; return await this.client.call('delete', path, { @@ -680,11 +777,6 @@ export class Database extends Service { /** * List Documents * - * Get a list of all the user documents. You can use the query params to - * filter your results. On admin mode, this endpoint will return a list of all - * of the project's documents. [Learn more about different API - * modes](/docs/admin). - * * @param {string} collectionId * @param {string[]} queries * @param {number} limit @@ -701,7 +793,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "collectionId"'); } - let path = '/database/collections/{collectionId}/documents'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/documents'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof queries !== 'undefined') { @@ -739,11 +831,6 @@ export class Database extends Service { /** * Create Document * - * Create a new Document. Before using this route, you should create a new - * collection resource using either a [server - * integration](/docs/server/database#databaseCreateCollection) API or - * directly from your database console. - * * @param {string} collectionId * @param {string} documentId * @param {object} data @@ -765,7 +852,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "data"'); } - let path = '/database/collections/{collectionId}/documents'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/documents'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof documentId !== 'undefined') { @@ -787,9 +874,6 @@ export class Database extends Service { /** * Get Document * - * Get a document by its unique ID. This endpoint response returns a JSON - * object with the document data. - * * @param {string} collectionId * @param {string} documentId * @throws {AppwriteException} @@ -804,7 +888,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "documentId"'); } - let path = '/database/collections/{collectionId}/documents/{documentId}'.replace('{collectionId}', collectionId).replace('{documentId}', documentId); + let path = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId).replace('{documentId}', documentId); let payload: Payload = {}; return await this.client.call('get', path, { @@ -814,9 +898,6 @@ export class Database extends Service { /** * Update Document * - * Update a document by its unique ID. Using the patch method you can pass - * only specific fields that will get updated. - * * @param {string} collectionId * @param {string} documentId * @param {object} data @@ -825,7 +906,7 @@ export class Database extends Service { * @throws {AppwriteException} * @returns {Promise} */ - async updateDocument(collectionId: string, documentId: string, data: object, read?: string[], write?: string[]): Promise { + async updateDocument(collectionId: string, documentId: string, data?: object, read?: string[], write?: string[]): Promise { if (typeof collectionId === 'undefined') { throw new AppwriteException('Missing required parameter: "collectionId"'); } @@ -834,11 +915,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "documentId"'); } - if (typeof data === 'undefined') { - throw new AppwriteException('Missing required parameter: "data"'); - } - - let path = '/database/collections/{collectionId}/documents/{documentId}'.replace('{collectionId}', collectionId).replace('{documentId}', documentId); + let path = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId).replace('{documentId}', documentId); let payload: Payload = {}; if (typeof data !== 'undefined') { @@ -857,8 +934,6 @@ export class Database extends Service { /** * Delete Document * - * Delete a document by its unique ID. - * * @param {string} collectionId * @param {string} documentId * @throws {AppwriteException} @@ -873,7 +948,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "documentId"'); } - let path = '/database/collections/{collectionId}/documents/{documentId}'.replace('{collectionId}', collectionId).replace('{documentId}', documentId); + let path = '/databases/{databaseId}/collections/{collectionId}/documents/{documentId}'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId).replace('{documentId}', documentId); let payload: Payload = {}; return await this.client.call('delete', path, { @@ -892,7 +967,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "collectionId"'); } - let path = '/database/collections/{collectionId}/indexes'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/indexes'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; return await this.client.call('get', path, { @@ -927,7 +1002,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "attributes"'); } - let path = '/database/collections/{collectionId}/indexes'.replace('{collectionId}', collectionId); + let path = '/databases/{databaseId}/collections/{collectionId}/indexes'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId); let payload: Payload = {}; if (typeof key !== 'undefined') { @@ -963,7 +1038,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "key"'); } - let path = '/database/collections/{collectionId}/indexes/{key}'.replace('{collectionId}', collectionId).replace('{key}', key); + let path = '/databases/{databaseId}/collections/{collectionId}/indexes/{key}'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId).replace('{key}', key); let payload: Payload = {}; return await this.client.call('get', path, { @@ -987,7 +1062,7 @@ export class Database extends Service { throw new AppwriteException('Missing required parameter: "key"'); } - let path = '/database/collections/{collectionId}/indexes/{key}'.replace('{collectionId}', collectionId).replace('{key}', key); + let path = '/databases/{databaseId}/collections/{collectionId}/indexes/{key}'.replace('{databaseId}', this.databaseId).replace('{collectionId}', collectionId).replace('{key}', key); let payload: Payload = {}; return await this.client.call('delete', path, { diff --git a/src/services/functions.ts b/src/services/functions.ts index fa123a3..9357eb3 100644 --- a/src/services/functions.ts +++ b/src/services/functions.ts @@ -1,6 +1,7 @@ import { basename } from "https://deno.land/std@0.122.0/path/mod.ts"; import { Service } from '../service.ts'; import { Payload, Client } from '../client.ts'; +import { InputFile } from '../inputFile.ts'; import { AppwriteException } from '../exception.ts'; import type { Models } from '../models.d.ts'; @@ -305,12 +306,12 @@ export class Functions extends Service { * * @param {string} functionId * @param {string} entrypoint - * @param {string} code + * @param {InputFile} code * @param {boolean} activate * @throws {AppwriteException} * @returns {Promise} */ - async createDeployment(functionId: string, entrypoint: string, code: string, activate: boolean, onProgress = (progress: UploadProgress) => {}): Promise { + async createDeployment(functionId: string, entrypoint: string, code: InputFile, activate: boolean, onProgress = (progress: UploadProgress) => {}): Promise { if (typeof functionId === 'undefined') { throw new AppwriteException('Missing required parameter: "functionId"'); } @@ -334,94 +335,96 @@ export class Functions extends Service { payload['entrypoint'] = entrypoint; } if (typeof code !== 'undefined') { - payload['code'] = code.toString(); + payload['code'] = code; } if (typeof activate !== 'undefined') { payload['activate'] = activate.toString(); } - const {size: size} = await Deno.stat(code); - if (size <= Client.CHUNK_SIZE) { - payload['code'] = new File([await Deno.readFile(code)], basename(code)); + const size = code.size; + + const headers: { [header: string]: string } = { + 'content-type': 'multipart/form-data', + }; - return await this.client.call('post', path, { - 'content-type': 'multipart/form-data', - }, payload); - } else { - const stream = await Deno.open(code, { read: true }); + let id: string | undefined = undefined; + let response: any = undefined; - let id = undefined; - let response = undefined; + let chunksUploaded = 0; - let counter = 0; - const totalCounters = Math.ceil(size / Client.CHUNK_SIZE); - const headers: { [header: string]: string } = { - 'content-type': 'multipart/form-data', - }; + let currentChunk = 1; + let currentPosition = 0; + let uploadableChunk = new Uint8Array(Client.CHUNK_SIZE); + const uploadChunk = async (lastUpload = false) => { + if(currentChunk <= chunksUploaded) { + return; + } - for (counter; counter < totalCounters; counter++) { - const start = (counter * Client.CHUNK_SIZE); - const end = Math.min((((counter * Client.CHUNK_SIZE) + Client.CHUNK_SIZE) - 1), size); - - headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size + const start = ((currentChunk - 1) * Client.CHUNK_SIZE); + const end = start + currentPosition; - if (id) { - headers['x-appwrite-id'] = id; - } - - let totalBuffer = new Uint8Array(Client.CHUNK_SIZE); - let lastBufferIndex = -1; - - for (let blockIndex = 0; blockIndex < Client.CHUNK_SIZE / Client.DENO_READ_CHUNK_SIZE; blockIndex++) { - const buf = new Uint8Array(Client.DENO_READ_CHUNK_SIZE); - const cursorPosition = await Deno.seek(stream.rid, start + (blockIndex * 16384), Deno.SeekMode.Start); - const numberOfBytesRead = await Deno.read(stream.rid, buf); - - if (!numberOfBytesRead) { - break; - } - - for (let byteIndex = 0; byteIndex < Client.DENO_READ_CHUNK_SIZE; byteIndex++) { - totalBuffer[(blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex] = buf[byteIndex]; - - if(buf[byteIndex] !== 0) { - lastBufferIndex = (blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex; - } - } - } + if(!lastUpload || currentChunk !== 1) { + headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size; + } - // Shrink empty bytes - if(lastBufferIndex !== -1) { - const newTotalBuffer = new Uint8Array(lastBufferIndex + 1); - for(let index = 0; index <= lastBufferIndex; index++) { - newTotalBuffer[index] = totalBuffer[index]; - } - totalBuffer = newTotalBuffer; + let uploadableChunkTrimmed: Uint8Array; + + if(currentPosition + 1 >= Client.CHUNK_SIZE) { + uploadableChunkTrimmed = uploadableChunk; + } else { + uploadableChunkTrimmed = new Uint8Array(currentPosition); + for(let i = 0; i <= currentPosition; i++) { + uploadableChunkTrimmed[i] = uploadableChunk[i]; } - - payload['code'] = new File([totalBuffer], basename(code)); + } - response = await this.client.call('post', path, headers, payload); + if (id) { + headers['x-appwrite-id'] = id; + } - if (!id) { - id = response['$id']; - } + payload['code'] = { type: 'file', file: new File([uploadableChunkTrimmed], code.filename), filename: code.filename }; - if (onProgress !== null) { - onProgress({ - $id: response['$id'], - progress: Math.min((counter+1) * Client.CHUNK_SIZE, size) / size * 100, - sizeUploaded: end+1, - chunksTotal: response['chunksTotal'], - chunksUploaded: response['chunksUploaded'] - }); - } + response = await this.client.call('post', path, headers, payload); + + if (!id) { + id = response['$id']; } - return response; + if (onProgress !== null) { + onProgress({ + $id: response['$id'], + progress: Math.min((currentChunk) * Client.CHUNK_SIZE, size) / size * 100, + sizeUploaded: end+1, + chunksTotal: response['chunksTotal'], + chunksUploaded: response['chunksUploaded'] + }); + } + + uploadableChunk = new Uint8Array(Client.CHUNK_SIZE); + currentPosition = 0; + currentChunk++; } + + for await (const chunk of code.stream) { + let i = 0; + for(const b of chunk) { + uploadableChunk[currentPosition] = chunk[i]; + + if(currentPosition + 1 >= Client.CHUNK_SIZE) { + await uploadChunk(); + currentPosition--; + } + + i++; + currentPosition++; + } + } + + await uploadChunk(true); + + return response; } /** * Get Deployment diff --git a/src/services/health.ts b/src/services/health.ts index 1f2fb81..cc6774b 100644 --- a/src/services/health.ts +++ b/src/services/health.ts @@ -1,6 +1,7 @@ import { basename } from "https://deno.land/std@0.122.0/path/mod.ts"; import { Service } from '../service.ts'; import { Payload, Client } from '../client.ts'; +import { InputFile } from '../inputFile.ts'; import { AppwriteException } from '../exception.ts'; import type { Models } from '../models.d.ts'; diff --git a/src/services/locale.ts b/src/services/locale.ts index 872d664..bc0ca86 100644 --- a/src/services/locale.ts +++ b/src/services/locale.ts @@ -1,6 +1,7 @@ import { basename } from "https://deno.land/std@0.122.0/path/mod.ts"; import { Service } from '../service.ts'; import { Payload, Client } from '../client.ts'; +import { InputFile } from '../inputFile.ts'; import { AppwriteException } from '../exception.ts'; import type { Models } from '../models.d.ts'; diff --git a/src/services/storage.ts b/src/services/storage.ts index fb7e815..d420bc7 100644 --- a/src/services/storage.ts +++ b/src/services/storage.ts @@ -1,6 +1,7 @@ import { basename } from "https://deno.land/std@0.122.0/path/mod.ts"; import { Service } from '../service.ts'; import { Payload, Client } from '../client.ts'; +import { InputFile } from '../inputFile.ts'; import { AppwriteException } from '../exception.ts'; import type { Models } from '../models.d.ts'; @@ -313,13 +314,13 @@ export class Storage extends Service { * * @param {string} bucketId * @param {string} fileId - * @param {string} file + * @param {InputFile} file * @param {string[]} read * @param {string[]} write * @throws {AppwriteException} * @returns {Promise} */ - async createFile(bucketId: string, fileId: string, file: string, read?: string[], write?: string[], onProgress = (progress: UploadProgress) => {}): Promise { + async createFile(bucketId: string, fileId: string, file: InputFile, read?: string[], write?: string[], onProgress = (progress: UploadProgress) => {}): Promise { if (typeof bucketId === 'undefined') { throw new AppwriteException('Missing required parameter: "bucketId"'); } @@ -339,7 +340,7 @@ export class Storage extends Service { payload['fileId'] = fileId; } if (typeof file !== 'undefined') { - payload['file'] = file.toString(); + payload['file'] = file; } if (typeof read !== 'undefined') { payload['read'] = read; @@ -347,96 +348,98 @@ export class Storage extends Service { if (typeof write !== 'undefined') { payload['write'] = write; } - const {size: size} = await Deno.stat(file); - if (size <= Client.CHUNK_SIZE) { - payload['file'] = new File([await Deno.readFile(file)], basename(file)); + const size = file.size; + + const headers: { [header: string]: string } = { + 'content-type': 'multipart/form-data', + }; - return await this.client.call('post', path, { - 'content-type': 'multipart/form-data', - }, payload); - } else { - const stream = await Deno.open(file, { read: true }); + let id: string | undefined = undefined; + let response: any = undefined; - let id = undefined; - let response = undefined; + let chunksUploaded = 0; - let counter = 0; - const totalCounters = Math.ceil(size / Client.CHUNK_SIZE); + if(fileId != 'unique()') { + try { + response = await this.client.call('get', path + '/' + fileId, headers); + chunksUploaded = response.chunksUploaded; + } catch(e) { + } + } - const headers: { [header: string]: string } = { - 'content-type': 'multipart/form-data', - }; + let currentChunk = 1; + let currentPosition = 0; + let uploadableChunk = new Uint8Array(Client.CHUNK_SIZE); - if(fileId != 'unique()') { - try { - response = await this.client.call('get', path + '/' + fileId, headers); - counter = response.chunksUploaded; - } catch(e) { - } + const uploadChunk = async (lastUpload = false) => { + if(currentChunk <= chunksUploaded) { + return; } - for (counter; counter < totalCounters; counter++) { - const start = (counter * Client.CHUNK_SIZE); - const end = Math.min((((counter * Client.CHUNK_SIZE) + Client.CHUNK_SIZE) - 1), size); - - headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size + const start = ((currentChunk - 1) * Client.CHUNK_SIZE); + const end = start + currentPosition; - if (id) { - headers['x-appwrite-id'] = id; - } - - let totalBuffer = new Uint8Array(Client.CHUNK_SIZE); - let lastBufferIndex = -1; - - for (let blockIndex = 0; blockIndex < Client.CHUNK_SIZE / Client.DENO_READ_CHUNK_SIZE; blockIndex++) { - const buf = new Uint8Array(Client.DENO_READ_CHUNK_SIZE); - const cursorPosition = await Deno.seek(stream.rid, start + (blockIndex * 16384), Deno.SeekMode.Start); - const numberOfBytesRead = await Deno.read(stream.rid, buf); - - if (!numberOfBytesRead) { - break; - } - - for (let byteIndex = 0; byteIndex < Client.DENO_READ_CHUNK_SIZE; byteIndex++) { - totalBuffer[(blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex] = buf[byteIndex]; - - if(buf[byteIndex] !== 0) { - lastBufferIndex = (blockIndex * Client.DENO_READ_CHUNK_SIZE) + byteIndex; - } - } - } + if(!lastUpload || currentChunk !== 1) { + headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size; + } - // Shrink empty bytes - if(lastBufferIndex !== -1) { - const newTotalBuffer = new Uint8Array(lastBufferIndex + 1); - for(let index = 0; index <= lastBufferIndex; index++) { - newTotalBuffer[index] = totalBuffer[index]; - } - totalBuffer = newTotalBuffer; + let uploadableChunkTrimmed: Uint8Array; + + if(currentPosition + 1 >= Client.CHUNK_SIZE) { + uploadableChunkTrimmed = uploadableChunk; + } else { + uploadableChunkTrimmed = new Uint8Array(currentPosition); + for(let i = 0; i <= currentPosition; i++) { + uploadableChunkTrimmed[i] = uploadableChunk[i]; } - - payload['file'] = new File([totalBuffer], basename(file)); + } - response = await this.client.call('post', path, headers, payload); + if (id) { + headers['x-appwrite-id'] = id; + } - if (!id) { - id = response['$id']; - } + payload['file'] = { type: 'file', file: new File([uploadableChunkTrimmed], file.filename), filename: file.filename }; - if (onProgress !== null) { - onProgress({ - $id: response['$id'], - progress: Math.min((counter+1) * Client.CHUNK_SIZE, size) / size * 100, - sizeUploaded: end+1, - chunksTotal: response['chunksTotal'], - chunksUploaded: response['chunksUploaded'] - }); - } + response = await this.client.call('post', path, headers, payload); + + if (!id) { + id = response['$id']; } - return response; + if (onProgress !== null) { + onProgress({ + $id: response['$id'], + progress: Math.min((currentChunk) * Client.CHUNK_SIZE, size) / size * 100, + sizeUploaded: end+1, + chunksTotal: response['chunksTotal'], + chunksUploaded: response['chunksUploaded'] + }); + } + + uploadableChunk = new Uint8Array(Client.CHUNK_SIZE); + currentPosition = 0; + currentChunk++; + } + + for await (const chunk of file.stream) { + let i = 0; + for(const b of chunk) { + uploadableChunk[currentPosition] = chunk[i]; + + if(currentPosition + 1 >= Client.CHUNK_SIZE) { + await uploadChunk(); + currentPosition--; + } + + i++; + currentPosition++; + } } + + await uploadChunk(true); + + return response; } /** * Get File diff --git a/src/services/teams.ts b/src/services/teams.ts index 1e3cd9b..9b7f0a7 100644 --- a/src/services/teams.ts +++ b/src/services/teams.ts @@ -1,6 +1,7 @@ import { basename } from "https://deno.land/std@0.122.0/path/mod.ts"; import { Service } from '../service.ts'; import { Payload, Client } from '../client.ts'; +import { InputFile } from '../inputFile.ts'; import { AppwriteException } from '../exception.ts'; import type { Models } from '../models.d.ts'; diff --git a/src/services/users.ts b/src/services/users.ts index ae10827..eb36c4b 100644 --- a/src/services/users.ts +++ b/src/services/users.ts @@ -1,6 +1,7 @@ import { basename } from "https://deno.land/std@0.122.0/path/mod.ts"; import { Service } from '../service.ts'; import { Payload, Client } from '../client.ts'; +import { InputFile } from '../inputFile.ts'; import { AppwriteException } from '../exception.ts'; import type { Models } from '../models.d.ts'; @@ -289,6 +290,35 @@ export class Users extends Service { 'content-type': 'application/json', }, payload); } + /** + * Update Phone + * + * Update the user phone by its unique ID. + * + * @param {string} userId + * @param {string} number + * @throws {AppwriteException} + * @returns {Promise} + */ + async updatePhone(userId: string, number: string): Promise> { + if (typeof userId === 'undefined') { + throw new AppwriteException('Missing required parameter: "userId"'); + } + + if (typeof number === 'undefined') { + throw new AppwriteException('Missing required parameter: "number"'); + } + + let path = '/users/{userId}/phone'.replace('{userId}', userId); + let payload: Payload = {}; + + if (typeof number !== 'undefined') { + payload['number'] = number; + } + return await this.client.call('patch', path, { + 'content-type': 'application/json', + }, payload); + } /** * Get User Preferences * @@ -449,7 +479,7 @@ export class Users extends Service { * @throws {AppwriteException} * @returns {Promise} */ - async updateVerification(userId: string, emailVerification: boolean): Promise> { + async updateEmailVerification(userId: string, emailVerification: boolean): Promise> { if (typeof userId === 'undefined') { throw new AppwriteException('Missing required parameter: "userId"'); } @@ -468,4 +498,33 @@ export class Users extends Service { 'content-type': 'application/json', }, payload); } + /** + * Update Phone Verification + * + * Update the user phone verification status by its unique ID. + * + * @param {string} userId + * @param {boolean} phoneVerification + * @throws {AppwriteException} + * @returns {Promise} + */ + async updatePhoneVerification(userId: string, phoneVerification: boolean): Promise> { + if (typeof userId === 'undefined') { + throw new AppwriteException('Missing required parameter: "userId"'); + } + + if (typeof phoneVerification === 'undefined') { + throw new AppwriteException('Missing required parameter: "phoneVerification"'); + } + + let path = '/users/{userId}/verification/phone'.replace('{userId}', userId); + let payload: Payload = {}; + + if (typeof phoneVerification !== 'undefined') { + payload['phoneVerification'] = phoneVerification; + } + return await this.client.call('patch', path, { + 'content-type': 'application/json', + }, payload); + } } \ No newline at end of file