210 lines
5.2 KiB
JavaScript
210 lines
5.2 KiB
JavaScript
/**
|
|
* Appwrite Web SDK - Minimal Bundle for Chrome Extension
|
|
* This is a simplified version for extension use
|
|
* For full SDK, download from: https://cdn.jsdelivr.net/npm/appwrite@latest/dist/iife/sdk.min.js
|
|
*
|
|
* Version: 21.x compatible
|
|
*/
|
|
|
|
(function(global) {
|
|
'use strict';
|
|
|
|
// Appwrite namespace
|
|
const Appwrite = {};
|
|
|
|
// Client class
|
|
class Client {
|
|
constructor() {
|
|
this.config = {
|
|
endpoint: 'https://cloud.appwrite.io/v1',
|
|
project: '',
|
|
};
|
|
this.headers = {
|
|
'content-type': 'application/json',
|
|
'x-sdk-name': 'Chrome Extension',
|
|
'x-sdk-platform': 'client',
|
|
'x-sdk-language': 'web',
|
|
'x-sdk-version': '21.0.0',
|
|
};
|
|
}
|
|
|
|
setEndpoint(endpoint) {
|
|
this.config.endpoint = endpoint;
|
|
return this;
|
|
}
|
|
|
|
setProject(project) {
|
|
this.config.project = project;
|
|
this.headers['x-appwrite-project'] = project;
|
|
return this;
|
|
}
|
|
|
|
setKey(key) {
|
|
if (key) {
|
|
this.headers['x-appwrite-key'] = key;
|
|
} else {
|
|
delete this.headers['x-appwrite-key'];
|
|
}
|
|
return this;
|
|
}
|
|
|
|
async call(method, path, headers = {}, params = {}) {
|
|
const url = new URL(this.config.endpoint + path);
|
|
const options = {
|
|
method: method.toUpperCase(),
|
|
headers: { ...this.headers, ...headers },
|
|
credentials: 'include',
|
|
};
|
|
|
|
if (method === 'GET') {
|
|
for (const [key, value] of Object.entries(params)) {
|
|
if (value !== undefined) {
|
|
url.searchParams.append(key, value);
|
|
}
|
|
}
|
|
} else {
|
|
options.body = JSON.stringify(params);
|
|
}
|
|
|
|
const response = await fetch(url.toString(), options);
|
|
const contentType = response.headers.get('content-type') || '';
|
|
|
|
let data;
|
|
if (contentType.includes('application/json')) {
|
|
data = await response.json();
|
|
} else {
|
|
data = await response.text();
|
|
}
|
|
|
|
if (response.status >= 400) {
|
|
throw new AppwriteException(
|
|
data.message || 'Unknown error',
|
|
response.status,
|
|
data.type || '',
|
|
data
|
|
);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
}
|
|
|
|
// Account class
|
|
class Account {
|
|
constructor(client) {
|
|
this.client = client;
|
|
}
|
|
|
|
async get() {
|
|
return await this.client.call('GET', '/account');
|
|
}
|
|
|
|
async create(userId, email, password, name = undefined) {
|
|
const params = { userId, email, password };
|
|
if (name) params.name = name;
|
|
return await this.client.call('POST', '/account', {}, params);
|
|
}
|
|
|
|
async createEmailPasswordSession(email, password) {
|
|
return await this.client.call('POST', '/account/sessions/email', {}, {
|
|
email,
|
|
password,
|
|
});
|
|
}
|
|
|
|
async createSession(userId, secret) {
|
|
return await this.client.call('POST', '/account/sessions', {}, {
|
|
userId,
|
|
secret,
|
|
});
|
|
}
|
|
|
|
async getSession(sessionId) {
|
|
return await this.client.call('GET', `/account/sessions/${sessionId}`);
|
|
}
|
|
|
|
async listSessions() {
|
|
return await this.client.call('GET', '/account/sessions');
|
|
}
|
|
|
|
async deleteSession(sessionId) {
|
|
return await this.client.call('DELETE', `/account/sessions/${sessionId}`);
|
|
}
|
|
|
|
async deleteSessions() {
|
|
return await this.client.call('DELETE', '/account/sessions');
|
|
}
|
|
|
|
async updateEmail(email, password) {
|
|
return await this.client.call('PATCH', '/account/email', {}, {
|
|
email,
|
|
password,
|
|
});
|
|
}
|
|
|
|
async updatePassword(password, oldPassword = undefined) {
|
|
const params = { password };
|
|
if (oldPassword) params.oldPassword = oldPassword;
|
|
return await this.client.call('PATCH', '/account/password', {}, params);
|
|
}
|
|
|
|
async updateName(name) {
|
|
return await this.client.call('PATCH', '/account/name', {}, { name });
|
|
}
|
|
|
|
async getPrefs() {
|
|
return await this.client.call('GET', '/account/prefs');
|
|
}
|
|
|
|
async updatePrefs(prefs) {
|
|
return await this.client.call('PATCH', '/account/prefs', {}, { prefs });
|
|
}
|
|
|
|
async createRecovery(email, url) {
|
|
return await this.client.call('POST', '/account/recovery', {}, {
|
|
email,
|
|
url,
|
|
});
|
|
}
|
|
|
|
async updateRecovery(userId, secret, password) {
|
|
return await this.client.call('PUT', '/account/recovery', {}, {
|
|
userId,
|
|
secret,
|
|
password,
|
|
});
|
|
}
|
|
|
|
async createVerification(url) {
|
|
return await this.client.call('POST', '/account/verification', {}, { url });
|
|
}
|
|
|
|
async updateVerification(userId, secret) {
|
|
return await this.client.call('PUT', '/account/verification', {}, {
|
|
userId,
|
|
secret,
|
|
});
|
|
}
|
|
}
|
|
|
|
// Exception class
|
|
class AppwriteException extends Error {
|
|
constructor(message, code = 0, type = '', response = null) {
|
|
super(message);
|
|
this.name = 'AppwriteException';
|
|
this.code = code;
|
|
this.type = type;
|
|
this.response = response;
|
|
}
|
|
}
|
|
|
|
// Export to Appwrite namespace
|
|
Appwrite.Client = Client;
|
|
Appwrite.Account = Account;
|
|
Appwrite.AppwriteException = AppwriteException;
|
|
|
|
// Make available globally
|
|
global.Appwrite = Appwrite;
|
|
|
|
})(typeof self !== 'undefined' ? self : this);
|