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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -8,7 +8,7 @@
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 {
@ -39,8 +39,8 @@ import {
INlpEntityFull,
} from "./nlp-entity.types";
import {
INlpDatasetSampleAttributes,
INlpSample,
INlpSampleAttributes,
INlpSampleFull,
} from "./nlp-sample.types";
import {
@ -170,7 +170,7 @@ export interface IEntityMapTypes {
INlpEntityFull
>;
[EntityType.NLP_SAMPLE]: IEntityTypes<
INlpSampleAttributes,
INlpDatasetSampleAttributes,
INlpSample,
INlpSampleFull
>;
@ -227,6 +227,26 @@ export interface IDynamicProps {
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 {
params?: any;
hasCount?: boolean;