dataPersister.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import {hooks} from "~/services/dataPersister/hook/_import";
  2. import {DataPersisterArgs} 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} from "~/types/enums";
  7. class DataPersister{
  8. private ctx !: Context;
  9. private arguments!: DataPersisterArgs;
  10. constructor() {
  11. this.sort()
  12. }
  13. initCtx(ctx:Context){
  14. Connection.initConnector(ctx.$axios)
  15. this.ctx = ctx
  16. }
  17. async invoke(args:DataPersisterArgs): Promise<any>{
  18. this.arguments = args
  19. try{
  20. this.preHook()
  21. await this.serialization()
  22. const url = this.constructUrl()
  23. const response = await this.connection(url)
  24. }catch(error){
  25. throw new Error(error.message);
  26. }
  27. }
  28. async preHook(){
  29. for(const hook of hooks){
  30. if(hook.support(this.arguments)){
  31. await new hook().invoke(this.arguments);
  32. }
  33. }
  34. }
  35. serialization(){
  36. console.log('serialization')
  37. }
  38. constructUrl(): string{
  39. const constructUrl = new ConstructUrl(this.ctx);
  40. return constructUrl.invoke(this.arguments)
  41. }
  42. connection(url: string): Promise<any>{
  43. const connection = new Connection()
  44. return connection.invoke(HTTP_METHOD.GET, 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 DataPersister