dataPersister.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {hooks} from "~/services/dataPersister/hook/_import";
  2. import {AnyJson, DataPersisterArgs, DataProviderArgs} from "~/types/interfaces";
  3. import {Context} from "@nuxt/types/app";
  4. import Connection from "~/services/connection/connection";
  5. import ConstructUrl from "~/services/connection/constructUrl";
  6. import {HTTP_METHOD, QUERY_TYPE} from "~/types/enums";
  7. import Serializer from "~/services/serializer/serializer";
  8. import ApiError from "~/services/utils/apiError";
  9. import DataProvider from "~/services/dataProvider/dataProvider";
  10. class DataPersister{
  11. private ctx !: Context
  12. private arguments: DataPersisterArgs
  13. constructor() {
  14. this.sort()
  15. this.arguments = {
  16. type: QUERY_TYPE.MODEL,
  17. progress:true
  18. }
  19. }
  20. initCtx(ctx:Context){
  21. Connection.initConnector(ctx.$axios)
  22. this.ctx = ctx
  23. }
  24. setArguments(args: DataProviderArgs){
  25. this.arguments = { ...this.arguments, ...args }
  26. }
  27. async invoke(args:DataPersisterArgs): Promise<any>{
  28. try{
  29. this.setArguments(args)
  30. this.startLoading()
  31. this.preHook()
  32. this.arguments.data = this.serialization()
  33. const url = this.constructUrl()
  34. const response = await this.connection(url)
  35. this.provideResponse(response)
  36. }catch(error){
  37. throw new ApiError(error.response.status, error.response.data.detail)
  38. }
  39. }
  40. startLoading(){
  41. if(this.arguments.progress){
  42. const $nuxt = window['$nuxt']
  43. $nuxt.$loading.start()
  44. }
  45. }
  46. async preHook(){
  47. for(const hook of hooks){
  48. if(hook.support(this.arguments)){
  49. await new hook().invoke(this.arguments)
  50. }
  51. }
  52. }
  53. serialization(){
  54. const serializer = new Serializer()
  55. return serializer.normalize(this.arguments)
  56. }
  57. constructUrl(): string{
  58. const constructUrl = new ConstructUrl()
  59. return constructUrl.invoke(this.arguments)
  60. }
  61. connection(url: string): Promise<any>{
  62. const connection = new Connection()
  63. return connection.invoke(this.arguments.id ? HTTP_METHOD.PUT : HTTP_METHOD.POST, url, this.arguments)
  64. }
  65. async provideResponse(response: AnyJson){
  66. const dataProvider = new DataProvider()
  67. dataProvider.setArguments({
  68. type: this.arguments.type,
  69. url: this.arguments.url,
  70. enumType: this.arguments.enumType,
  71. model: this.arguments.model,
  72. root_model: this.arguments.root_model,
  73. id: this.arguments.id,
  74. root_id: this.arguments.root_id
  75. })
  76. const deserializeResponse = dataProvider.deserialization(response)
  77. return await dataProvider.provide(deserializeResponse)
  78. }
  79. sort(){
  80. hooks.sort(function(a, b) {
  81. if (a.priority > b.priority) {
  82. return 1
  83. }
  84. if (a.priority < b.priority) {
  85. return -1
  86. }
  87. return 0
  88. });
  89. }
  90. }
  91. export default DataPersister