import { FetchConfig } from './use-fetch.js'; /** * useModuleEntity * * Summary * - A convenience hook that manages common module/entity operations: list, get-by-id, * create/update, and delete. It composes `useFetch` for HTTP calls and `useDebounce` * for debouncing search queries. * * Main features * - List: performs a POST to `listUrl` with a JSON body `listParams` and re-runs * when `searchQuery` (debounced) or `listDeps` change. * - Get by ID: performs a POST to `unitByIdUrl` with `byIdParams` in the JSON body * and exposes `byIdFetchNow`, `byIdLoading`, and `byIdError`. * - Create/Update: performs a POST to `unitUrl` with a JSON body `updateParams`. * On successful create/update the hook triggers a coalesced list refresh. * - Delete: performs a DELETE to `unitUrl` with a JSON body `deleteParams` and * triggers a coalesced list refresh when the operation succeeds. * - Refresh batching: `refreshList(delay)` coalesces multiple rapid updates/deletes * using a short timeout (default 300ms) to avoid unnecessary list reloads. * * Parameters (object) * - `byIdCallback(data)`, `byIdDeps`, `byIdParams` * - `deleteCallback(data)`, `deleteDeps`, `deleteParams` * - `listCallback(data)`, `listDeps`, `listParams`, `listUrl` * - `searchQuery` (string) — debounced before influencing the list fetch * - `unitByIdUrl`, `unitUrl` * - `updateCallback(data)`, `updateDeps`, `updateParams` * * Return values * - `byIdFetchNow(url?, config?)`, `byIdLoading`, `byIdError` * - `listFetchNow(url?, config?)`, `listLoading`, `listError` * - `updateFetchNow(url?, config?)`, `updateLoading`, `updateError` * - `deleteFetchNow(url?, config?)`, `deleteLoading`, `deleteError` * * Behavior notes * - All requests that accept parameters send them in the JSON request body (stringified). * - All callbacks receive an object `{ data, error, status, ok }` from `useFetch`. * - Callbacks are wrapped so thrown errors are swallowed to ensure refresh logic * still runs reliably. * - `refreshList()` calls `listFetchNow(undefined, { method: 'POST', body: JSON.stringify(listParams) })` * to explicitly re-query the list endpoint with the current `listParams`. * - Network calls are delegated to `useFetch`; callers can invoke the returned * `*FetchNow` functions directly with an override URL or config. * * Example * const { listFetchNow, updateFetchNow } = useModuleEntity({ * listUrl: '/items', * listParams: { page: 1 }, * listCallback: (res) => console.log(res), * unitUrl: '/items', * updateCallback: () => {}, * updateParams: {} * }); */ interface CallbackParams { data: any; error: any; status?: number; ok?: boolean; } interface Params { byIdCallback: (d: CallbackParams) => void; byIdDeps?: Array | Array; byIdParams: object; deleteCallback?: (d: CallbackParams) => void; deleteDeps?: Array | Array; deleteParams?: object; listCallback: (d: CallbackParams) => void; listDeps?: Array | Array; listParams: object; listUrl: string; searchQuery: string; unitByIdUrl: string; unitUrl: string; updateCallback: (d: CallbackParams) => void; updateDeps?: Array | Array; updateParams: object; } interface Return { byIdError?: Error; byIdFetchNow: (url?: string, config?: FetchConfig) => void; byIdLoading: boolean; deleteError?: Error; deleteFetchNow?: (url?: string, config?: FetchConfig) => void; deleteLoading: boolean; listError?: Error; listFetchNow: (url?: string, config?: FetchConfig) => void; listLoading: boolean; updateError?: Error; updateFetchNow: (url?: string, config?: FetchConfig) => void; updateLoading: boolean; } declare const useModuleEntity: ({ byIdCallback, byIdDeps, byIdParams, deleteCallback, deleteDeps, deleteParams, listCallback, listDeps, listParams, listUrl, searchQuery, unitByIdUrl, unitUrl, updateCallback, updateDeps, updateParams, }: Params) => Return; export { type CallbackParams, useModuleEntity };