import { __INTERNAL__, State, EndpointInterface, FetchFunction, } from '@rest-hooks/react'; import { useMemo } from 'react'; import { FetchShape, ParamsFromShape } from '../endpoint/index.js'; const { useCacheState } = __INTERNAL__; /** * Gets meta for a fetch key. * @see https://resthooks.io/docs/api/useMeta */ export default function useMeta< E extends | Pick, 'key'> | Pick, 'getFetchKey'>, Args extends | (E extends { key: any } ? readonly [...Parameters] : readonly [ParamsFromShape]) | readonly [null], >(endpoint: E, ...args: Args): StateMeta | null { const state = useCacheState(); const key = args[0] ? (endpoint as any).key ? (endpoint as any).key(...args) : (endpoint as any).getFetchKey(args[0]) : ''; return useMemo(() => { if (!key) return null; return selectMeta(state, key); }, [key, state]); } function selectMeta( state: State, fetchKey: string, ): State['meta'][string] { return state.meta[fetchKey]; } type StateMeta = State['meta'][string];