ff-admin/src/stores/admin/user/webapi.ts

63 lines
2 KiB
TypeScript

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<WebapiViewModel>,
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<AxiosResponse<any, any>> {
return http.get(`/admin/webapi/${id}`);
},
fetchWebapiTokenById(id: number): Promise<AxiosResponse<any, any>> {
return http.get(`/admin/webapi/${id}/token`);
},
async createWebapi(webapi: CreateWebapiViewModel): Promise<AxiosResponse<any, any>> {
const result = await http.post("/admin/webapi", webapi);
this.fetchWebapis();
return result;
},
async updateActiveWebapi(id: number, webapi: UpdateWebapiViewModel): Promise<AxiosResponse<any, any>> {
const result = await http.patch(`/admin/webapi/${id}`, webapi);
this.fetchWebapis();
return result;
},
async updateActiveWebapiPermissions(
webapi: number,
permission: PermissionObject
): Promise<AxiosResponse<any, any>> {
const result = await http.patch(`/admin/webapi/${webapi}/permissions`, {
permissions: permission,
});
this.fetchWebapis();
return result;
},
async deleteWebapi(webapi: number): Promise<AxiosResponse<any, any>> {
const result = await http.delete(`/admin/webapi/${webapi}`);
this.fetchWebapis();
return result;
},
},
});