|
|
@@ -1,19 +1,23 @@
|
|
|
import {hooks} from "~/services/dataPersister/hook/_import";
|
|
|
-import {AnyJson, DataPersisterArgs} from "~/types/interfaces";
|
|
|
+import {AnyJson, DataPersisterArgs, DataProviderArgs} 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";
|
|
|
+import {HTTP_METHOD, QUERY_TYPE} from "~/types/enums";
|
|
|
import Serializer from "~/services/serializer/serializer";
|
|
|
import ApiError from "~/services/utils/apiError";
|
|
|
import DataProvider from "~/services/dataProvider/dataProvider";
|
|
|
|
|
|
class DataPersister{
|
|
|
private ctx !: Context
|
|
|
- private arguments!: DataPersisterArgs
|
|
|
+ private defaultArguments: DataPersisterArgs
|
|
|
|
|
|
constructor() {
|
|
|
this.sort()
|
|
|
+ this.defaultArguments = {
|
|
|
+ type: QUERY_TYPE.MODEL,
|
|
|
+ progress:true
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
initCtx(ctx:Context){
|
|
|
@@ -21,59 +25,73 @@ class DataPersister{
|
|
|
this.ctx = ctx
|
|
|
}
|
|
|
|
|
|
+ getArguments(args: DataPersisterArgs):DataPersisterArgs{
|
|
|
+ return { ...this.defaultArguments, ...args }
|
|
|
+ }
|
|
|
+
|
|
|
async invoke(args:DataPersisterArgs): Promise<any>{
|
|
|
- this.arguments = args
|
|
|
try{
|
|
|
- this.preHook()
|
|
|
+ const dpArgs = this.getArguments(args)
|
|
|
+
|
|
|
+ this.startLoading(dpArgs)
|
|
|
+
|
|
|
+ this.preHook(dpArgs)
|
|
|
|
|
|
- this.arguments.data = this.serialization()
|
|
|
+ dpArgs.data = this.serialization(dpArgs)
|
|
|
|
|
|
- const url = this.constructUrl()
|
|
|
+ const url = this.constructUrl(dpArgs)
|
|
|
|
|
|
- const response = await this.connection(url)
|
|
|
+ const response = await this.connection(url, dpArgs)
|
|
|
|
|
|
- this.provideResponse(response)
|
|
|
+ this.provideResponse(response, dpArgs)
|
|
|
}catch(error){
|
|
|
throw new ApiError(error.response.status, error.response.data.detail)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async preHook(){
|
|
|
+ startLoading(args: DataPersisterArgs){
|
|
|
+ if(args.progress){
|
|
|
+ const $nuxt = window['$nuxt']
|
|
|
+ $nuxt.$loading.start()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async preHook(args: DataPersisterArgs){
|
|
|
for(const hook of hooks){
|
|
|
- if(hook.support(this.arguments)){
|
|
|
- await new hook().invoke(this.arguments)
|
|
|
+ if(hook.support(args)){
|
|
|
+ await new hook().invoke(args)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- serialization(){
|
|
|
+ serialization(args: DataPersisterArgs){
|
|
|
const serializer = new Serializer()
|
|
|
- return serializer.normalize(this.arguments)
|
|
|
+ return serializer.normalize(args)
|
|
|
}
|
|
|
|
|
|
- constructUrl(): string{
|
|
|
+ constructUrl(args: DataPersisterArgs): string{
|
|
|
const constructUrl = new ConstructUrl()
|
|
|
- return constructUrl.invoke(this.arguments)
|
|
|
+ return constructUrl.invoke(args)
|
|
|
}
|
|
|
|
|
|
- connection(url: string): Promise<any>{
|
|
|
+ connection(url: string, args: DataPersisterArgs): Promise<any>{
|
|
|
const connection = new Connection()
|
|
|
- return connection.invoke(this.arguments.id ? HTTP_METHOD.PUT : HTTP_METHOD.POST, url, this.arguments)
|
|
|
+ return connection.invoke(args.id ? HTTP_METHOD.PUT : HTTP_METHOD.POST, url, args)
|
|
|
}
|
|
|
|
|
|
- async provideResponse(response: AnyJson){
|
|
|
+ async provideResponse(response: AnyJson, args: DataPersisterArgs){
|
|
|
const dataProvider = new DataProvider()
|
|
|
- dataProvider.setArguments({
|
|
|
- type: this.arguments.type,
|
|
|
- url: this.arguments.url,
|
|
|
- enumType: this.arguments.enumType,
|
|
|
- model: this.arguments.model,
|
|
|
- root_model: this.arguments.root_model,
|
|
|
- id: this.arguments.id,
|
|
|
- root_id: this.arguments.root_id
|
|
|
- })
|
|
|
+ const dataProviderArgs: DataProviderArgs = {
|
|
|
+ type: args.type,
|
|
|
+ url: args.url,
|
|
|
+ enumType: args.enumType,
|
|
|
+ model: args.model,
|
|
|
+ root_model: args.root_model,
|
|
|
+ id: args.id,
|
|
|
+ root_id: args.root_id
|
|
|
+ }
|
|
|
const deserializeResponse = dataProvider.deserialization(response)
|
|
|
- return await dataProvider.provide(deserializeResponse)
|
|
|
+ return await dataProvider.provide(deserializeResponse, dataProviderArgs)
|
|
|
}
|
|
|
|
|
|
sort(){
|