mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
added useEntity and useEntityQuery hooks
This commit is contained in:
37
app/src/ui/client/api/use-data.ts
Normal file
37
app/src/ui/client/api/use-data.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import type { DataApi } from "data/api/DataApi";
|
||||
import { useApi } from "ui/client";
|
||||
|
||||
type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => any
|
||||
? (...args: P) => ReturnType<F>
|
||||
: never;
|
||||
|
||||
/**
|
||||
* Maps all DataApi functions and omits
|
||||
* the first argument "entity" for convenience
|
||||
* @param entity
|
||||
*/
|
||||
export const useData = <T extends keyof DataApi>(entity: string) => {
|
||||
const api = useApi().data;
|
||||
const methods = [
|
||||
"readOne",
|
||||
"readMany",
|
||||
"readManyByReference",
|
||||
"createOne",
|
||||
"updateOne",
|
||||
"deleteOne"
|
||||
] as const;
|
||||
|
||||
return methods.reduce(
|
||||
(acc, method) => {
|
||||
// @ts-ignore
|
||||
acc[method] = (...params) => {
|
||||
// @ts-ignore
|
||||
return api[method](entity, ...params);
|
||||
};
|
||||
return acc;
|
||||
},
|
||||
{} as {
|
||||
[K in (typeof methods)[number]]: OmitFirstArg<(typeof api)[K]>;
|
||||
}
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user