frontend/classes/ResourceRepo.ts

55 lines
1.2 KiB
TypeScript

import axios from "axios";
export class ResourceRepo<T>
{
private resources: Ref<T[] | undefined> = ref(undefined);
private interval: NodeJS.Timeout | undefined = undefined;
static init<T>()
{
return new ResourceRepo<T>();
}
load(resourceType: string)
{
this.refresh(resourceType);
return this;
}
clear()
{
clearTimeout(this.interval);
}
get()
{
return computed(() => {
return this.resources.value;
})
}
private refresh(resourceType: string)
{
const namespace = this.getNamespace();
let url = useRuntimeConfig().public.apiBase + '/resources/' + resourceType
if (namespace)
{
url = url + "/" + namespace;
}
axios.get<T[]>(url)
.then((response) => {
this.resources.value = undefined;
this.resources.value = response.data;
});
}
private getNamespace()
{
const namespace = useRoute().params.namespace as string;
if (namespace !== "_all")
{
return namespace as string;
}
return undefined;
}
}