2022-12-14 01:04:10 -08:00
|
|
|
import {
|
|
|
|
getInstalledCommunityNodes,
|
|
|
|
installNewPackage,
|
|
|
|
uninstallPackage,
|
|
|
|
updatePackage,
|
|
|
|
} from '@/api/communityNodes';
|
|
|
|
import { getAvailableCommunityPackageCount } from '@/api/settings';
|
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
import { useRootStore } from './n8nRootStore';
|
2023-04-24 03:18:24 -07:00
|
|
|
import type { PublicInstalledPackage } from 'n8n-workflow';
|
2022-12-14 01:04:10 -08:00
|
|
|
import Vue from 'vue';
|
2023-04-24 03:18:24 -07:00
|
|
|
import type { CommunityNodesState, CommunityPackageMap } from '@/Interface';
|
2022-12-14 01:04:10 -08:00
|
|
|
import { STORES } from '@/constants';
|
2022-11-04 06:04:31 -07:00
|
|
|
|
|
|
|
const LOADER_DELAY = 300;
|
|
|
|
|
|
|
|
export const useCommunityNodesStore = defineStore(STORES.COMMUNITY_NODES, {
|
|
|
|
state: (): CommunityNodesState => ({
|
|
|
|
// -1 means that package count has not been fetched yet
|
|
|
|
availablePackageCount: -1,
|
|
|
|
installedPackages: {},
|
|
|
|
}),
|
|
|
|
getters: {
|
2022-12-14 01:04:10 -08:00
|
|
|
getInstalledPackages(): PublicInstalledPackage[] {
|
|
|
|
return Object.values(this.installedPackages).sort((a, b) =>
|
|
|
|
a.packageName.localeCompare(b.packageName),
|
|
|
|
);
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
|
|
|
getInstalledPackageByName() {
|
|
|
|
return (name: string): PublicInstalledPackage => this.installedPackages[name];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
async fetchAvailableCommunityPackageCount(): Promise<void> {
|
|
|
|
if (this.availablePackageCount === -1) {
|
|
|
|
this.availablePackageCount = await getAvailableCommunityPackageCount();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async fetchInstalledPackages(): Promise<void> {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
const installedPackages = await getInstalledCommunityNodes(rootStore.getRestApiContext);
|
|
|
|
this.setInstalledPackages(installedPackages);
|
2022-12-14 01:04:10 -08:00
|
|
|
const timeout = installedPackages.length > 0 ? 0 : LOADER_DELAY;
|
2022-11-04 06:04:31 -07:00
|
|
|
setTimeout(() => {
|
|
|
|
return;
|
|
|
|
}, timeout);
|
|
|
|
},
|
|
|
|
async installPackage(packageName: string): Promise<void> {
|
|
|
|
try {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
await installNewPackage(rootStore.getRestApiContext, packageName);
|
|
|
|
await this.fetchInstalledPackages();
|
|
|
|
} catch (error) {
|
2022-12-14 01:04:10 -08:00
|
|
|
throw error;
|
2022-11-04 06:04:31 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
async uninstallPackage(packageName: string): Promise<void> {
|
|
|
|
try {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
await uninstallPackage(rootStore.getRestApiContext, packageName);
|
|
|
|
this.removePackageByName(packageName);
|
|
|
|
} catch (error) {
|
2022-12-14 01:04:10 -08:00
|
|
|
throw error;
|
2022-11-04 06:04:31 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
async updatePackage(packageName: string): Promise<void> {
|
|
|
|
try {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
const packageToUpdate: PublicInstalledPackage = this.getInstalledPackageByName(packageName);
|
2022-12-14 01:04:10 -08:00
|
|
|
const updatedPackage: PublicInstalledPackage = await updatePackage(
|
|
|
|
rootStore.getRestApiContext,
|
|
|
|
packageToUpdate.packageName,
|
|
|
|
);
|
2022-11-04 06:04:31 -07:00
|
|
|
this.updatePackageObject(updatedPackage);
|
|
|
|
} catch (error) {
|
2022-12-14 01:04:10 -08:00
|
|
|
throw error;
|
2022-11-04 06:04:31 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
setInstalledPackages(packages: PublicInstalledPackage[]) {
|
2022-12-14 01:04:10 -08:00
|
|
|
this.installedPackages = packages.reduce(
|
|
|
|
(packageMap: CommunityPackageMap, pack: PublicInstalledPackage) => {
|
|
|
|
packageMap[pack.packageName] = pack;
|
|
|
|
return packageMap;
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
);
|
2022-11-04 06:04:31 -07:00
|
|
|
},
|
|
|
|
removePackageByName(name: string): void {
|
|
|
|
Vue.delete(this.installedPackages, name);
|
|
|
|
},
|
|
|
|
updatePackageObject(newPackage: PublicInstalledPackage) {
|
|
|
|
this.installedPackages[newPackage.packageName] = newPackage;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|