import { defineStore } from "pinia"; import type { CreateWebapiViewModel, UpdateWebapiViewModel, WebapiViewModel, } from "@/viewmodels/admin/user/webapi.models"; import { http } from "@/serverCom"; import type { PermissionObject } from "@/types/permissionTypes"; import type { AxiosResponse } from "axios"; export const useWebapiStore = defineStore("webapi", { state: () => { return { webapis: [] as Array, loading: null as null | "loading" | "success" | "failed", }; }, actions: { fetchWebapis() { this.loading = "loading"; http .get("/admin/webapi") .then((result) => { this.webapis = result.data; this.loading = "success"; }) .catch((err) => { this.loading = "failed"; }); }, fetchWebapiById(id: number): Promise> { return http.get(`/admin/webapi/${id}`); }, fetchWebapiTokenById(id: number): Promise> { return http.get(`/admin/webapi/${id}/token`); }, async createWebapi(webapi: CreateWebapiViewModel): Promise> { const result = await http.post("/admin/webapi", webapi); this.fetchWebapis(); return result; }, async updateActiveWebapi(id: number, webapi: UpdateWebapiViewModel): Promise> { const result = await http.patch(`/admin/webapi/${id}`, webapi); this.fetchWebapis(); return result; }, async updateActiveWebapiPermissions( webapi: number, permission: PermissionObject ): Promise> { const result = await http.patch(`/admin/webapi/${webapi}/permissions`, { permissions: permission, }); this.fetchWebapis(); return result; }, async deleteWebapi(webapi: number): Promise> { const result = await http.delete(`/admin/webapi/${webapi}`); this.fetchWebapis(); return result; }, }, });