fix(frontend): enhance hook types

This commit is contained in:
yassinedorbozgithub 2025-06-21 10:37:09 +01:00
parent ced3ecbd7a
commit 54c11beda8
13 changed files with 94 additions and 110 deletions

View File

@ -15,7 +15,6 @@ import { EntityType } from "@/services/types";
import { ComponentFormProps } from "@/types/common/dialogs.types"; import { ComponentFormProps } from "@/types/common/dialogs.types";
import { import {
INlpDatasetSample, INlpDatasetSample,
INlpDatasetSampleAttributes,
INlpSampleFormAttributes, INlpSampleFormAttributes,
} from "@/types/nlp-sample.types"; } from "@/types/nlp-sample.types";
@ -29,10 +28,7 @@ export const NlpSampleForm: FC<ComponentFormProps<INlpDatasetSample>> = ({
}) => { }) => {
const { t } = useTranslate(); const { t } = useTranslate();
const { toast } = useToast(); const { toast } = useToast();
const { mutate: updateSample } = useUpdate< const { mutate: updateSample } = useUpdate(EntityType.NLP_SAMPLE, {
EntityType.NLP_SAMPLE,
INlpDatasetSampleAttributes
>(EntityType.NLP_SAMPLE, {
onError: () => { onError: () => {
toast.error(t("message.internal_server_error")); toast.error(t("message.internal_server_error"));
}, },

View File

@ -9,19 +9,19 @@
import { useMutation, useQueryClient } from "react-query"; import { useMutation, useQueryClient } from "react-query";
import { QueryType, TMutationOptions } from "@/services/types"; import { QueryType, TMutationOptions } from "@/services/types";
import { IBaseSchema, IDynamicProps, TType } from "@/types/base.types"; import { IBaseSchema, THook } from "@/types/base.types";
import { useEntityApiClient } from "../useApiClient"; import { useEntityApiClient } from "../useApiClient";
import { isSameEntity, useNormalizeAndCache } from "./helpers"; import { isSameEntity, useNormalizeAndCache } from "./helpers";
export const useCreate = < export const useCreate = <
TEntity extends IDynamicProps["entity"], TE extends THook["entity"],
TAttr = TType<TEntity>["attributes"], TAttr = THook<{ entity: TE }>["attributes"],
TBasic extends IBaseSchema = TType<TEntity>["basic"], TBasic extends IBaseSchema = THook<{ entity: TE }>["basic"],
TFull extends IBaseSchema = TType<TEntity>["full"], TFull extends IBaseSchema = THook<{ entity: TE }>["full"],
>( >(
entity: TEntity, entity: TE,
options?: Omit< options?: Omit<
TMutationOptions<TBasic, Error, TAttr, TBasic>, TMutationOptions<TBasic, Error, TAttr, TBasic>,
"mutationFn" | "mutationKey" "mutationFn" | "mutationKey"

View File

@ -9,24 +9,19 @@
import { useMutation, useQueryClient } from "react-query"; import { useMutation, useQueryClient } from "react-query";
import { QueryType, TMutationOptions } from "@/services/types"; import { QueryType, TMutationOptions } from "@/services/types";
import { import { IBaseSchema, IEntityMapTypes, THook } from "@/types/base.types";
IBaseSchema,
IDynamicProps,
IEntityMapTypes,
TType,
} from "@/types/base.types";
import { useEntityApiClient } from "../useApiClient"; import { useEntityApiClient } from "../useApiClient";
import { isSameEntity } from "./helpers"; import { isSameEntity } from "./helpers";
export const useDelete = < export const useDelete = <
TEntity extends IDynamicProps["entity"], TE extends THook["entity"],
TAttr = TType<TEntity>["attributes"], TAttr = THook<{ entity: TE }>["attributes"],
TBasic extends IBaseSchema = TType<TEntity>["basic"], TBasic extends IBaseSchema = THook<{ entity: TE }>["basic"],
TFull extends IBaseSchema = TType<TEntity>["full"], TFull extends IBaseSchema = THook<{ entity: TE }>["full"],
>( >(
entity: TEntity, entity: TE,
options?: Omit< options?: Omit<
TMutationOptions<string, Error, string, TBasic>, TMutationOptions<string, Error, string, TBasic>,
"mutationFn" | "mutationKey" "mutationFn" | "mutationKey"

View File

@ -9,19 +9,19 @@
import { useMutation, useQueryClient } from "react-query"; import { useMutation, useQueryClient } from "react-query";
import { QueryType, TMutationOptions } from "@/services/types"; import { QueryType, TMutationOptions } from "@/services/types";
import { IBaseSchema, IDynamicProps, TType } from "@/types/base.types"; import { IBaseSchema, THook } from "@/types/base.types";
import { useEntityApiClient } from "../useApiClient"; import { useEntityApiClient } from "../useApiClient";
import { isSameEntity } from "./helpers"; import { isSameEntity } from "./helpers";
export const useDeleteMany = < export const useDeleteMany = <
TEntity extends IDynamicProps["entity"], TE extends THook["entity"],
TAttr = TType<TEntity>["attributes"], TAttr = THook<{ entity: TE }>["attributes"],
TBasic extends IBaseSchema = TType<TEntity>["basic"], TBasic extends IBaseSchema = THook<{ entity: TE }>["basic"],
TFull extends IBaseSchema = TType<TEntity>["full"], TFull extends IBaseSchema = THook<{ entity: TE }>["full"],
>( >(
entity: TEntity, entity: TE,
options?: Omit< options?: Omit<
TMutationOptions<string, Error, string[], TBasic>, TMutationOptions<string, Error, string[], TBasic>,
"mutationFn" | "mutationKey" "mutationFn" | "mutationKey"

View File

@ -8,19 +8,12 @@
import { useQuery, UseQueryOptions } from "react-query"; import { useQuery, UseQueryOptions } from "react-query";
import { import { EntityType, Format, QueryType } from "@/services/types";
EntityType,
Format,
QueryType,
TPopulateTypeFromFormat,
} from "@/services/types";
import { import {
IBaseSchema, IBaseSchema,
IDynamicProps,
IFindConfigProps, IFindConfigProps,
POPULATE_BY_TYPE, POPULATE_BY_TYPE,
TAllowedFormat, THook,
TType,
} from "@/types/base.types"; } from "@/types/base.types";
import { useEntityApiClient } from "../useApiClient"; import { useEntityApiClient } from "../useApiClient";
@ -31,13 +24,13 @@ import { useCount } from "./useCount";
import { useGetFromCache } from "./useGet"; import { useGetFromCache } from "./useGet";
export const useFind = < export const useFind = <
TDynamicProps extends IDynamicProps, TP extends THook["params"],
TAttr = TType<TDynamicProps["entity"]>["attributes"], TAttr = THook<TP>["attributes"],
TBasic extends IBaseSchema = TType<TDynamicProps["entity"]>["basic"], TBasic extends IBaseSchema = THook<TP>["basic"],
TFull extends IBaseSchema = TType<TDynamicProps["entity"]>["full"], TFull extends IBaseSchema = THook<TP>["full"],
P = TPopulateTypeFromFormat<TDynamicProps>, P = THook<TP>["populate"],
>( >(
{ entity, format }: TDynamicProps & TAllowedFormat<TDynamicProps["entity"]>, { entity, format }: THook<TP>["params"],
config?: IFindConfigProps, config?: IFindConfigProps,
options?: Omit< options?: Omit<
UseQueryOptions<string[], Error, string[], [QueryType, EntityType, string]>, UseQueryOptions<string[], Error, string[], [QueryType, EntityType, string]>,

View File

@ -11,10 +11,9 @@ import { useQuery, useQueryClient, UseQueryOptions } from "react-query";
import { EntityType, Format, QueryType } from "@/services/types"; import { EntityType, Format, QueryType } from "@/services/types";
import { import {
IBaseSchema, IBaseSchema,
IDynamicProps,
IEntityMapTypes, IEntityMapTypes,
POPULATE_BY_TYPE, POPULATE_BY_TYPE,
TAllowedFormat, THook,
TType, TType,
} from "@/types/base.types"; } from "@/types/base.types";
@ -23,13 +22,13 @@ import { useEntityApiClient } from "../useApiClient";
import { useNormalizeAndCache } from "./helpers"; import { useNormalizeAndCache } from "./helpers";
export const useGet = < export const useGet = <
TDynamicProps extends IDynamicProps, T extends THook["params"],
TAttr = TType<TDynamicProps["entity"]>["attributes"], TAttr = THook<T>["attributes"],
TBasic extends IBaseSchema = TType<TDynamicProps["entity"]>["basic"], TBasic extends IBaseSchema = THook<T>["basic"],
TFull extends IBaseSchema = TType<TDynamicProps["entity"]>["full"], TFull extends IBaseSchema = THook<T>["full"],
>( >(
id: string, id: string,
{ entity, format }: TDynamicProps & TAllowedFormat<TDynamicProps["entity"]>, { entity, format }: THook<T>["params"],
options?: Omit< options?: Omit<
UseQueryOptions< UseQueryOptions<
unknown, unknown,

View File

@ -9,18 +9,18 @@
import { useMutation, useQueryClient } from "react-query"; import { useMutation, useQueryClient } from "react-query";
import { QueryType, TMutationOptions } from "@/services/types"; import { QueryType, TMutationOptions } from "@/services/types";
import { IBaseSchema, IDynamicProps, TType } from "@/types/base.types"; import { IBaseSchema, THook } from "@/types/base.types";
import { useEntityApiClient } from "../useApiClient"; import { useEntityApiClient } from "../useApiClient";
import { isSameEntity, useNormalizeAndCache } from "./helpers"; import { isSameEntity, useNormalizeAndCache } from "./helpers";
export const useImport = < export const useImport = <
TEntity extends IDynamicProps["entity"], TE extends THook["entity"],
TAttr extends File = File, TAttr extends File = File,
TBasic extends IBaseSchema = TType<TEntity>["basic"], TBasic extends IBaseSchema = THook<{ entity: TE }>["basic"],
>( >(
entity: TEntity, entity: TE,
options: Omit< options: Omit<
TMutationOptions<TBasic[], Error, TAttr, TBasic[]>, TMutationOptions<TBasic[], Error, TAttr, TBasic[]>,
"mutationFn" | "mutationKey" "mutationFn" | "mutationKey"

View File

@ -8,19 +8,12 @@
import { useInfiniteQuery, UseInfiniteQueryOptions } from "react-query"; import { useInfiniteQuery, UseInfiniteQueryOptions } from "react-query";
import { import { EntityType, Format, QueryType } from "@/services/types";
EntityType,
Format,
QueryType,
TPopulateTypeFromFormat,
} from "@/services/types";
import { import {
IBaseSchema, IBaseSchema,
IDynamicProps,
IFindConfigProps, IFindConfigProps,
POPULATE_BY_TYPE, POPULATE_BY_TYPE,
TAllowedFormat, THook,
TType,
} from "@/types/base.types"; } from "@/types/base.types";
import { useEntityApiClient } from "../useApiClient"; import { useEntityApiClient } from "../useApiClient";
@ -29,13 +22,13 @@ import { useNormalizeAndCache } from "./helpers";
import { useGetFromCache } from "./useGet"; import { useGetFromCache } from "./useGet";
export const useInfiniteFind = < export const useInfiniteFind = <
TDynamicProps extends IDynamicProps, T extends THook["params"],
TAttr = TType<TDynamicProps["entity"]>["attributes"], TAttr = THook<T>["attributes"],
TBasic extends IBaseSchema = TType<TDynamicProps["entity"]>["basic"], TBasic extends IBaseSchema = THook<T>["basic"],
TFull extends IBaseSchema = TType<TDynamicProps["entity"]>["full"], TFull extends IBaseSchema = THook<T>["full"],
P = TPopulateTypeFromFormat<TDynamicProps>, P = THook<T>["populate"],
>( >(
{ entity, format }: TDynamicProps & TAllowedFormat<TDynamicProps["entity"]>, { entity, format }: THook<T>["params"],
config?: IFindConfigProps, config?: IFindConfigProps,
options?: Omit< options?: Omit<
UseInfiniteQueryOptions< UseInfiniteQueryOptions<

View File

@ -8,19 +8,12 @@
import { useInfiniteQuery, UseInfiniteQueryOptions } from "react-query"; import { useInfiniteQuery, UseInfiniteQueryOptions } from "react-query";
import { import { EntityType, Format, QueryType } from "@/services/types";
EntityType,
Format,
QueryType,
TPopulateTypeFromFormat,
} from "@/services/types";
import { import {
IBaseSchema, IBaseSchema,
IDynamicProps,
IFindConfigProps, IFindConfigProps,
POPULATE_BY_TYPE, POPULATE_BY_TYPE,
TAllowedFormat, THook,
TType,
} from "@/types/base.types"; } from "@/types/base.types";
import { useEntityApiClient } from "../useApiClient"; import { useEntityApiClient } from "../useApiClient";
@ -32,13 +25,13 @@ import { useGetFromCache } from "./useGet";
const PAGE_SIZE = 20; const PAGE_SIZE = 20;
export const useNormalizedInfiniteQuery = < export const useNormalizedInfiniteQuery = <
TDynamicProps extends IDynamicProps, T extends THook["params"],
TAttr = TType<TDynamicProps["entity"]>["attributes"], TAttr = THook<T>["attributes"],
TBasic extends IBaseSchema = TType<TDynamicProps["entity"]>["basic"], TBasic extends IBaseSchema = THook<T>["basic"],
TFull extends IBaseSchema = TType<TDynamicProps["entity"]>["full"], TFull extends IBaseSchema = THook<T>["full"],
P = TPopulateTypeFromFormat<TDynamicProps>, P = THook<T>["populate"],
>( >(
{ entity, format }: TDynamicProps & TAllowedFormat<TDynamicProps["entity"]>, { entity, format }: THook<T>["params"],
config?: IFindConfigProps, config?: IFindConfigProps,
options?: Omit< options?: Omit<
UseInfiniteQueryOptions< UseInfiniteQueryOptions<

View File

@ -9,12 +9,7 @@
import { useMutation, useQueryClient } from "react-query"; import { useMutation, useQueryClient } from "react-query";
import { QueryType, TMutationOptions, TSetCacheProps } from "@/services/types"; import { QueryType, TMutationOptions, TSetCacheProps } from "@/services/types";
import { import { IBaseSchema, IEntityMapTypes, THook, TType } from "@/types/base.types";
IBaseSchema,
IDynamicProps,
IEntityMapTypes,
TType,
} from "@/types/base.types";
import { merge } from "@/utils/object"; import { merge } from "@/utils/object";
import { useEntityApiClient } from "../useApiClient"; import { useEntityApiClient } from "../useApiClient";
@ -23,12 +18,12 @@ import { useNormalizeAndCache } from "./helpers";
import { useGetFromCache } from "./useGet"; import { useGetFromCache } from "./useGet";
export const useUpdate = < export const useUpdate = <
TEntity extends IDynamicProps["entity"], TE extends THook["entity"],
TAttr = TType<TEntity>["attributes"], TAttr = THook<{ entity: TE }>["attributes"],
TBasic extends IBaseSchema = TType<TEntity>["basic"], TBasic extends IBaseSchema = THook<{ entity: TE }>["basic"],
TFull extends IBaseSchema = TType<TEntity>["full"], TFull extends IBaseSchema = THook<{ entity: TE }>["full"],
>( >(
entity: TEntity, entity: TE,
options?: Omit< options?: Omit<
TMutationOptions< TMutationOptions<
TBasic, TBasic,

View File

@ -9,19 +9,19 @@
import { useMutation, useQueryClient } from "react-query"; import { useMutation, useQueryClient } from "react-query";
import { QueryType, TMutationOptions } from "@/services/types"; import { QueryType, TMutationOptions } from "@/services/types";
import { IBaseSchema, IDynamicProps, TType } from "@/types/base.types"; import { IBaseSchema, THook } from "@/types/base.types";
import { useEntityApiClient } from "../useApiClient"; import { useEntityApiClient } from "../useApiClient";
import { isSameEntity } from "./helpers"; import { isSameEntity } from "./helpers";
export const useUpdateMany = < export const useUpdateMany = <
TEntity extends IDynamicProps["entity"], TE extends THook["entity"],
TAttr = TType<TEntity>["attributes"], TAttr = THook<{ entity: TE }>["attributes"],
TBasic extends IBaseSchema = TType<TEntity>["basic"], TBasic extends IBaseSchema = THook<{ entity: TE }>["basic"],
TFull extends IBaseSchema = TType<TEntity>["full"], TFull extends IBaseSchema = THook<{ entity: TE }>["full"],
>( >(
entity: TEntity, entity: TE,
options?: Omit< options?: Omit<
TMutationOptions< TMutationOptions<
string, string,

View File

@ -10,19 +10,19 @@ import { useMutation, useQueryClient } from "react-query";
import { QueryType, TMutationOptions } from "@/services/types"; import { QueryType, TMutationOptions } from "@/services/types";
import { AttachmentResourceRef } from "@/types/attachment.types"; import { AttachmentResourceRef } from "@/types/attachment.types";
import { IBaseSchema, IDynamicProps, TType } from "@/types/base.types"; import { IBaseSchema, THook } from "@/types/base.types";
import { useEntityApiClient } from "../useApiClient"; import { useEntityApiClient } from "../useApiClient";
import { isSameEntity, useNormalizeAndCache } from "./helpers"; import { isSameEntity, useNormalizeAndCache } from "./helpers";
export const useUpload = < export const useUpload = <
TEntity extends IDynamicProps["entity"], TE extends THook["entity"],
TAttr = TType<TEntity>["attributes"], TAttr = THook<{ entity: TE }>["attributes"],
TBasic extends IBaseSchema = TType<TEntity>["basic"], TBasic extends IBaseSchema = THook<{ entity: TE }>["basic"],
TFull extends IBaseSchema = TType<TEntity>["full"], TFull extends IBaseSchema = THook<{ entity: TE }>["full"],
>( >(
entity: TEntity, entity: TE,
options?: Omit< options?: Omit<
TMutationOptions< TMutationOptions<
TBasic, TBasic,

View File

@ -8,7 +8,7 @@
import { GridPaginationModel, GridSortModel } from "@mui/x-data-grid"; import { GridPaginationModel, GridSortModel } from "@mui/x-data-grid";
import { EntityType, Format } from "@/services/types"; import { EntityType, Format, TPopulateTypeFromFormat } from "@/services/types";
import { IAttachment, IAttachmentAttributes } from "./attachment.types"; import { IAttachment, IAttachmentAttributes } from "./attachment.types";
import { import {
@ -39,8 +39,8 @@ import {
INlpEntityFull, INlpEntityFull,
} from "./nlp-entity.types"; } from "./nlp-entity.types";
import { import {
INlpDatasetSampleAttributes,
INlpSample, INlpSample,
INlpSampleAttributes,
INlpSampleFull, INlpSampleFull,
} from "./nlp-sample.types"; } from "./nlp-sample.types";
import { import {
@ -170,7 +170,7 @@ export interface IEntityMapTypes {
INlpEntityFull INlpEntityFull
>; >;
[EntityType.NLP_SAMPLE]: IEntityTypes< [EntityType.NLP_SAMPLE]: IEntityTypes<
INlpSampleAttributes, INlpDatasetSampleAttributes,
INlpSample, INlpSample,
INlpSampleFull INlpSampleFull
>; >;
@ -227,6 +227,26 @@ export interface IDynamicProps {
format?: Format; format?: Format;
} }
type AllNever<T> = {
[K in keyof T]: never;
};
export type THook<
G extends IDynamicProps = IDynamicProps,
TE extends keyof IEntityMapTypes = G["entity"],
TP extends IDynamicProps = IDynamicProps &
G &
AllNever<Omit<G, keyof IDynamicProps>> &
TAllowedFormat<TE>,
> = {
full: TType<TE>["full"];
basic: TType<TE>["basic"];
params: TP;
entity: TE;
populate: TPopulateTypeFromFormat<G>;
attributes: TType<TE>["attributes"];
};
export interface IFindConfigProps { export interface IFindConfigProps {
params?: any; params?: any;
hasCount?: boolean; hasCount?: boolean;