Skip to content

Commit

Permalink
Add new push message parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
abnegate committed Dec 10, 2024
1 parent 255c672 commit 7c865f9
Show file tree
Hide file tree
Showing 37 changed files with 1,100 additions and 1,187 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 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-1.6.0-blue.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-1.6.1-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
2 changes: 1 addition & 1 deletion docs/examples/databases/update-string-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ const response = await databases.updateStringAttribute(
'', // key
false, // required
'<DEFAULT>', // default
null, // size (optional)
1, // size (optional)
'' // newKey (optional)
);
2 changes: 1 addition & 1 deletion docs/examples/functions/create-deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const functions = new Functions(client);

const response = await functions.createDeployment(
'<FUNCTION_ID>', // functionId
Payload.fromFile('/path/to/file.png'), // code
InputFile.fromPath('/path/to/file.png', 'file.png'), // code
false, // activate
'<ENTRYPOINT>', // entrypoint (optional)
'<COMMANDS>' // commands (optional)
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/functions/create-execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const functions = new Functions(client);

const response = await functions.createExecution(
'<FUNCTION_ID>', // functionId
Payload.fromJson({ x: "y" }), // body (optional)
'<BODY>', // body (optional)
false, // async (optional)
'<PATH>', // path (optional)
ExecutionMethod.GET, // method (optional)
Expand Down
13 changes: 8 additions & 5 deletions docs/examples/messaging/create-push.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, Messaging } from "https://deno.land/x/appwrite/mod.ts";
import { Client, Messaging, MessagePriority } from "https://deno.land/x/appwrite/mod.ts";

const client = new Client()
.setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
Expand All @@ -9,8 +9,8 @@ const messaging = new Messaging(client);

const response = await messaging.createPush(
'<MESSAGE_ID>', // messageId
'<TITLE>', // title
'<BODY>', // body
'<TITLE>', // title (optional)
'<BODY>', // body (optional)
[], // topics (optional)
[], // users (optional)
[], // targets (optional)
Expand All @@ -21,7 +21,10 @@ const response = await messaging.createPush(
'<SOUND>', // sound (optional)
'<COLOR>', // color (optional)
'<TAG>', // tag (optional)
'<BADGE>', // badge (optional)
null, // badge (optional)
false, // draft (optional)
'' // scheduledAt (optional)
'', // scheduledAt (optional)
false, // contentAvailable (optional)
false, // critical (optional)
MessagePriority.Normal // priority (optional)
);
7 changes: 5 additions & 2 deletions docs/examples/messaging/update-push.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, Messaging } from "https://deno.land/x/appwrite/mod.ts";
import { Client, Messaging, MessagePriority } from "https://deno.land/x/appwrite/mod.ts";

const client = new Client()
.setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint
Expand All @@ -23,5 +23,8 @@ const response = await messaging.updatePush(
'<TAG>', // tag (optional)
null, // badge (optional)
false, // draft (optional)
'' // scheduledAt (optional)
'', // scheduledAt (optional)
false, // contentAvailable (optional)
false, // critical (optional)
MessagePriority.Normal // priority (optional)
);
2 changes: 1 addition & 1 deletion docs/examples/storage/create-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ const storage = new Storage(client);
const response = await storage.createFile(
'<BUCKET_ID>', // bucketId
'<FILE_ID>', // fileId
Payload.fromFile('/path/to/file.png'), // file
InputFile.fromPath('/path/to/file.png', 'file.png'), // file
["read("any")"] // permissions (optional)
);
6 changes: 4 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Query } from "./src/query.ts";
import { Permission } from "./src/permission.ts";
import { Role } from "./src/role.ts";
import { ID } from "./src/id.ts";
import { Payload } from "./src/payload.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";
Expand All @@ -28,6 +28,7 @@ import { IndexType } from "./src/enums/index-type.ts";
import { Runtime } from "./src/enums/runtime.ts";
import { ExecutionMethod } from "./src/enums/execution-method.ts";
import { Name } from "./src/enums/name.ts";
import { MessagePriority } from "./src/enums/message-priority.ts";
import { SmtpEncryption } from "./src/enums/smtp-encryption.ts";
import { Compression } from "./src/enums/compression.ts";
import { ImageGravity } from "./src/enums/image-gravity.ts";
Expand All @@ -41,7 +42,7 @@ export {
Permission,
Role,
ID,
Payload,
InputFile,
AppwriteException,
Account,
Avatars,
Expand All @@ -66,6 +67,7 @@ export {
Runtime,
ExecutionMethod,
Name,
MessagePriority,
SmtpEncryption,
Compression,
ImageGravity,
Expand Down
50 changes: 7 additions & 43 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { AppwriteException } from './exception.ts';
import { Payload } from './payload.ts';
import { getBoundary, parse as parseMultipart } from './multipart.ts';

export interface Params {
export interface Payload {
[key: string]: any;
}

Expand All @@ -11,13 +9,13 @@ export class Client {
static DENO_READ_CHUNK_SIZE = 16384; // 16kb; refference: https://github.com/denoland/deno/discussions/9906

endpoint: string = 'https://cloud.appwrite.io/v1';
headers: Params = {
headers: Payload = {
'content-type': '',
'user-agent' : `AppwriteDenoSDK/13.0.0 (${Deno.build.os}; ${Deno.build.arch})`,
'user-agent' : `AppwriteDenoSDK/12.2.0 (${Deno.build.os}; ${Deno.build.arch})`,
'x-sdk-name': 'Deno',
'x-sdk-platform': 'server',
'x-sdk-language': 'deno',
'x-sdk-version': '13.0.0',
'x-sdk-version': '12.2.0',
'X-Appwrite-Response-Format':'1.6.0',
};

Expand Down Expand Up @@ -130,7 +128,7 @@ export class Client {
return this;
}

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

Expand Down Expand Up @@ -194,40 +192,6 @@ export class Client {
return response.headers.get("location");
}

if (response.headers.get('content-type')?.includes('multipart/form-data')) {
const boundary = getBoundary(
response.headers.get("content-type") || ""
);

const body = new Uint8Array(await response.arrayBuffer());
const parts = parseMultipart(body, boundary);
const partsObject: { [key: string]: any } = {};

for (const part of parts) {
if (!part.name) {
continue;
}
if (part.name === "responseBody") {
partsObject[part.name] = Payload.fromBinary(part.data, part.filename);
} else if (part.name === "responseStatusCode") {
partsObject[part.name] = parseInt(part.data.toString());
} else if (part.name === "duration") {
partsObject[part.name] = parseFloat(part.data.toString());
} else if (part.type === 'application/json') {
try {
partsObject[part.name] = JSON.parse(part.data.toString());
} catch (e) {
throw new Error(`Error parsing JSON for part ${part.name}: ${e instanceof Error ? e.message : 'Unknown error'}`);
}
} else {
partsObject[part.name] = new TextDecoder().decode(part.data);
}
}

const data = partsObject;
return data;
}

const text = await response.text();
let json = undefined;
try {
Expand All @@ -238,8 +202,8 @@ export class Client {
return json;
}

static flatten(data: Params, prefix = ''): Params {
let output: Params = {};
static flatten(data: Payload, prefix = ''): Payload {
let output: Payload = {};

for (const [key, value] of Object.entries(data)) {
let finalKey = prefix ? prefix + '[' + key +']' : key;
Expand Down
4 changes: 4 additions & 0 deletions src/enums/message-priority.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum MessagePriority {
Normal = 'normal',
High = 'high',
}
12 changes: 11 additions & 1 deletion src/enums/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum Runtime {
Node190 = 'node-19.0',
Node200 = 'node-20.0',
Node210 = 'node-21.0',
Node22 = 'node-22',
Php80 = 'php-8.0',
Php81 = 'php-8.1',
Php82 = 'php-8.2',
Expand All @@ -23,29 +24,38 @@ export enum Runtime {
Deno124 = 'deno-1.24',
Deno135 = 'deno-1.35',
Deno140 = 'deno-1.40',
Deno146 = 'deno-1.46',
Deno20 = 'deno-2.0',
Dart215 = 'dart-2.15',
Dart216 = 'dart-2.16',
Dart217 = 'dart-2.17',
Dart218 = 'dart-2.18',
Dart30 = 'dart-3.0',
Dart31 = 'dart-3.1',
Dart33 = 'dart-3.3',
Dotnet31 = 'dotnet-3.1',
Dart35 = 'dart-3.5',
Dotnet60 = 'dotnet-6.0',
Dotnet70 = 'dotnet-7.0',
Dotnet80 = 'dotnet-8.0',
Java80 = 'java-8.0',
Java110 = 'java-11.0',
Java170 = 'java-17.0',
Java180 = 'java-18.0',
Java210 = 'java-21.0',
Java22 = 'java-22',
Swift55 = 'swift-5.5',
Swift58 = 'swift-5.8',
Swift59 = 'swift-5.9',
Swift510 = 'swift-5.10',
Kotlin16 = 'kotlin-1.6',
Kotlin18 = 'kotlin-1.8',
Kotlin19 = 'kotlin-1.9',
Kotlin20 = 'kotlin-2.0',
Cpp17 = 'cpp-17',
Cpp20 = 'cpp-20',
Bun10 = 'bun-1.0',
Bun11 = 'bun-1.1',
Go123 = 'go-1.23',
Static1 = 'static-1',
Flutter324 = 'flutter-3.24',
}
46 changes: 46 additions & 0 deletions src/inputFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const _bufferToString = (buffer: Uint8Array): ReadableStream<Uint8Array> => {
return new ReadableStream({
start(controller) {
controller.enqueue(buffer);
controller.close();
}
});
};

export class InputFile {
stream: ReadableStream<Uint8Array>; // 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 = file.readable;
const size = Deno.statSync(filePath).size;
return new InputFile(stream, filename, size);
};

static fromBlob = async (blob: Blob, filename: string) => {
const arrayBuffer = await blob.arrayBuffer();
const buffer = new Uint8Array(arrayBuffer);
return InputFile.fromBuffer(buffer, filename);
};

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<Uint8Array>, filename: string, size: number) {
this.stream = stream;
this.filename = filename;
this.size = size;
}
}
16 changes: 9 additions & 7 deletions src/models.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Payload } from './payload.ts';

export namespace Models {
/**
* Documents List
Expand Down Expand Up @@ -1681,11 +1679,11 @@ export namespace Models {
*/
userId: string;
/**
* User name.
* User name. Hide this attribute by toggling membership privacy in the Console.
*/
userName: string;
/**
* User email address.
* User email address. Hide this attribute by toggling membership privacy in the Console.
*/
userEmail: string;
/**
Expand All @@ -1709,7 +1707,7 @@ export namespace Models {
*/
confirm: boolean;
/**
* Multi factor authentication status, true if the user has MFA enabled or false otherwise.
* Multi factor authentication status, true if the user has MFA enabled or false otherwise. Hide this attribute by toggling membership privacy in the Console.
*/
mfa: boolean;
/**
Expand Down Expand Up @@ -1774,7 +1772,7 @@ export namespace Models {
*/
events: string[];
/**
* Function execution schedult in CRON format.
* Function execution schedule in CRON format.
*/
schedule: string;
/**
Expand Down Expand Up @@ -2007,7 +2005,7 @@ export namespace Models {
/**
* HTTP response body. This will return empty unless execution is created as synchronous.
*/
responseBody: Payload;
responseBody: string;
/**
* HTTP response headers as a key-value object. This will return only whitelisted headers. All headers are returned if execution is created as synchronous.
*/
Expand Down Expand Up @@ -2591,5 +2589,9 @@ export namespace Models {
* The target identifier.
*/
identifier: string;
/**
* Is the target expired.
*/
expired: boolean;
}
}
Loading

0 comments on commit 7c865f9

Please sign in to comment.