| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import {AnyJson, DataProviderArgs} from "~/types/interfaces";
- import {DENORMALIZER_TYPE, HTTP_METHOD, QUERY_TYPE} from "~/types/enums";
- import {providers} from "~/services/dataProvider/provider/_import";
- import ConstructUrl from "~/services/connection/constructUrl";
- import Connection from "~/services/connection/connection";
- import Serializer from "~/services/serializer/serializer";
- import {Context} from "@nuxt/types/app";
- import ApiError from "~/services/utils/apiError";
- class DataProvider{
- private ctx !: Context;
- private arguments!: DataProviderArgs;
- constructor() {
- this.arguments = {
- type: QUERY_TYPE.MODEL,
- progress:false
- }
- }
- initCtx(ctx:Context){
- Connection.initConnector(ctx.$axios)
- this.ctx = ctx
- }
- setArguments(args: DataProviderArgs){
- this.arguments = { ...this.arguments, ...args }
- }
- async invoke(args:DataProviderArgs): Promise<any>{
- try{
- this.setArguments(args)
- this.startLoading()
- const url = this.constructUrl()
- const response = await this.connection(url)
- const deserializeResponse = await this.deserialization(response)
- return await this.provide(deserializeResponse)
- }catch(error){
- throw new ApiError(500, error)
- }
- }
- startLoading(){
- if(this.arguments.progress){
- const $nuxt = window['$nuxt']
- $nuxt.$loading.start()
- }
- }
- constructUrl(): string{
- const constructUrl = new ConstructUrl();
- return constructUrl.invoke(this.arguments)
- }
- connection(url: string): Promise<any>{
- const connection = new Connection()
- return connection.invoke(HTTP_METHOD.GET, url, this.arguments)
- }
- provide(data: AnyJson): any{
- for(const provider of providers){
- if(provider.support(this.arguments)){
- return new provider(this.ctx, this.arguments).invoke(data);
- }
- }
- }
- deserialization(data: AnyJson): AnyJson{
- const serializer = new Serializer()
- return serializer.denormalize(data, DENORMALIZER_TYPE.HYDRA)
- }
- }
- export default DataProvider
|