|
|
@@ -1,60 +1,60 @@
|
|
|
-import {Store} from "vuex";
|
|
|
-import {Rest} from "~/services/queries/rest";
|
|
|
-import {Model} from "@/models/Model"
|
|
|
+import {AnyJson, DataProviderArgs} from "~/types/interfaces";
|
|
|
+import {DENORMALIZER_TYPE, HTTP_METHOD} 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";
|
|
|
|
|
|
class DataProvider{
|
|
|
- private store: Store<any>
|
|
|
- private $rest: Rest
|
|
|
+ private ctx !: Context;
|
|
|
+ private arguments!: DataProviderArgs;
|
|
|
|
|
|
- constructor(store:Store<any>, $rest: Rest) {
|
|
|
- this.store = store
|
|
|
- this.$rest = $rest
|
|
|
+ constructor() {
|
|
|
}
|
|
|
|
|
|
- public async getItem(model: typeof Model, id: number){
|
|
|
- const repository = this.store.$repo(model);
|
|
|
- const entity = repository.getModel().$entity();
|
|
|
-
|
|
|
- const response = await this.$rest.getItem(`/api/${entity}`, id)
|
|
|
- if(response)
|
|
|
- repository.insert(response);
|
|
|
+ initCtx(ctx:Context){
|
|
|
+ Connection.initConnector(ctx.$axios)
|
|
|
+ this.ctx = ctx
|
|
|
}
|
|
|
|
|
|
- public async getCollection(model: typeof Model){
|
|
|
- const repository = this.store.$repo(model);
|
|
|
- const entity = repository.getModel().$entity();
|
|
|
-
|
|
|
- const response = await this.$rest.getCollection(`/api/${entity}`)
|
|
|
- if(response)
|
|
|
- repository.insert(response);
|
|
|
- }
|
|
|
+ async invoke(args:DataProviderArgs): Promise<any>{
|
|
|
+ this.arguments = args
|
|
|
+ try{
|
|
|
+ const url = this.constructUrl()
|
|
|
|
|
|
- public async getSubResourceCollection(root_model: typeof Model, root_id: number, model: typeof Model){
|
|
|
- const root_repository = this.store.$repo(root_model);
|
|
|
- const root_entity = root_repository.getModel().$entity();
|
|
|
+ const response = await this.connection(url)
|
|
|
|
|
|
- const repository = this.store.$repo(model);
|
|
|
- const entity = repository.getModel().$entity();
|
|
|
+ const deserializeResponse = await this.deserialization(response)
|
|
|
|
|
|
- const response = await this.$rest.getCollection(`/api/${root_entity}/${root_id}/${entity}`)
|
|
|
- if(response){
|
|
|
- repository.insert(response)
|
|
|
- return repository.all();
|
|
|
+ return await this.provide(deserializeResponse)
|
|
|
+ }catch(error){
|
|
|
+ throw new Error(error.message);
|
|
|
}
|
|
|
- return [];
|
|
|
}
|
|
|
|
|
|
- public async getSubResourceItem(root_model: typeof Model, root_id: number, model: typeof Model, id: number) {
|
|
|
- const root_repository = this.store.$repo(root_model);
|
|
|
- const root_entity = root_repository.getModel().$entity();
|
|
|
+ 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)
|
|
|
+ }
|
|
|
|
|
|
- const repository = this.store.$repo(model);
|
|
|
- const entity = repository.getModel().$entity();
|
|
|
+ provide(data: AnyJson): any{
|
|
|
+ for(const provider of providers){
|
|
|
+ if(provider.support(this.arguments)){
|
|
|
+ return new provider(this.ctx, this.arguments).invoke(data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- const response = await this.$rest.getItem(`/api/${root_entity}/${root_id}/${entity}`, id)
|
|
|
- if(response)
|
|
|
- repository.insert(response);
|
|
|
+ deserialization(data: AnyJson): AnyJson{
|
|
|
+ const serializer = new Serializer()
|
|
|
+ return serializer.denormalize(data, DENORMALIZER_TYPE.DEFAULT)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export const $dataProvider = (store:Store<any>, $rest:Rest) => new DataProvider(store, $rest);
|
|
|
+export default DataProvider
|