dataDeleter.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { Context } from '@nuxt/types/app'
  2. import { hooks } from '~/services/dataDeleter/hook/_import'
  3. import { DataDeleterArgs } from '~/types/interfaces'
  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) { repositoryHelper.deleteItem(this.arguments.model, this.arguments.id) }
  26. } catch (error) {
  27. throw new ApiError(error.response.status, error.response.data.detail)
  28. }
  29. }
  30. async preHook () {
  31. for (const hook of hooks) {
  32. if (hook.support(this.arguments)) {
  33. await new hook().invoke(this.arguments)
  34. }
  35. }
  36. }
  37. constructUrl (): string {
  38. const constructUrl = new ConstructUrl()
  39. return constructUrl.invoke(this.arguments)
  40. }
  41. connection (url: string): Promise<any> {
  42. const connection = new Connection()
  43. return connection.invoke(HTTP_METHOD.DELETE, url, this.arguments)
  44. }
  45. sort () {
  46. hooks.sort(function (a, b) {
  47. if (a.priority > b.priority) {
  48. return 1
  49. }
  50. if (a.priority < b.priority) {
  51. return -1
  52. }
  53. return 0
  54. })
  55. }
  56. }
  57. export default DataDeleter