dataPersister.ts 2.8 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 defaultArguments: DataPersisterArgs
  13. constructor() {
  14. this.sort()
  15. this.defaultArguments = {
  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. getArguments(args: DataPersisterArgs):DataPersisterArgs{
  25. return { ...this.defaultArguments, ...args }
  26. }
  27. async invoke(args:DataPersisterArgs): Promise<any>{
  28. try{
  29. const dpArgs = this.getArguments(args)
  30. this.startLoading(dpArgs)
  31. this.preHook(dpArgs)
  32. dpArgs.data = this.serialization(dpArgs)
  33. const url = this.constructUrl(dpArgs)
  34. const response = await this.connection(url, dpArgs)
  35. this.provideResponse(response, dpArgs)
  36. }catch(error){
  37. throw new ApiError(error.response.status, error.response.data.detail)
  38. }
  39. }
  40. startLoading(args: DataPersisterArgs){
  41. if(args.progress){
  42. const $nuxt = window['$nuxt']
  43. $nuxt.$loading.start()
  44. }
  45. }
  46. async preHook(args: DataPersisterArgs){
  47. for(const hook of hooks){
  48. if(hook.support(args)){
  49. await new hook().invoke(args)
  50. }
  51. }
  52. }
  53. serialization(args: DataPersisterArgs){
  54. const serializer = new Serializer()
  55. return serializer.normalize(args)
  56. }
  57. constructUrl(args: DataPersisterArgs): string{
  58. const constructUrl = new ConstructUrl()
  59. return constructUrl.invoke(args)
  60. }
  61. connection(url: string, args: DataPersisterArgs): Promise<any>{
  62. const connection = new Connection()
  63. return connection.invoke(args.id ? HTTP_METHOD.PUT : HTTP_METHOD.POST, url, args)
  64. }
  65. async provideResponse(response: AnyJson, args: DataPersisterArgs){
  66. const dataProvider = new DataProvider()
  67. const dataProviderArgs: DataProviderArgs = {
  68. type: args.type,
  69. url: args.url,
  70. enumType: args.enumType,
  71. model: args.model,
  72. root_model: args.root_model,
  73. id: args.id,
  74. root_id: args.root_id
  75. }
  76. const deserializeResponse = dataProvider.deserialization(response)
  77. return await dataProvider.provide(deserializeResponse, dataProviderArgs)
  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