@nativescript/appwrite
Version:
Appwrite SDK for NativeScript
642 lines • 19.9 kB
JavaScript
import { Utils } from '@nativescript/core';
export function dataDeserialize(nativeData) {
if (nativeData === null || typeof nativeData !== 'object') {
return nativeData;
}
let store;
switch (nativeData.getClass().getName()) {
case 'java.lang.String': {
return String(nativeData);
}
case 'java.lang.Boolean': {
return String(nativeData) === 'true';
}
case 'java.lang.Float':
case 'java.lang.Integer':
case 'java.lang.Long':
case 'java.lang.Double':
case 'java.lang.Short': {
return Number(nativeData);
}
case 'org.json.JSONArray': {
store = [];
for (let j = 0; j < nativeData.length(); j++) {
store[j] = dataDeserialize(nativeData.get(j));
}
break;
}
case 'org.json.JSONObject': {
store = {};
const i = nativeData.keys();
let key;
while (i.hasNext()) {
key = i.next();
store[key] = dataDeserialize(nativeData.get(key));
}
break;
}
case 'androidx.collection.SimpleArrayMap': {
const count = nativeData.size();
for (let l = 0; l < count; l++) {
const key = nativeData.keyAt(l);
store[key] = dataDeserialize(nativeData.get(key));
}
break;
}
case 'com.google.gson.internal.LinkedTreeMap':
case 'androidx.collection.ArrayMap':
case 'android.os.Bundle':
case 'java.util.HashMap':
case 'java.util.Map': {
store = {};
const keys = nativeData.keySet().toArray();
for (let k = 0; k < keys.length; k++) {
const key = keys[k];
store[key] = dataDeserialize(nativeData.get(key));
}
break;
}
default:
if (typeof nativeData === 'object' && nativeData instanceof java.util.List) {
const array = [];
const size = nativeData.size();
for (let i = 0, n = size; i < n; i++) {
array[i] = dataDeserialize(nativeData.get(i));
}
store = array;
}
else {
store = null;
}
break;
}
return store;
}
export class Client {
constructor() {
this._native = new io.appwrite.Client();
}
setEndpoint(endpoint) {
this._native.setEndpoint(endpoint);
return this;
}
setProject(project) {
this._native.setProject(project);
return this;
}
setKey(key) {
this._native.setKey(key);
return this;
}
setJWT(jwt) {
this._native.setJWT(jwt);
return this;
}
setSelfSigned(status) {
this._native.setSelfSigned(status);
return this;
}
setSession(session) {
this._native.setSession(session);
return this;
}
addHeader(key, value) {
this._native.addHeader(key, value);
return this;
}
setForwardedUserAgent(value) {
this._native.setForwardedUserAgent(value);
return this;
}
setLocale(value) {
this._native.setLocale(value);
return this;
}
get native() {
return this._native;
}
}
export class ID {
static custom(id) {
return org.nativescript.plugins.appwrite.ID.custom(id);
}
static unique() {
return org.nativescript.plugins.appwrite.ID.unique();
}
}
export class Target {
constructor(target) {
this._native = target;
}
get native() {
return this._native;
}
get createdAt() {
return this.native.getCreatedAt();
}
get expired() {
return this.native.getExpired();
}
get id() {
return this.native.getId();
}
get identifier() {
return this.native.getIdentifier();
}
get name() {
return this.native.getName();
}
get providerId() {
return this.native.getProviderId();
}
get providerType() {
return this.native.getProviderType();
}
get updatedAt() {
return this.native.getUpdatedAt();
}
get userId() {
return this.native.getUserId();
}
}
export class User {
constructor(user) {
this._native = user;
}
get native() {
return this._native;
}
get accessedAt() {
return this.native.getAccessedAt();
}
get createdAt() {
return this.native.getCreatedAt();
}
get email() {
return this.native.getEmail();
}
get emailVerification() {
return this.native.getEmailVerification();
}
get id() {
return this.native.getId();
}
get labels() {
return dataDeserialize(this.native.getLabels());
}
get mfa() {
return this.native.getMfa();
}
get name() {
return this.native.getName();
}
get password() {
return this.native.getPassword();
}
get passwordUpdate() {
return this.native.getPasswordUpdate();
}
get phone() {
return this.native.getPhone();
}
get phoneVerification() {
return this.native.getPhoneVerification();
}
get prefs() {
return dataDeserialize(this.native.getPrefs());
}
get registration() {
return this.native.getRegistration();
}
get status() {
return this.native.getStatus();
}
get targets() {
const targets = this.native.getTargets();
const count = targets.size();
const ret = new Array(count);
for (let i = 0; i < count; i++) {
const target = targets.get(i);
ret[i] = new Target(target);
}
return ret;
}
get updatedAt() {
return this.native.getUpdatedAt();
}
get userHash() {
return this.native.getHash();
}
toJSON() {
return dataDeserialize(this.native.toMap());
}
}
export class Account {
constructor(client) {
this._native = new io.appwrite.services.Account(client.native);
}
get native() {
return this._native;
}
create(userId, email, password, name) {
return new Promise((resolve, reject) => {
org.nativescript.plugins.appwrite.Account.create(this.native, userId, email, password, name ?? null, new kotlin.jvm.functions.Function2({
invoke(response, error) {
if (error) {
reject(error);
}
else {
resolve(new User(response));
}
},
}));
});
}
createSession(userId, secret) {
return new Promise((resolve, reject) => {
org.nativescript.plugins.appwrite.Account.createSession(this.native, userId, secret, new kotlin.jvm.functions.Function2({
invoke(session, error) {
if (error) {
reject(error);
}
else {
resolve(new Session(session));
}
},
}));
});
}
createEmailPasswordSession(email, password) {
return new Promise((resolve, reject) => {
org.nativescript.plugins.appwrite.Account.createEmailPasswordSession(this.native, email, password, new kotlin.jvm.functions.Function2({
invoke(session, error) {
if (error) {
reject(error);
}
else {
resolve(new Session(session));
}
},
}));
});
}
createAnonymousSession() {
return new Promise((resolve, reject) => {
org.nativescript.plugins.appwrite.Account.createAnonymousSession(this.native, new kotlin.jvm.functions.Function2({
invoke(session, error) {
if (error) {
reject(error);
}
else {
resolve(new Session(session));
}
},
}));
});
}
deleteSession(sessionId) {
return new Promise((resolve, reject) => {
org.nativescript.plugins.appwrite.Account.deleteSession(this.native, sessionId, new kotlin.jvm.functions.Function1({
invoke(error) {
if (error) {
reject(error);
}
else {
resolve();
}
},
}));
});
}
}
export class Document {
constructor(document) {
this._native = document;
}
get native() {
return this._native;
}
get collectionId() {
return this.native.getCollectionId();
}
get createdAt() {
return this.native.getCreatedAt();
}
get data() {
return dataDeserialize(this.native.getData());
}
get databaseId() {
return this.native.getDatabaseId();
}
get id() {
return this.native.getId();
}
get permissions() {
return dataDeserialize(this.native.getPermissions());
}
get updatedAt() {
return this.native.getUpdatedAt();
}
toJSON() {
return {
collectionId: this.collectionId,
createdAt: this.createdAt,
data: this.data,
databaseId: this.databaseId,
id: this.id,
permissions: this.permissions,
updatedAt: this.updatedAt,
};
}
}
export class Databases {
constructor(client) {
this._native = new io.appwrite.services.Databases(client.native);
}
get native() {
return this._native;
}
createDocument(databaseId, collectionId, documentId, data = {}, permissions = []) {
return new Promise((resolve, reject) => {
org.nativescript.plugins.appwrite.Databases.createDocument(this.native, databaseId, collectionId, documentId, Utils.dataSerialize(data ?? {}, true), Utils.dataSerialize(permissions ?? [], true), new kotlin.jvm.functions.Function2({
invoke(document, error) {
if (error) {
reject(error);
}
else {
resolve(new Document(document));
}
},
}));
});
}
getDocument(databaseId, collectionId, documentId, queries = []) {
return new Promise((resolve, reject) => {
org.nativescript.plugins.appwrite.Databases.getDocument(this.native, databaseId, collectionId, documentId, Utils.dataSerialize(queries ?? [], true), new kotlin.jvm.functions.Function2({
invoke(document, error) {
if (error) {
reject(error);
}
else {
resolve(new Document(document));
}
},
}));
});
}
listDocuments(databaseId, collectionId, queries = []) {
return new Promise((resolve, reject) => {
org.nativescript.plugins.appwrite.Databases.listDocuments(this.native, databaseId, collectionId, Utils.dataSerialize(queries ?? [], true), new kotlin.jvm.functions.Function2({
invoke(document, error) {
if (error) {
reject(error);
}
else {
const count = document.size();
const ret = new Array(count);
for (let i = 0; i < count; i++) {
const doc = document.get(i);
ret[i] = new Document(doc);
}
resolve(ret);
}
},
}));
});
}
updateDocument(databaseId, collectionId, documentId, data, permissions = []) {
return new Promise((resolve, reject) => {
org.nativescript.plugins.appwrite.Databases.updateDocument(this.native, databaseId, collectionId, documentId, Utils.dataSerialize(data ?? {}, true), Utils.dataSerialize(permissions ?? [], true), new kotlin.jvm.functions.Function2({
invoke(document, error) {
if (error) {
reject(error);
}
else {
resolve(new Document(document));
}
},
}));
});
}
deleteDocument(databaseId, collectionId, documentId) {
return new Promise((resolve, reject) => {
org.nativescript.plugins.appwrite.Databases.deleteDocument(this.native, databaseId, collectionId, documentId, new kotlin.jvm.functions.Function2({
invoke(error) {
if (error) {
reject(error);
}
else {
resolve();
}
},
}));
});
}
}
export class Functions {
constructor(client) {
this._native = new io.appwrite.services.Functions(client.native);
}
get native() {
return this._native;
}
}
export class Storage {
constructor(client) {
this._native = new io.appwrite.services.Storage(client.native);
}
get native() {
return this._native;
}
}
export class Session {
constructor(session) {
this._native = session;
}
get native() {
return this._native;
}
get clientCode() {
return this.native.getClientCode();
}
get clientEngine() {
return this.native.getClientEngine();
}
get clientEngineVersion() {
return this.native.getClientEngineVersion();
}
get clientName() {
return this.native.getClientName();
}
get clientType() {
return this.native.getClientType();
}
get clientVersion() {
return this.native.getClientVersion();
}
get countryCode() {
return this.native.getCountryCode();
}
get countryName() {
return this.native.getCountryName();
}
get createdAt() {
return this.native.getCreatedAt();
}
get current() {
return this.native.getCurrent();
}
get deviceBrand() {
return this.native.getDeviceBrand();
}
get deviceModel() {
return this.native.getDeviceModel();
}
get deviceName() {
return this.native.getDeviceName();
}
get expire() {
return this.native.getExpire();
}
get factors() {
return dataDeserialize(this.native.getFactors());
}
get id() {
return this.native.getId();
}
get ip() {
return this.native.getIp();
}
get mfaUpdatedAt() {
return this.native.getMfaUpdatedAt();
}
get osCode() {
return this.native.getOsCode();
}
get osName() {
return this.native.getOsName();
}
get osVersion() {
return this.native.getOsVersion();
}
get provider() {
return this.native.getProvider();
}
get providerAccessToken() {
return this.native.getProviderAccessToken();
}
get providerAccessTokenExpiry() {
return this.native.getProviderAccessTokenExpiry();
}
get providerRefreshToken() {
return this.native.getProviderRefreshToken();
}
get providerUid() {
return this.native.getProviderUid();
}
get secret() {
return this.native.getSecret();
}
get updatedAt() {
return this.native.getUpdatedAt();
}
get userId() {
return this.native.getUserId();
}
toJSON() {
return {
clientCode: this.clientCode,
clientEngine: this.clientEngine,
clientEngineVersion: this.clientEngineVersion,
clientName: this.clientName,
clientType: this.clientType,
clientVersion: this.clientVersion,
countryCode: this.countryCode,
countryName: this.countryName,
createdAt: this.createdAt,
current: this.current,
deviceBrand: this.deviceBrand,
deviceModel: this.deviceModel,
deviceName: this.deviceName,
expire: this.expire,
factors: this.factors,
id: this.id,
ip: this.ip,
mfaUpdatedAt: this.mfaUpdatedAt,
osCode: this.osCode,
osName: this.osName,
osVersion: this.osVersion,
provider: this.provider,
providerAccessToken: this.providerAccessToken,
providerAccessTokenExpiry: this.providerAccessTokenExpiry,
providerRefreshToken: this.providerRefreshToken,
providerUid: this.providerUid,
secret: this.secret,
updatedAt: this.updatedAt,
userId: this.userId,
};
}
}
export class Query {
static between(attribute, start, end) {
return org.nativescript.plugins.appwrite.Query.between(attribute, start, end);
}
static contains(attribute, value) {
return org.nativescript.plugins.appwrite.Query.contains(attribute, value);
}
static cursorAfter(id) {
return org.nativescript.plugins.appwrite.Query.cursorAfter(id);
}
static cursorBefore(id) {
return org.nativescript.plugins.appwrite.Query.cursorBefore(id);
}
static endsWith(attribute, value) {
return org.nativescript.plugins.appwrite.Query.endsWith(attribute, value);
}
static equal(attribute, value) {
return org.nativescript.plugins.appwrite.Query.equal(attribute, value);
}
static greaterThanEqual(attribute, value) {
return org.nativescript.plugins.appwrite.Query.greaterThanEqual(attribute, value);
}
static greaterThan(attribute, value) {
return org.nativescript.plugins.appwrite.Query.greaterThan(attribute, value);
}
static isNotNull(attribute) {
return org.nativescript.plugins.appwrite.Query.isNotNull(attribute);
}
static isNull(attribute) {
return org.nativescript.plugins.appwrite.Query.isNull(attribute);
}
static lessThanEqualWithAttribute(attribute, value) {
return org.nativescript.plugins.appwrite.Query.lessThanEqual(attribute, value);
}
static lessThan(attribute, value) {
return org.nativescript.plugins.appwrite.Query.lessThan(attribute, value);
}
static limit(limit) {
return org.nativescript.plugins.appwrite.Query.limit(limit);
}
static notEqual(attribute, value) {
return org.nativescript.plugins.appwrite.Query.notEqual(attribute, value);
}
static offset(offset) {
return org.nativescript.plugins.appwrite.Query.offset(offset);
}
static orderAsc(attribute) {
return org.nativescript.plugins.appwrite.Query.orderAsc(attribute);
}
static orderDesc(attribute) {
return org.nativescript.plugins.appwrite.Query.orderDesc(attribute);
}
static search(attribute, value) {
return org.nativescript.plugins.appwrite.Query.search(attribute, value);
}
static select(attributes) {
return org.nativescript.plugins.appwrite.Query.select(Utils.dataSerialize(attributes, true));
}
static startsWith(attribute, value) {
return org.nativescript.plugins.appwrite.Query.startsWith(attribute, value);
}
}
//# sourceMappingURL=index.android.js.map