From 0907ded735e93082aebf8c7cad1bb93c1ad49332 Mon Sep 17 00:00:00 2001 From: qschroter Date: Fri, 10 Nov 2023 11:18:01 +0100 Subject: [PATCH] may fix some issue --- src/Bones.UI/core/serviceFactory.ts | 46 +++++++++---------- .../services/testUserService.ts | 17 +++---- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/Bones.UI/core/serviceFactory.ts b/src/Bones.UI/core/serviceFactory.ts index 2ffcd13..8344727 100644 --- a/src/Bones.UI/core/serviceFactory.ts +++ b/src/Bones.UI/core/serviceFactory.ts @@ -2,36 +2,34 @@ import axios, { AxiosInstance } from "axios"; import { buildURL } from "../tools"; import { NotifyService } from "./notifyService"; -import { AllCallback, INotifyService } from "../abstractions"; +import { AllCallback } from "../abstractions"; -export class ServiceFactory { +export class ServiceFactory { static http: AxiosInstance = axios; - private notifyService: NotifyService | null = null; + private notifyService: NotifyService; + EntityDetails: new (dto: TDetailsDTO) => TDetails; - private constructor(type?: string) { - if (type) this.notifyService = new NotifyService(type); + constructor(type: string, entity: new (dto: TDetailsDTO) => TDetails) { + this.notifyService = new NotifyService(type); + this.EntityDetails = entity; } - static create(type: string, factory: (f: ServiceFactory) => T): () => T { - const f = new ServiceFactory(type); - - return () => factory(f); + create(factory: (f: ServiceFactory) => T): () => T { + return () => factory(this); } - static createComplete( - entityName: string, + createComplete( manyURL: string | (() => string), oneURL: (id: string) => string, - entityDetails: new (dto: TDetailsDTO) => TDetails, entityInfos: new (dto: TInfosDTO) => TInfos, ) { - return ServiceFactory.create(entityName, factory => factory.build( + return this.create(factory => factory.build( factory.addNotify(), factory.addGetMany(manyURL, entityInfos), - factory.addGet(id => oneURL(id), entityDetails), - factory.addCreate(manyURL, entityDetails), - factory.addUpdate(id => oneURL(id), entityDetails), + factory.addGet(id => oneURL(id)), + factory.addCreate(manyURL), + factory.addUpdate(id => oneURL(id)), factory.addRemove(id => oneURL(id)) )); } @@ -53,14 +51,14 @@ export class ServiceFactory { } - addGet(url: (id: string) => string, entity: new (dto: TDetailsDTO) => TDetails) + addGet(url: (id: string) => string) : { get: (id: string) => Promise } { const get = async (id: string) => { const response = await ServiceFactory.http.get(url(id)); const dto: TDetailsDTO = response.data; - const result = new entity(dto); + const result = new this.EntityDetails(dto); return result; } @@ -68,13 +66,13 @@ export class ServiceFactory { return { get }; } - addCreate(url: string | (() => string), entity: new (dto: TDetailsDTO) => TDetails) + addCreate(url: string | (() => string)) : { create: (dto: TCreateDTO) => Promise } { const create = async (dto: TCreateDTO) => { const realUrl = typeof url === "string" ? url : url(); const response = await ServiceFactory.http.post(realUrl, dto); - const result = new entity(response.data); + const result = new this.EntityDetails(response.data); if (this.notifyService) this.notifyService.notify("add", result); @@ -85,12 +83,12 @@ export class ServiceFactory { return { create }; } - addUpdate(url: (id: string) => string, entity: new (dto: TDetailsDTO) => TDetails) + addUpdate(url: (id: string) => string) : { update: (id: string, dto: TUpdateDTO) => Promise } { const update = async (id: string, dto: TUpdateDTO) => { const response = await ServiceFactory.http.post(url(id), dto); - const result = new entity(response.data); + const result = new this.EntityDetails(response.data); if (this.notifyService) this.notifyService.notify("update", result); @@ -114,10 +112,10 @@ export class ServiceFactory { return { remove }; } - addNotify(others?: (notifyService: NotifyService) => U): { subscribe: (event: "add" | "update" | "delete" | "all", callback: AllCallback) => number, unsubscribe: (id: number) => void } & U { + addNotify(others?: (notifyService: NotifyService) => U): { subscribe: (event: "add" | "update" | "delete" | "all", callback: AllCallback) => number, unsubscribe: (id: number) => void } & U { if (!this.notifyService) throw new Error("Create your service with a type if you want to use notify"); - const notifyService = (this.notifyService as NotifyService) + const notifyService = this.notifyService const { subscribe, unsubscribe } = notifyService; diff --git a/tests/Bones.UI.Tests/services/testUserService.ts b/tests/Bones.UI.Tests/services/testUserService.ts index 94a156a..9845e26 100644 --- a/tests/Bones.UI.Tests/services/testUserService.ts +++ b/tests/Bones.UI.Tests/services/testUserService.ts @@ -1,14 +1,15 @@ import { ServiceFactory, ComposableFactory } from '@dative-gpi/bones-ui'; -import { CreateTestUserDTO, TestUserDetails, UpdateTestUserDTO } from '../models/testUserDetails'; -import { TestUserFilter, TestUserInfos } from '../models/testUserInfos'; +import { CreateTestUserDTO, TestUserDetails, TestUserDetailsDTO, UpdateTestUserDTO } from '../models/testUserDetails'; +import { TestUserFilter, TestUserInfos, TestUserInfosDTO } from '../models/testUserInfos'; export const TEST_USERS_URL = "/api/testUsers"; export const TEST_USER_URL = (id: string) => `/api/testUsers/${id}`; -const testUserServiceFactory = ServiceFactory.createComplete("testUser", TEST_USERS_URL, TEST_USER_URL, TestUserDetails, TestUserInfos); +const testUserServiceFactory = new ServiceFactory("test", TestUserDetails) + .createComplete(TEST_USERS_URL, TEST_USER_URL, TestUserInfos); -export const useTestUser = ComposableFactory.get(testUserServiceFactory); -export const useTestUsers = ComposableFactory.getMany(testUserServiceFactory); -export const useCreateTestUser = ComposableFactory.create(testUserServiceFactory); -export const useUpdateTestUser = ComposableFactory.update(testUserServiceFactory); -export const useRemoveTestUser = ComposableFactory.remove(testUserServiceFactory); \ No newline at end of file +export const useTestUser = ComposableFactory.get(testUserServiceFactory); +export const useTestUsers = ComposableFactory.getMany(testUserServiceFactory); +export const useCreateTestUser = ComposableFactory.create(testUserServiceFactory); +export const useUpdateTestUser = ComposableFactory.update(testUserServiceFactory); +export const useRemoveTestUser = ComposableFactory.remove(testUserServiceFactory);