Always check if client is defined

This commit is contained in:
alexweininger 2021-04-30 02:34:03 -07:00
parent d8ba312721
commit cbd6cf1d1a
19 changed files with 88 additions and 53 deletions

View file

@ -9,12 +9,12 @@ const sdk: SDK = require("node-appwrite");
export let client: Client; export let client: Client;
export let clientConfig: { endpoint: string; projectId: string; secret: string }; export let clientConfig: { endpoint: string; projectId: string; secret: string };
export let usersClient: Users; export let usersClient: Users | undefined;
export let healthClient: Health; export let healthClient: Health | undefined;
export let databaseClient: Database; export let databaseClient: Database | undefined;
export let storageClient: Storage; export let storageClient: Storage | undefined;
export function initAppwriteClient({ endpoint, projectId, secret }: AppwriteProjectConfiguration) { function initAppwriteClient({ endpoint, projectId, secret }: AppwriteProjectConfiguration) {
client = new sdk.Client(); client = new sdk.Client();
clientConfig = { endpoint, projectId, secret }; clientConfig = { endpoint, projectId, secret };
client.setEndpoint(endpoint).setProject(projectId).setKey(secret); client.setEndpoint(endpoint).setProject(projectId).setKey(secret);
@ -26,3 +26,15 @@ export function initAppwriteClient({ endpoint, projectId, secret }: AppwriteProj
return client; return client;
} }
export function createAppwriteClient(config?: AppwriteProjectConfiguration) {
if (config) {
initAppwriteClient(config);
return;
}
usersClient = undefined;
healthClient = undefined;
databaseClient = undefined;
storageClient = undefined;
}

View file

@ -1,4 +1,4 @@
import { initAppwriteClient } from "../client"; import { createAppwriteClient } from "../client";
import { addProjectConfiguration } from '../settings'; import { addProjectConfiguration } from '../settings';
import { addProjectWizard } from "../ui/AddProjectWizard"; import { addProjectWizard } from "../ui/AddProjectWizard";
import { refreshTree } from '../utils/refreshTree'; import { refreshTree } from '../utils/refreshTree';
@ -7,7 +7,7 @@ export async function connectAppwrite() {
const projectConfiguration = await addProjectWizard(); const projectConfiguration = await addProjectWizard();
if (projectConfiguration) { if (projectConfiguration) {
addProjectConfiguration(projectConfiguration); addProjectConfiguration(projectConfiguration);
initAppwriteClient(projectConfiguration); createAppwriteClient(projectConfiguration);
refreshTree(); refreshTree();
} }
} }

View file

@ -1,9 +1,13 @@
import { window } from 'vscode'; import { window } from "vscode";
import { databaseClient } from '../../client'; import { databaseClient } from "../../client";
export async function createCollection(): Promise<void> { export async function createCollection(): Promise<void> {
if (!databaseClient) {
return;
}
const name = await window.showInputBox({ const name = await window.showInputBox({
prompt: 'Collection name' prompt: "Collection name",
}); });
if (name && name.length > 0) { if (name && name.length > 0) {

View file

@ -5,6 +5,11 @@ import { createRuleWizard } from "../../ui/createRuleWizard";
import { refreshTree } from '../../utils/refreshTree'; import { refreshTree } from '../../utils/refreshTree';
export async function createRule(rulesTreeItem: RulesTreeItem): Promise<void> { export async function createRule(rulesTreeItem: RulesTreeItem): Promise<void> {
if (!databaseClient) {
return;
}
const ruleContext = await createRuleWizard(); const ruleContext = await createRuleWizard();
const collection = rulesTreeItem.parent.collection; const collection = rulesTreeItem.parent.collection;

View file

@ -4,6 +4,9 @@ import { CollectionTreeItem } from "../../tree/database/CollectionTreeItem";
import { confirmDialog } from "../../ui/confirmDialog"; import { confirmDialog } from "../../ui/confirmDialog";
export async function deleteCollection(collectionTreeItem: CollectionTreeItem): Promise<void> { export async function deleteCollection(collectionTreeItem: CollectionTreeItem): Promise<void> {
if (!databaseClient) {
return;
}
const collection = collectionTreeItem.collection; const collection = collectionTreeItem.collection;
try { try {
const shouldDelete = await confirmDialog(`Delete collection "${collection.name}"?`); const shouldDelete = await confirmDialog(`Delete collection "${collection.name}"?`);

View file

@ -4,6 +4,9 @@ import { DocumentTreeItem } from "../../tree/database/DocumentTreeItem";
import { confirmDialog } from "../../ui/confirmDialog"; import { confirmDialog } from "../../ui/confirmDialog";
export async function deleteDocument(documentTreeItem: DocumentTreeItem): Promise<void> { export async function deleteDocument(documentTreeItem: DocumentTreeItem): Promise<void> {
if (!databaseClient) {
return;
}
const document = documentTreeItem.document; const document = documentTreeItem.document;
const collection = documentTreeItem.parent.parent.collection; const collection = documentTreeItem.parent.parent.collection;
try { try {

View file

@ -1,6 +1,6 @@
import { window } from "vscode"; import { window } from "vscode";
import { databaseClient } from '../../../client'; import { databaseClient } from "../../../client";
import { CollapsableTreeItem } from '../../../tree/CollapsableTreeItem'; import { CollapsableTreeItem } from "../../../tree/CollapsableTreeItem";
import { PermissionsTreeItem } from "../../../tree/database/settings/PermissionsTreeItem"; import { PermissionsTreeItem } from "../../../tree/database/settings/PermissionsTreeItem";
import { PermissionTreeItem } from "../../../tree/database/settings/PermissionTreeItem"; import { PermissionTreeItem } from "../../../tree/database/settings/PermissionTreeItem";
@ -25,7 +25,9 @@ export async function createPermissionWizard(kind?: "read" | "write"): Promise<C
} }
export async function createPermission(treeItem: PermissionsTreeItem): Promise<void> { export async function createPermission(treeItem: PermissionsTreeItem): Promise<void> {
if (!databaseClient) {
return;
}
const collection = treeItem.parent.collection; const collection = treeItem.parent.collection;
const context = await createPermissionWizard(undefined); const context = await createPermissionWizard(undefined);
@ -37,7 +39,7 @@ export async function createPermission(treeItem: PermissionsTreeItem): Promise<v
const read = Array.from(collection.$permissions.read); const read = Array.from(collection.$permissions.read);
const write = Array.from(collection.$permissions.write); const write = Array.from(collection.$permissions.write);
if (context.kind === 'read') { if (context.kind === "read") {
read.push(context.permission); read.push(context.permission);
} else { } else {
write.push(context.permission); write.push(context.permission);

View file

@ -2,6 +2,9 @@ import { databaseClient } from "../../../client";
import { PermissionTreeItem } from "../../../tree/database/settings/PermissionTreeItem"; import { PermissionTreeItem } from "../../../tree/database/settings/PermissionTreeItem";
export async function deletePermission(treeItem: PermissionTreeItem): Promise<void> { export async function deletePermission(treeItem: PermissionTreeItem): Promise<void> {
if (!databaseClient) {
return;
}
const collection = treeItem.parent.parent.collection; const collection = treeItem.parent.parent.collection;
const kind = treeItem.kind; const kind = treeItem.kind;

View file

@ -1,8 +1,11 @@
import { window } from 'vscode'; import { window } from "vscode";
import { databaseClient } from '../../../client'; import { databaseClient } from "../../../client";
import { PermissionTreeItem } from '../../../tree/database/settings/PermissionTreeItem'; import { PermissionTreeItem } from "../../../tree/database/settings/PermissionTreeItem";
export async function editPermission(treeItem: PermissionTreeItem): Promise<void> { export async function editPermission(treeItem: PermissionTreeItem): Promise<void> {
if (!databaseClient) {
return;
}
const editedPermission = await window.showInputBox({ const editedPermission = await window.showInputBox({
value: treeItem.permission, value: treeItem.permission,
}); });

View file

@ -3,6 +3,9 @@ import { RuleTreeItem } from '../../tree/database/settings/RuleTreeItem';
import { refreshTree } from '../../utils/refreshTree'; import { refreshTree } from '../../utils/refreshTree';
export async function removeRule(ruleItem: RuleTreeItem): Promise<void> { export async function removeRule(ruleItem: RuleTreeItem): Promise<void> {
if (!databaseClient) {
return;
}
const rule = ruleItem.rule; const rule = ruleItem.rule;
const collection = ruleItem.parent.parent.collection; const collection = ruleItem.parent.parent.collection;
await databaseClient.removeRule(collection, rule); await databaseClient.removeRule(collection, rule);

View file

@ -1,12 +1,12 @@
import { window } from "vscode"; import { window } from "vscode";
import { initAppwriteClient } from "../../client"; import { createAppwriteClient } from '../../client';
import { addProjectWizard } from "../../ui/AddProjectWizard"; import { addProjectWizard } from "../../ui/AddProjectWizard";
export async function addProject() { export async function addProject() {
const projectConfiguration = await addProjectWizard(); const projectConfiguration = await addProjectWizard();
if (projectConfiguration) { if (projectConfiguration) {
initAppwriteClient(projectConfiguration); createAppwriteClient(projectConfiguration);
} }
window.showInformationMessage("Connected to Appwrite project."); window.showInformationMessage("Connected to Appwrite project.");

View file

@ -1,11 +1,8 @@
import { window } from "vscode"; import { removeProjectConfig } from "../../settings";
import { initAppwriteClient } from "../../client"; import { ProjectTreeItem } from "../../tree/projects/ProjectTreeItem";
import { removeProjectConfig } from '../../settings';
import { ProjectTreeItem } from '../../tree/projects/ProjectTreeItem';
import { addProjectWizard } from "../../ui/AddProjectWizard";
export async function removeProject(project: ProjectTreeItem | string) { export async function removeProject(project: ProjectTreeItem | string) {
if (typeof project === 'string') { if (typeof project === "string") {
await removeProjectConfig(project); await removeProjectConfig(project);
return; return;
} }

View file

@ -1,5 +1,3 @@
import { window } from "vscode";
import { initAppwriteClient } from "../../client";
import { setActiveProjectId } from '../../settings'; import { setActiveProjectId } from '../../settings';
import { ProjectTreeItem } from "../../tree/projects/ProjectTreeItem"; import { ProjectTreeItem } from "../../tree/projects/ProjectTreeItem";

View file

@ -3,6 +3,9 @@ import { usersClient } from '../../client';
import { ext } from '../../extensionVariables'; import { ext } from '../../extensionVariables';
export async function createUser(): Promise<void> { export async function createUser(): Promise<void> {
if (!usersClient) {
return;
}
const email = await window.showInputBox({ const email = await window.showInputBox({
ignoreFocusOut: true, ignoreFocusOut: true,
placeHolder: "jane.doe@hotmail.com", placeHolder: "jane.doe@hotmail.com",

View file

@ -5,6 +5,9 @@ import { DialogResponses } from "../../ui/DialogResponses";
import { refreshTree } from "../../utils/refreshTree"; import { refreshTree } from "../../utils/refreshTree";
export async function deleteUser(userTreeItem: UserTreeItem): Promise<void> { export async function deleteUser(userTreeItem: UserTreeItem): Promise<void> {
if (!usersClient) {
return;
}
const user = userTreeItem.user; const user = userTreeItem.user;
const userId = user.$id; const userId = user.$id;
const shouldDeleteUser = await window.showWarningMessage( const shouldDeleteUser = await window.showWarningMessage(

View file

@ -3,6 +3,9 @@ import { UserTreeItem } from "../../tree/users/UserTreeItem";
import { openReadOnlyJson } from '../../ui/openReadonlyContent'; import { openReadOnlyJson } from '../../ui/openReadonlyContent';
export async function getUserLogs(treeItem: UserTreeItem): Promise<void> { export async function getUserLogs(treeItem: UserTreeItem): Promise<void> {
if (!usersClient) {
return;
}
const userId = treeItem.user.$id; const userId = treeItem.user.$id;
const logs = await usersClient.getLogs(userId); const logs = await usersClient.getLogs(userId);

View file

@ -1,12 +1,11 @@
import * as vscode from "vscode"; import * as vscode from "vscode";
import { workspace } from 'vscode'; import { createAppwriteClient } from "./client";
import { initAppwriteClient } from "./client";
import { registerCommands } from "./commands/registerCommands"; import { registerCommands } from "./commands/registerCommands";
import { ext } from "./extensionVariables"; import { ext } from "./extensionVariables";
import { getActiveProjectConfiguration, getActiveProjectId, getDefaultProject } from "./settings"; import { getActiveProjectConfiguration, getActiveProjectId, getDefaultProject } from "./settings";
import { DatabaseTreeItemProvider } from "./tree/database/DatabaseTreeItemProvider"; import { DatabaseTreeItemProvider } from "./tree/database/DatabaseTreeItemProvider";
import { HealthTreeItemProvider } from "./tree/health/HealthTreeItemProvider"; import { HealthTreeItemProvider } from "./tree/health/HealthTreeItemProvider";
import { ProjectsTreeItemProvider } from './tree/projects/ProjectsTreeItemProvider'; import { ProjectsTreeItemProvider } from "./tree/projects/ProjectsTreeItemProvider";
import { StorageTreeItemProvider } from "./tree/storage/StorageTreeItemProvider"; import { StorageTreeItemProvider } from "./tree/storage/StorageTreeItemProvider";
import { UserTreeItemProvider } from "./tree/users/UserTreeItemProvider"; import { UserTreeItemProvider } from "./tree/users/UserTreeItemProvider";
import { createAppwriteOutputChannel } from "./ui/AppwriteOutputChannel"; import { createAppwriteOutputChannel } from "./ui/AppwriteOutputChannel";
@ -25,9 +24,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.window.registerTreeDataProvider("Projects", projectsTreeItemProvider); vscode.window.registerTreeDataProvider("Projects", projectsTreeItemProvider);
const activeProject = await getActiveProjectConfiguration(); const activeProject = await getActiveProjectConfiguration();
if (activeProject) { createAppwriteClient(activeProject);
initAppwriteClient(activeProject);
}
ext.context = context; ext.context = context;
ext.outputChannel = createAppwriteOutputChannel("Appwrite", "appwrite"); ext.outputChannel = createAppwriteOutputChannel("Appwrite", "appwrite");
@ -37,7 +34,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
health: healthTreeItemProvider, health: healthTreeItemProvider,
database: databaseTreeItemProvider, database: databaseTreeItemProvider,
storage: storageTreeItemProvider, storage: storageTreeItemProvider,
projects: projectsTreeItemProvider projects: projectsTreeItemProvider,
}; };
registerCommands(context); registerCommands(context);

View file

@ -1,5 +1,5 @@
import { workspace } from "vscode"; import { workspace } from "vscode";
import { initAppwriteClient } from "./client"; import { createAppwriteClient } from "./client";
export type AppwriteProjectConfiguration = { export type AppwriteProjectConfiguration = {
nickname?: string; nickname?: string;
@ -64,9 +64,7 @@ export async function setActiveProjectId(projectId: string): Promise<void> {
const configuration = workspace.getConfiguration("appwrite"); const configuration = workspace.getConfiguration("appwrite");
await configuration.update("activeProjectId", projectId, true); await configuration.update("activeProjectId", projectId, true);
const active = await getActiveProjectConfiguration(); const active = await getActiveProjectConfiguration();
if (active) { createAppwriteClient(active);
initAppwriteClient(active);
}
} }
export async function updateActiveProjectId(): Promise<void> { export async function updateActiveProjectId(): Promise<void> {
@ -75,17 +73,12 @@ export async function updateActiveProjectId(): Promise<void> {
const configuration = workspace.getConfiguration("appwrite"); const configuration = workspace.getConfiguration("appwrite");
await configuration.update("activeProjectId", projects[0].projectId, true); await configuration.update("activeProjectId", projects[0].projectId, true);
const active = await getActiveProjectConfiguration(); const active = await getActiveProjectConfiguration();
if (active) { createAppwriteClient(active);
initAppwriteClient(active);
}
} }
} }
export async function removeProjectConfig(projectId: string): Promise<void> { export async function removeProjectConfig(projectId: string): Promise<void> {
const projects = await getAppwriteProjects(); const projects = await getAppwriteProjects();
const activeProjectId = await getActiveProjectId();
const updatedProjects = projects.filter((project) => project.projectId !== projectId); const updatedProjects = projects.filter((project) => project.projectId !== projectId);
const configuration = workspace.getConfiguration("appwrite"); const configuration = workspace.getConfiguration("appwrite");
await configuration.update("projects", updatedProjects, true); await configuration.update("projects", updatedProjects, true);

View file

@ -1,11 +1,11 @@
import { ThemeIcon, TreeItem, TreeItemCollapsibleState } from "vscode"; import { ThemeIcon, TreeItem, TreeItemCollapsibleState } from "vscode";
import { Collection } from "../../appwrite"; import { Collection } from "../../appwrite";
import { databaseClient } from '../../client'; import { databaseClient } from "../../client";
import { AppwriteTreeItemBase } from '../../ui/AppwriteTreeItemBase'; import { AppwriteTreeItemBase } from "../../ui/AppwriteTreeItemBase";
import { DatabaseTreeItemProvider } from './DatabaseTreeItemProvider'; import { DatabaseTreeItemProvider } from "./DatabaseTreeItemProvider";
import { DocumentsTreeItem } from './DocumentsTreeItem'; import { DocumentsTreeItem } from "./DocumentsTreeItem";
import { PermissionsTreeItem } from './settings/PermissionsTreeItem'; import { PermissionsTreeItem } from "./settings/PermissionsTreeItem";
import { RulesTreeItem } from './settings/RulesTreeItem'; import { RulesTreeItem } from "./settings/RulesTreeItem";
export class CollectionTreeItem extends AppwriteTreeItemBase { export class CollectionTreeItem extends AppwriteTreeItemBase {
constructor(public collection: Collection, public readonly provider: DatabaseTreeItemProvider) { constructor(public collection: Collection, public readonly provider: DatabaseTreeItemProvider) {
@ -17,7 +17,10 @@ export class CollectionTreeItem extends AppwriteTreeItemBase {
} }
public async refresh(): Promise<void> { public async refresh(): Promise<void> {
this.collection = await databaseClient.getCollection(this.collection.$id) ?? this.collection; if (!databaseClient) {
return;
}
this.collection = (await databaseClient.getCollection(this.collection.$id)) ?? this.collection;
this.provider.refreshChild(this); this.provider.refreshChild(this);
} }
@ -25,5 +28,5 @@ export class CollectionTreeItem extends AppwriteTreeItemBase {
contextValue = "collection"; contextValue = "collection";
iconPath = new ThemeIcon('folder'); iconPath = new ThemeIcon("folder");
} }