Skip to content

Commit

Permalink
may fix some issue
Browse files Browse the repository at this point in the history
  • Loading branch information
SchroterQuentin committed Nov 10, 2023
1 parent cc27b1f commit 0907ded
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
46 changes: 22 additions & 24 deletions src/Bones.UI/core/serviceFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TDetailsDTO, TDetails> {
static http: AxiosInstance = axios;

private notifyService: NotifyService<unknown> | null = null;
private notifyService: NotifyService<TDetails>;
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<TDetails>(type);
this.EntityDetails = entity;
}

static create<T>(type: string, factory: (f: ServiceFactory) => T): () => T {
const f = new ServiceFactory(type);

return () => factory(f);
create<T>(factory: (f: ServiceFactory<TDetailsDTO, TDetails>) => T): () => T {
return () => factory(this);
}

static createComplete<TInfos, TInfosDTO, TDetails, TDetailsDTO, TCreateDTO, TUpdateDTO, TFilterDTO>(
entityName: string,
createComplete<TInfos, TInfosDTO, TCreateDTO, TUpdateDTO, TFilterDTO>(
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<TDetails>(),
factory.addGetMany<TInfosDTO, TInfos, TFilterDTO>(manyURL, entityInfos),
factory.addGet<TDetailsDTO, TDetails>(id => oneURL(id), entityDetails),
factory.addCreate<TCreateDTO, TDetailsDTO, TDetails>(manyURL, entityDetails),
factory.addUpdate<TUpdateDTO, TDetailsDTO, TDetails>(id => oneURL(id), entityDetails),
factory.addGet(id => oneURL(id)),
factory.addCreate<TCreateDTO>(manyURL),
factory.addUpdate<TUpdateDTO>(id => oneURL(id)),
factory.addRemove(id => oneURL(id))
));
}
Expand All @@ -53,28 +51,28 @@ export class ServiceFactory {
}


addGet<TDetailsDTO, TDetails>(url: (id: string) => string, entity: new (dto: TDetailsDTO) => TDetails)
addGet(url: (id: string) => string)
: { get: (id: string) => Promise<TDetails> } {

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;
}

return { get };
}

addCreate<TCreateDTO, TDetailsDTO, TDetails>(url: string | (() => string), entity: new (dto: TDetailsDTO) => TDetails)
addCreate<TCreateDTO>(url: string | (() => string))
: { create: (dto: TCreateDTO) => Promise<TDetails> } {

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);
Expand All @@ -85,12 +83,12 @@ export class ServiceFactory {
return { create };
}

addUpdate<TUpdateDTO, TDetailsDTO, TDetails>(url: (id: string) => string, entity: new (dto: TDetailsDTO) => TDetails)
addUpdate<TUpdateDTO>(url: (id: string) => string)
: { update: (id: string, dto: TUpdateDTO) => Promise<TDetails> } {

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);
Expand All @@ -114,10 +112,10 @@ export class ServiceFactory {
return { remove };
}

addNotify<TEntity, U = {}>(others?: (notifyService: NotifyService<TEntity>) => U): { subscribe: (event: "add" | "update" | "delete" | "all", callback: AllCallback<TEntity>) => number, unsubscribe: (id: number) => void } & U {
addNotify<U = {}>(others?: (notifyService: NotifyService<TDetails>) => U): { subscribe: (event: "add" | "update" | "delete" | "all", callback: AllCallback<TDetails>) => 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<TEntity>)
const notifyService = this.notifyService

const { subscribe, unsubscribe } = notifyService;

Expand Down
17 changes: 9 additions & 8 deletions tests/Bones.UI.Tests/services/testUserService.ts
Original file line number Diff line number Diff line change
@@ -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<TestUserDetails, TestUserDetailsDTO>("test", TestUserDetails)
.createComplete<TestUserInfos, TestUserInfosDTO, CreateTestUserDTO, UpdateTestUserDTO, TestUserFilter>(TEST_USERS_URL, TEST_USER_URL, TestUserInfos);

export const useTestUser = ComposableFactory.get<TestUserDetails>(testUserServiceFactory);
export const useTestUsers = ComposableFactory.getMany<TestUserInfos, TestUserFilter>(testUserServiceFactory);
export const useCreateTestUser = ComposableFactory.create<CreateTestUserDTO, TestUserDetails>(testUserServiceFactory);
export const useUpdateTestUser = ComposableFactory.update<UpdateTestUserDTO, TestUserDetails>(testUserServiceFactory);
export const useRemoveTestUser = ComposableFactory.remove(testUserServiceFactory);
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);

0 comments on commit 0907ded

Please sign in to comment.