2023-04-18 03:41:55 -07:00
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
import { computed, ref } from 'vue';
|
2023-04-24 03:18:24 -07:00
|
|
|
import type { EnvironmentVariable } from '@/Interface';
|
2023-04-18 03:41:55 -07:00
|
|
|
import * as environmentsApi from '@/api/environments.ee';
|
2023-05-05 01:41:54 -07:00
|
|
|
import { useRootStore } from '@/stores/n8nRoot.store';
|
2023-04-18 03:41:55 -07:00
|
|
|
|
|
|
|
export const useEnvironmentsStore = defineStore('environments', () => {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
|
|
|
|
const variables = ref<EnvironmentVariable[]>([]);
|
|
|
|
|
|
|
|
async function fetchAllVariables() {
|
|
|
|
const data = await environmentsApi.getVariables(rootStore.getRestApiContext);
|
|
|
|
|
|
|
|
variables.value = data;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createVariable(variable: Omit<EnvironmentVariable, 'id'>) {
|
|
|
|
const data = await environmentsApi.createVariable(rootStore.getRestApiContext, variable);
|
|
|
|
|
|
|
|
variables.value.unshift(data);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateVariable(variable: EnvironmentVariable) {
|
|
|
|
const data = await environmentsApi.updateVariable(rootStore.getRestApiContext, variable);
|
|
|
|
|
|
|
|
variables.value = variables.value.map((v) => (v.id === data.id ? data : v));
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteVariable(variable: EnvironmentVariable) {
|
|
|
|
const data = await environmentsApi.deleteVariable(rootStore.getRestApiContext, {
|
|
|
|
id: variable.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
variables.value = variables.value.filter((v) => v.id !== variable.id);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const variablesAsObject = computed(() =>
|
|
|
|
variables.value.reduce<Record<string, string | boolean | number>>((acc, variable) => {
|
|
|
|
acc[variable.key] = variable.value;
|
|
|
|
return acc;
|
|
|
|
}, {}),
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
variables,
|
|
|
|
variablesAsObject,
|
|
|
|
fetchAllVariables,
|
|
|
|
createVariable,
|
|
|
|
updateVariable,
|
|
|
|
deleteVariable,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
export default useEnvironmentsStore;
|