Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: for appwrite 1.4.x #74

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Appwrite Web SDK

![License](https://img.shields.io/github/license/appwrite/sdk-for-web.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-1.4.0-blue.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-1.4.2-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 @@ -33,7 +33,7 @@ import { Client, Account } from "appwrite";
To install with a CDN (content delivery network) add the following scripts to the bottom of your <body> tag, but before you use any Appwrite services:

```html
<script src="https://cdn.jsdelivr.net/npm/appwrite@12.0.0"></script>
<script src="https://cdn.jsdelivr.net/npm/appwrite@13.0.0"></script>
```


Expand Down
2 changes: 1 addition & 1 deletion docs/examples/teams/create-membership.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ client
.setProject('5df5acd0d48c2') // Your project ID
;

const promise = teams.createMembership('[TEAM_ID]', [], 'https://example.com');
const promise = teams.createMembership('[TEAM_ID]', []);

promise.then(function (response) {
console.log(response); // Success
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "appwrite",
"homepage": "https://appwrite.io/support",
"description": "Appwrite is an open-source self-hosted backend server that abstract and simplify complex and repetitive development tasks behind a very simple REST API",
"version": "12.0.0",
"version": "13.0.0",
"license": "BSD-3-Clause",
"main": "dist/cjs/sdk.js",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Client {
'x-sdk-name': 'Web',
'x-sdk-platform': 'client',
'x-sdk-language': 'web',
'x-sdk-version': '12.0.0',
'x-sdk-version': '13.0.0',
'X-Appwrite-Response-Format': '1.4.0',
};

Expand Down
78 changes: 72 additions & 6 deletions src/role.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,100 @@
/**
* Helper class to generate role strings for `Permission`.
*/
export class Role {

/**
* Grants access to anyone.
*
* This includes authenticated and unauthenticated users.
*
* @returns {string}
*/
public static any(): string {
return 'any'
}

/**
* Grants access to a specific user by user ID.
*
* You can optionally pass verified or unverified for
* `status` to target specific types of users.
*
* @param {string} id
* @param {string} status
* @returns {string}
*/
public static user(id: string, status: string = ''): string {
if(status === '') {
if (status === '') {
return `user:${id}`
}
return `user:${id}/${status}`
}


/**
* Grants access to any authenticated or anonymous user.
*
* You can optionally pass verified or unverified for
* `status` to target specific types of users.
*
* @param {string} status
* @returns {string}
*/
public static users(status: string = ''): string {
if(status === '') {
if (status === '') {
return 'users'
}
return `users/${status}`
}


/**
* Grants access to any guest user without a session.
*
* Authenticated users don't have access to this role.
*
* @returns {string}
*/
public static guests(): string {
return 'guests'
}


/**
* Grants access to a team by team ID.
*
* You can optionally pass a role for `role` to target
* team members with the specified role.
*
* @param {string} id
* @param {string} role
* @returns {string}
*/
public static team(id: string, role: string = ''): string {
if(role === '') {
if (role === '') {
return `team:${id}`
}
return `team:${id}/${role}`
}

/**
* Grants access to a specific member of a team.
*
* When the member is removed from the team, they will
* no longer have access.
*
* @param {string} id
* @returns {string}
*/
public static member(id: string): string {
return `member:${id}`
}

/**
* Grants access to a user with the specified label.
*
* @param {string} name
* @returns {string}
*/
public static label(name: string): string {
return `label:${name}`
}
}
42 changes: 16 additions & 26 deletions src/services/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,57 +111,47 @@ export class Storage extends Service {

if (size <= Service.CHUNK_SIZE) {
return await this.client.call('post', uri, {

'content-type': 'multipart/form-data',
}, payload);
}
let id = undefined;
let response = undefined;

const headers: { [header: string]: string } = {
const apiHeaders: { [header: string]: string } = {
'content-type': 'multipart/form-data',
}

let counter = 0;
const totalCounters = Math.ceil(size / Service.CHUNK_SIZE);
let offset = 0;
let response = undefined;
if(fileId != 'unique()') {
try {
response = await this.client.call('GET', new URL(this.client.config.endpoint + apiPath + '/' + fileId), headers);
counter = response.chunksUploaded;
response = await this.client.call('GET', new URL(this.client.config.endpoint + apiPath + '/' + fileId), apiHeaders);
offset = response.chunksUploaded * Service.CHUNK_SIZE;
} catch(e) {
}
}

for (counter; counter < totalCounters; counter++) {
const start = (counter * Service.CHUNK_SIZE);
const end = Math.min((((counter * Service.CHUNK_SIZE) + Service.CHUNK_SIZE) - 1), size);

headers['content-range'] = 'bytes ' + start + '-' + end + '/' + size
while (offset < size) {
let end = Math.min(offset + Service.CHUNK_SIZE - 1, size - 1);

if (id) {
headers['x-appwrite-id'] = id;
apiHeaders['content-range'] = 'bytes ' + offset + '-' + end + '/' + size;
if (response && response.$id) {
apiHeaders['x-appwrite-id'] = response.$id;
}

const stream = file.slice(start, end + 1);
payload['file'] = new File([stream], file.name);

response = await this.client.call('post', uri, headers, payload);

if (!id) {
id = response['$id'];
}
const chunk = file.slice(offset, end + 1);
payload['file'] = new File([chunk], file.name);
response = await this.client.call('post', uri, apiHeaders, payload);

if (onProgress) {
onProgress({
$id: response.$id,
progress: Math.min((counter + 1) * Service.CHUNK_SIZE - 1, size) / size * 100,
sizeUploaded: end,
progress: (offset / size) * 100,
sizeUploaded: offset,
chunksTotal: response.chunksTotal,
chunksUploaded: response.chunksUploaded
});
}
offset += Service.CHUNK_SIZE;
}

return response;
}

Expand Down
8 changes: 2 additions & 6 deletions src/services/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,15 @@ export class Teams extends Service {
*
* @param {string} teamId
* @param {string[]} roles
* @param {string} url
* @param {string} email
* @param {string} userId
* @param {string} phone
* @param {string} url
* @param {string} name
* @throws {AppwriteException}
* @returns {Promise}
*/
async createMembership(teamId: string, roles: string[], url: string, email?: string, userId?: string, phone?: string, name?: string): Promise<Models.Membership> {
async createMembership(teamId: string, roles: string[], email?: string, userId?: string, phone?: string, url?: string, name?: string): Promise<Models.Membership> {
if (typeof teamId === 'undefined') {
throw new AppwriteException('Missing required parameter: "teamId"');
}
Expand All @@ -239,10 +239,6 @@ export class Teams extends Service {
throw new AppwriteException('Missing required parameter: "roles"');
}

if (typeof url === 'undefined') {
throw new AppwriteException('Missing required parameter: "url"');
}

const apiPath = '/teams/{teamId}/memberships'.replace('{teamId}', teamId);
const payload: Payload = {};

Expand Down