| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import { Context } from '@nuxt/types/app'
- import { hooks } from '~/services/dataDeleter/hook/_import'
- import { DataDeleterArgs } from '~/types/interfaces'
- 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<any> {
- 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<any> {
- 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
|