import {hooks} from "~/services/dataDeleter/hook/_import"; import {DataDeleterArgs} from "~/types/interfaces"; import {Context} from "@nuxt/types/app"; import Connection from "~/services/connection/connection"; import ApiError from "~/services/utils/apiError"; import {HTTP_METHOD} from "~/types/enums"; import ConstructUrl from "~/services/connection/constructUrl"; import {repositoryHelper} from "~/services/store/repository"; class DataDeleter{ private ctx !: Context private arguments!: DataDeleterArgs constructor() { this.sort() } initCtx(ctx:Context){ Connection.initConnector(ctx.$axios) this.ctx = ctx } async invoke(args:DataDeleterArgs): Promise{ this.arguments = args try{ this.preHook() const url = this.constructUrl() const response = await this.connection(url) if(this.arguments.model) repositoryHelper.deleteItem(this.arguments.model, this.arguments.id) }catch(error){ throw new ApiError(error.response.status, error.response.data.detail) } } async preHook(){ for(const hook of hooks){ if(hook.support(this.arguments)){ await new hook().invoke(this.arguments) } } } constructUrl(): string{ const constructUrl = new ConstructUrl() return constructUrl.invoke(this.arguments) } connection(url: string): Promise{ const connection = new Connection() return connection.invoke(HTTP_METHOD.DELETE, url, this.arguments) } sort(){ hooks.sort(function(a, b) { if (a.priority > b.priority) { return 1 } if (a.priority < b.priority) { return -1 } return 0 }); } } export default DataDeleter