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 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;
}

View file

@ -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();
}
}

View file

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

View file

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

View file

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

View file

@ -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<C
}
export async function createPermission(treeItem: PermissionsTreeItem): Promise<void> {
if (!databaseClient) {
return;
}
const collection = treeItem.parent.collection;
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 write = Array.from(collection.$permissions.write);
if (context.kind === 'read') {
if (context.kind === "read") {
read.push(context.permission);
} else {
write.push(context.permission);

View file

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

View file

@ -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<void> {
if (!databaseClient) {
return;
}
const editedPermission = await window.showInputBox({
value: treeItem.permission,
});

View file

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

View file

@ -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.");

View file

@ -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;
}

View file

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

View file

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

View file

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

View file

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

View file

@ -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<void>
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<void>
health: healthTreeItemProvider,
database: databaseTreeItemProvider,
storage: storageTreeItemProvider,
projects: projectsTreeItemProvider
projects: projectsTreeItemProvider,
};
registerCommands(context);

View file

@ -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<void> {
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<void> {
@ -75,17 +73,12 @@ export async function updateActiveProjectId(): Promise<void> {
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<void> {
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);

View file

@ -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<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);
}
@ -25,5 +28,5 @@ export class CollectionTreeItem extends AppwriteTreeItemBase {
contextValue = "collection";
iconPath = new ThemeIcon('folder');
iconPath = new ThemeIcon("folder");
}