From cbd6cf1d1ab33fbb5fe5d9df7fcf5483f9c88491 Mon Sep 17 00:00:00 2001 From: alexweininger Date: Fri, 30 Apr 2021 02:34:03 -0700 Subject: [PATCH] Always check if client is defined --- src/client.ts | 22 ++++++++++++++----- src/commands/connectAppwrite.ts | 4 ++-- src/commands/database/createCollection.ts | 12 ++++++---- src/commands/database/createRule.ts | 5 +++++ src/commands/database/deleteCollection.ts | 3 +++ src/commands/database/deleteDocument.ts | 3 +++ .../database/permissions/createPermission.ts | 10 +++++---- .../database/permissions/deletePermission.ts | 3 +++ .../database/permissions/editPermission.ts | 9 +++++--- src/commands/database/removeRule.ts | 3 +++ src/commands/project/addProject.ts | 4 ++-- src/commands/project/removeProject.ts | 9 +++----- src/commands/project/setActiveProject.ts | 2 -- src/commands/users/createUser.ts | 3 +++ src/commands/users/deleteUser.ts | 3 +++ src/commands/users/getUserLogs.ts | 3 +++ src/extension.ts | 11 ++++------ src/settings.ts | 13 +++-------- src/tree/database/CollectionTreeItem.ts | 19 +++++++++------- 19 files changed, 88 insertions(+), 53 deletions(-) diff --git a/src/client.ts b/src/client.ts index 2dd16bd..ed9a65b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -9,12 +9,12 @@ const sdk: SDK = require("node-appwrite"); export let client: Client; export let clientConfig: { endpoint: string; projectId: string; secret: string }; -export let usersClient: Users; -export let healthClient: Health; -export let databaseClient: Database; -export let storageClient: Storage; +export let usersClient: Users | undefined; +export let healthClient: Health | undefined; +export let databaseClient: Database | undefined; +export let storageClient: Storage | undefined; -export function initAppwriteClient({ endpoint, projectId, secret }: AppwriteProjectConfiguration) { +function initAppwriteClient({ endpoint, projectId, secret }: AppwriteProjectConfiguration) { client = new sdk.Client(); clientConfig = { endpoint, projectId, secret }; client.setEndpoint(endpoint).setProject(projectId).setKey(secret); @@ -26,3 +26,15 @@ export function initAppwriteClient({ endpoint, projectId, secret }: AppwriteProj return client; } + +export function createAppwriteClient(config?: AppwriteProjectConfiguration) { + if (config) { + initAppwriteClient(config); + return; + } + + usersClient = undefined; + healthClient = undefined; + databaseClient = undefined; + storageClient = undefined; +} diff --git a/src/commands/connectAppwrite.ts b/src/commands/connectAppwrite.ts index 20ed863..adb2e29 100644 --- a/src/commands/connectAppwrite.ts +++ b/src/commands/connectAppwrite.ts @@ -1,4 +1,4 @@ -import { initAppwriteClient } from "../client"; +import { createAppwriteClient } from "../client"; import { addProjectConfiguration } from '../settings'; import { addProjectWizard } from "../ui/AddProjectWizard"; import { refreshTree } from '../utils/refreshTree'; @@ -7,7 +7,7 @@ export async function connectAppwrite() { const projectConfiguration = await addProjectWizard(); if (projectConfiguration) { addProjectConfiguration(projectConfiguration); - initAppwriteClient(projectConfiguration); + createAppwriteClient(projectConfiguration); refreshTree(); } } diff --git a/src/commands/database/createCollection.ts b/src/commands/database/createCollection.ts index 180b84c..4973b37 100644 --- a/src/commands/database/createCollection.ts +++ b/src/commands/database/createCollection.ts @@ -1,13 +1,17 @@ -import { window } from 'vscode'; -import { databaseClient } from '../../client'; +import { window } from "vscode"; +import { databaseClient } from "../../client"; export async function createCollection(): Promise { + if (!databaseClient) { + return; + } + const name = await window.showInputBox({ - prompt: 'Collection name' + prompt: "Collection name", }); if (name && name.length > 0) { - await databaseClient.createCollection({name}); + await databaseClient.createCollection({ name }); window.showInformationMessage(`Created collection "${name}".`); } } diff --git a/src/commands/database/createRule.ts b/src/commands/database/createRule.ts index 640a284..f6dc0bc 100644 --- a/src/commands/database/createRule.ts +++ b/src/commands/database/createRule.ts @@ -5,6 +5,11 @@ import { createRuleWizard } from "../../ui/createRuleWizard"; import { refreshTree } from '../../utils/refreshTree'; export async function createRule(rulesTreeItem: RulesTreeItem): Promise { + + if (!databaseClient) { + return; + } + const ruleContext = await createRuleWizard(); const collection = rulesTreeItem.parent.collection; diff --git a/src/commands/database/deleteCollection.ts b/src/commands/database/deleteCollection.ts index 68c1e4f..fe1458f 100644 --- a/src/commands/database/deleteCollection.ts +++ b/src/commands/database/deleteCollection.ts @@ -4,6 +4,9 @@ import { CollectionTreeItem } from "../../tree/database/CollectionTreeItem"; import { confirmDialog } from "../../ui/confirmDialog"; export async function deleteCollection(collectionTreeItem: CollectionTreeItem): Promise { + if (!databaseClient) { + return; + } const collection = collectionTreeItem.collection; try { const shouldDelete = await confirmDialog(`Delete collection "${collection.name}"?`); diff --git a/src/commands/database/deleteDocument.ts b/src/commands/database/deleteDocument.ts index a572c68..221e948 100644 --- a/src/commands/database/deleteDocument.ts +++ b/src/commands/database/deleteDocument.ts @@ -4,6 +4,9 @@ import { DocumentTreeItem } from "../../tree/database/DocumentTreeItem"; import { confirmDialog } from "../../ui/confirmDialog"; export async function deleteDocument(documentTreeItem: DocumentTreeItem): Promise { + if (!databaseClient) { + return; + } const document = documentTreeItem.document; const collection = documentTreeItem.parent.parent.collection; try { diff --git a/src/commands/database/permissions/createPermission.ts b/src/commands/database/permissions/createPermission.ts index 0502151..18f0226 100644 --- a/src/commands/database/permissions/createPermission.ts +++ b/src/commands/database/permissions/createPermission.ts @@ -1,6 +1,6 @@ import { window } from "vscode"; -import { databaseClient } from '../../../client'; -import { CollapsableTreeItem } from '../../../tree/CollapsableTreeItem'; +import { databaseClient } from "../../../client"; +import { CollapsableTreeItem } from "../../../tree/CollapsableTreeItem"; import { PermissionsTreeItem } from "../../../tree/database/settings/PermissionsTreeItem"; import { PermissionTreeItem } from "../../../tree/database/settings/PermissionTreeItem"; @@ -25,7 +25,9 @@ export async function createPermissionWizard(kind?: "read" | "write"): Promise { - + if (!databaseClient) { + return; + } const collection = treeItem.parent.collection; const context = await createPermissionWizard(undefined); @@ -37,7 +39,7 @@ export async function createPermission(treeItem: PermissionsTreeItem): Promise { + if (!databaseClient) { + return; + } const collection = treeItem.parent.parent.collection; const kind = treeItem.kind; diff --git a/src/commands/database/permissions/editPermission.ts b/src/commands/database/permissions/editPermission.ts index 757ed92..81f5aa1 100644 --- a/src/commands/database/permissions/editPermission.ts +++ b/src/commands/database/permissions/editPermission.ts @@ -1,8 +1,11 @@ -import { window } from 'vscode'; -import { databaseClient } from '../../../client'; -import { PermissionTreeItem } from '../../../tree/database/settings/PermissionTreeItem'; +import { window } from "vscode"; +import { databaseClient } from "../../../client"; +import { PermissionTreeItem } from "../../../tree/database/settings/PermissionTreeItem"; export async function editPermission(treeItem: PermissionTreeItem): Promise { + if (!databaseClient) { + return; + } const editedPermission = await window.showInputBox({ value: treeItem.permission, }); diff --git a/src/commands/database/removeRule.ts b/src/commands/database/removeRule.ts index d69a1cf..5c455d5 100644 --- a/src/commands/database/removeRule.ts +++ b/src/commands/database/removeRule.ts @@ -3,6 +3,9 @@ import { RuleTreeItem } from '../../tree/database/settings/RuleTreeItem'; import { refreshTree } from '../../utils/refreshTree'; export async function removeRule(ruleItem: RuleTreeItem): Promise { + if (!databaseClient) { + return; + } const rule = ruleItem.rule; const collection = ruleItem.parent.parent.collection; await databaseClient.removeRule(collection, rule); diff --git a/src/commands/project/addProject.ts b/src/commands/project/addProject.ts index 5b22e76..8a6e0a9 100644 --- a/src/commands/project/addProject.ts +++ b/src/commands/project/addProject.ts @@ -1,12 +1,12 @@ import { window } from "vscode"; -import { initAppwriteClient } from "../../client"; +import { createAppwriteClient } from '../../client'; import { addProjectWizard } from "../../ui/AddProjectWizard"; export async function addProject() { const projectConfiguration = await addProjectWizard(); if (projectConfiguration) { - initAppwriteClient(projectConfiguration); + createAppwriteClient(projectConfiguration); } window.showInformationMessage("Connected to Appwrite project."); diff --git a/src/commands/project/removeProject.ts b/src/commands/project/removeProject.ts index 77c875e..6affe16 100644 --- a/src/commands/project/removeProject.ts +++ b/src/commands/project/removeProject.ts @@ -1,11 +1,8 @@ -import { window } from "vscode"; -import { initAppwriteClient } from "../../client"; -import { removeProjectConfig } from '../../settings'; -import { ProjectTreeItem } from '../../tree/projects/ProjectTreeItem'; -import { addProjectWizard } from "../../ui/AddProjectWizard"; +import { removeProjectConfig } from "../../settings"; +import { ProjectTreeItem } from "../../tree/projects/ProjectTreeItem"; export async function removeProject(project: ProjectTreeItem | string) { - if (typeof project === 'string') { + if (typeof project === "string") { await removeProjectConfig(project); return; } diff --git a/src/commands/project/setActiveProject.ts b/src/commands/project/setActiveProject.ts index fcf4d76..ede1c01 100644 --- a/src/commands/project/setActiveProject.ts +++ b/src/commands/project/setActiveProject.ts @@ -1,5 +1,3 @@ -import { window } from "vscode"; -import { initAppwriteClient } from "../../client"; import { setActiveProjectId } from '../../settings'; import { ProjectTreeItem } from "../../tree/projects/ProjectTreeItem"; diff --git a/src/commands/users/createUser.ts b/src/commands/users/createUser.ts index 55dbdf9..38fc112 100644 --- a/src/commands/users/createUser.ts +++ b/src/commands/users/createUser.ts @@ -3,6 +3,9 @@ import { usersClient } from '../../client'; import { ext } from '../../extensionVariables'; export async function createUser(): Promise { + if (!usersClient) { + return; + } const email = await window.showInputBox({ ignoreFocusOut: true, placeHolder: "jane.doe@hotmail.com", diff --git a/src/commands/users/deleteUser.ts b/src/commands/users/deleteUser.ts index bbf7ea5..3c17a64 100644 --- a/src/commands/users/deleteUser.ts +++ b/src/commands/users/deleteUser.ts @@ -5,6 +5,9 @@ import { DialogResponses } from "../../ui/DialogResponses"; import { refreshTree } from "../../utils/refreshTree"; export async function deleteUser(userTreeItem: UserTreeItem): Promise { + if (!usersClient) { + return; + } const user = userTreeItem.user; const userId = user.$id; const shouldDeleteUser = await window.showWarningMessage( diff --git a/src/commands/users/getUserLogs.ts b/src/commands/users/getUserLogs.ts index ff0f1ab..93a4f46 100644 --- a/src/commands/users/getUserLogs.ts +++ b/src/commands/users/getUserLogs.ts @@ -3,6 +3,9 @@ import { UserTreeItem } from "../../tree/users/UserTreeItem"; import { openReadOnlyJson } from '../../ui/openReadonlyContent'; export async function getUserLogs(treeItem: UserTreeItem): Promise { + if (!usersClient) { + return; + } const userId = treeItem.user.$id; const logs = await usersClient.getLogs(userId); diff --git a/src/extension.ts b/src/extension.ts index 3bd7da2..c0273fb 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,12 +1,11 @@ import * as vscode from "vscode"; -import { workspace } from 'vscode'; -import { initAppwriteClient } from "./client"; +import { createAppwriteClient } from "./client"; import { registerCommands } from "./commands/registerCommands"; import { ext } from "./extensionVariables"; import { getActiveProjectConfiguration, getActiveProjectId, getDefaultProject } from "./settings"; import { DatabaseTreeItemProvider } from "./tree/database/DatabaseTreeItemProvider"; 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 { UserTreeItemProvider } from "./tree/users/UserTreeItemProvider"; import { createAppwriteOutputChannel } from "./ui/AppwriteOutputChannel"; @@ -25,9 +24,7 @@ export async function activate(context: vscode.ExtensionContext): Promise vscode.window.registerTreeDataProvider("Projects", projectsTreeItemProvider); const activeProject = await getActiveProjectConfiguration(); - if (activeProject) { - initAppwriteClient(activeProject); - } + createAppwriteClient(activeProject); ext.context = context; ext.outputChannel = createAppwriteOutputChannel("Appwrite", "appwrite"); @@ -37,7 +34,7 @@ export async function activate(context: vscode.ExtensionContext): Promise health: healthTreeItemProvider, database: databaseTreeItemProvider, storage: storageTreeItemProvider, - projects: projectsTreeItemProvider + projects: projectsTreeItemProvider, }; registerCommands(context); diff --git a/src/settings.ts b/src/settings.ts index 100d600..55bd14b 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,5 +1,5 @@ import { workspace } from "vscode"; -import { initAppwriteClient } from "./client"; +import { createAppwriteClient } from "./client"; export type AppwriteProjectConfiguration = { nickname?: string; @@ -64,9 +64,7 @@ export async function setActiveProjectId(projectId: string): Promise { const configuration = workspace.getConfiguration("appwrite"); await configuration.update("activeProjectId", projectId, true); const active = await getActiveProjectConfiguration(); - if (active) { - initAppwriteClient(active); - } + createAppwriteClient(active); } export async function updateActiveProjectId(): Promise { @@ -75,17 +73,12 @@ export async function updateActiveProjectId(): Promise { const configuration = workspace.getConfiguration("appwrite"); await configuration.update("activeProjectId", projects[0].projectId, true); const active = await getActiveProjectConfiguration(); - if (active) { - initAppwriteClient(active); - } + createAppwriteClient(active); } } export async function removeProjectConfig(projectId: string): Promise { const projects = await getAppwriteProjects(); - - const activeProjectId = await getActiveProjectId(); - const updatedProjects = projects.filter((project) => project.projectId !== projectId); const configuration = workspace.getConfiguration("appwrite"); await configuration.update("projects", updatedProjects, true); diff --git a/src/tree/database/CollectionTreeItem.ts b/src/tree/database/CollectionTreeItem.ts index c8ff6fc..22c3a02 100644 --- a/src/tree/database/CollectionTreeItem.ts +++ b/src/tree/database/CollectionTreeItem.ts @@ -1,11 +1,11 @@ import { ThemeIcon, TreeItem, TreeItemCollapsibleState } from "vscode"; import { Collection } from "../../appwrite"; -import { databaseClient } from '../../client'; -import { AppwriteTreeItemBase } from '../../ui/AppwriteTreeItemBase'; -import { DatabaseTreeItemProvider } from './DatabaseTreeItemProvider'; -import { DocumentsTreeItem } from './DocumentsTreeItem'; -import { PermissionsTreeItem } from './settings/PermissionsTreeItem'; -import { RulesTreeItem } from './settings/RulesTreeItem'; +import { databaseClient } from "../../client"; +import { AppwriteTreeItemBase } from "../../ui/AppwriteTreeItemBase"; +import { DatabaseTreeItemProvider } from "./DatabaseTreeItemProvider"; +import { DocumentsTreeItem } from "./DocumentsTreeItem"; +import { PermissionsTreeItem } from "./settings/PermissionsTreeItem"; +import { RulesTreeItem } from "./settings/RulesTreeItem"; export class CollectionTreeItem extends AppwriteTreeItemBase { constructor(public collection: Collection, public readonly provider: DatabaseTreeItemProvider) { @@ -17,7 +17,10 @@ export class CollectionTreeItem extends AppwriteTreeItemBase { } public async refresh(): Promise { - 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); } @@ -25,5 +28,5 @@ export class CollectionTreeItem extends AppwriteTreeItemBase { contextValue = "collection"; - iconPath = new ThemeIcon('folder'); + iconPath = new ThemeIcon("folder"); }