import { FetchConfig } from './use-fetch.mjs'; /** * useModuleEntityV2 * * 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 GET to `listUrl` with `listParams`. The hook listens to * `searchQuery` (debounced) and `listDeps` to re-run the list fetch. * - Get by ID: performs a GET to `unitByIdUrl` with `byIdParams` and returns * `byIdFetchNow`, `byIdLoading`, and `byIdError`. * - Create/Update: performs a PUT 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` — params for GET by ID request * - `deleteCallback(data)`, `deleteDeps`, `deleteParams` — body for DELETE request * - `listCallback(data)`, `listDeps`, `listParams`, `listUrl` — params for GET list * - `searchQuery` (string) — debounced before influencing the list fetch * - `unitByIdUrl`, `unitUrl` * - `updateCallback(data)`, `updateDeps`, `updateParams` — body for PUT request * * Parameter types * - `listParams` and `byIdParams` support all primitive types including booleans. * They are appended as URL query parameters for GET requests. * Example: { active: true, page: 1 } → ?active=true&page=1 * - `updateParams` and `deleteParams` are sent as JSON in request body. * * 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 callbacks receive an object `{ data, error, status, ok }` from `useFetch`. * - Callbacks are wrapped so that any errors they throw are swallowed; this * ensures the hook's refresh logic still runs reliably. * - `refreshList()` calls `listFetchNow(undefined, { method: 'POST', params: listParams })` * to explicitly re-query the list endpoint with the current `listParams`. * - Network calls are delegated to `useFetch`; callers can call the returned * `*FetchNow` functions directly with an override URL or config. * * Example * const { listFetchNow, updateFetchNow } = useModuleEntityV2({ * listUrl: '/items', * listParams: { page: 1, active: true }, * listCallback: (res) => console.log(res), * unitUrl: '/items', * updateCallback: () => {}, * updateParams: {} * }); */ interface CallbackParamsV2 { data: any; error: any; status?: number; ok?: boolean; } interface Params { byIdCallback: (d: CallbackParamsV2) => void; byIdDeps?: Array | Array; byIdParams: object; deleteCallback?: (d: CallbackParamsV2) => void; deleteDeps?: Array | Array; deleteParams?: object; listCallback: (d: CallbackParamsV2) => void; listDeps?: Array | Array; listParams: object; listUrl: string; /** Optional headers to include on every fetch made by this hook */ headers?: Record; searchQuery: string; unitByIdUrl: string; unitUrl: string; updateCallback: (d: CallbackParamsV2) => 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 useModuleEntityV2: ({ byIdCallback, byIdDeps, byIdParams, deleteCallback, deleteDeps, deleteParams, listCallback, listDeps, listParams, listUrl, headers, searchQuery, unitByIdUrl, unitUrl, updateCallback, updateDeps, updateParams, }: Params) => Return; export { type CallbackParamsV2, useModuleEntityV2 };