react-ketting
Version:
Ketting bindings for React
65 lines (64 loc) • 2.06 kB
TypeScript
import { Resource, State as ResourceState } from 'ketting';
import { ResourceLike } from '../util';
declare type UseReadResourceResponse<T> = {
loading: boolean;
error: Error | null;
/**
* The ResourceState.
*
* Note that this will be `null` until loading is "false".
*/
resourceState: ResourceState<T>;
/**
* The 'real' resource.
*
* This will be `null` until we have it. It's not typed null because it
* makes it very clumsy to work with the hook.
*/
resource: Resource<T>;
/**
* Change the resource that the hook uses.
*
* A reason you might want to do this is if the resource itself changed
* uris.
*/
setResource(resource: Resource<T>): void;
};
export declare type UseReadResourceOptions<T> = {
initialState?: ResourceState<T>;
refreshOnStale?: boolean;
/**
* HTTP headers to include if there was no existing cache, and the initial
* GET request must be done to get the state.
*
* These headers are not used on subsequent refreshes/stale cases.
*/
initialGetRequestHeaders?: Record<string, string>;
};
/**
* The useReadResource hook is an internal hook that helps setting up a lot of
* the plumbing for dealing with resources and state.
*
* It's not recommended for external users to use this directly, instead use
* one of the more specialized hooks such as useResource or useCollection.
*
* Example call:
*
* <pre>
* const {
* loading,
* error,
* resourceState,
* } = useResource(resource);
* </pre>
*
* Returned properties:
*
* * loading - will be true as long as the result is still being fetched from
* the server.
* * error - Will be null or an error object.
* * resourceState - A state object. The `.data` property of this object will
* contain the parsed JSON from the server.
*/
export declare function useReadResource<T>(resourceLike: ResourceLike<T>, options: UseReadResourceOptions<T>): UseReadResourceResponse<T>;
export {};