| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import {hooks} from "~/services/dataPersister/hook/_import";
- import {DataPersisterArgs} from "~/types/interfaces";
- import {Context} from "@nuxt/types/app";
- import Connection from "~/services/connection/connection";
- import ConstructUrl from "~/services/connection/constructUrl";
- import {HTTP_METHOD} from "~/types/enums";
- class DataPersister{
- private ctx !: Context;
- private arguments!: DataPersisterArgs;
- constructor() {
- this.sort()
- }
- initCtx(ctx:Context){
- Connection.initConnector(ctx.$axios)
- this.ctx = ctx
- }
- async invoke(args:DataPersisterArgs): Promise<any>{
- this.arguments = args
- try{
- this.preHook()
- await this.serialization()
- const url = this.constructUrl()
- const response = await this.connection(url)
- }catch(error){
- throw new Error(error.message);
- }
- }
- async preHook(){
- for(const hook of hooks){
- if(hook.support(this.arguments)){
- await new hook().invoke(this.arguments);
- }
- }
- }
- serialization(){
- console.log('serialization')
- }
- constructUrl(): string{
- const constructUrl = new ConstructUrl(this.ctx);
- return constructUrl.invoke(this.arguments)
- }
- connection(url: string): Promise<any>{
- const connection = new Connection()
- return connection.invoke(HTTP_METHOD.GET, 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 DataPersister
|