dataDeleter.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {hooks} from "~/services/dataDeleter/hook/_import";
  2. import {DataDeleterArgs} from "~/types/interfaces";
  3. import {Context} from "@nuxt/types/app";
  4. import Connection from "~/services/connection/connection";
  5. import ApiError from "~/services/utils/apiError";
  6. import {HTTP_METHOD} from "~/types/enums";
  7. import ConstructUrl from "~/services/connection/constructUrl";
  8. import {repositoryHelper} from "~/services/store/repository";
  9. class DataDeleter{
  10. private ctx !: Context
  11. private arguments!: DataDeleterArgs
  12. constructor() {
  13. this.sort()
  14. }
  15. initCtx(ctx:Context){
  16. Connection.initConnector(ctx.$axios)
  17. this.ctx = ctx
  18. }
  19. async invoke(args:DataDeleterArgs): Promise<any>{
  20. this.arguments = args
  21. try{
  22. this.preHook()
  23. const url = this.constructUrl()
  24. const response = await this.connection(url)
  25. if(this.arguments.model)
  26. repositoryHelper.deleteItem(this.arguments.model, this.arguments.id)
  27. }catch(error){
  28. throw new ApiError(error.response.status, error.response.data.detail)
  29. }
  30. }
  31. async preHook(){
  32. for(const hook of hooks){
  33. if(hook.support(this.arguments)){
  34. await new hook().invoke(this.arguments)
  35. }
  36. }
  37. }
  38. constructUrl(): string{
  39. const constructUrl = new ConstructUrl()
  40. return constructUrl.invoke(this.arguments)
  41. }
  42. connection(url: string): Promise<any>{
  43. const connection = new Connection()
  44. return connection.invoke(HTTP_METHOD.DELETE, url, this.arguments)
  45. }
  46. sort(){
  47. hooks.sort(function(a, b) {
  48. if (a.priority > b.priority) {
  49. return 1
  50. }
  51. if (a.priority < b.priority) {
  52. return -1
  53. }
  54. return 0
  55. });
  56. }
  57. }
  58. export default DataDeleter