valync
Version:
**A lightweight, framework-agnostic async data handling library for React & Vue, inspired by Riverpod’s AsyncValue pattern and powered by ts-results-es.**
67 lines • 2.66 kB
TypeScript
import { ApiResponse, AsyncValue, ValyncOptions, Observer, CacheKey, RequestMethod, RequestBody } from "../core/index";
export type CreateValyncOptions = Pick<ValyncOptions<any>, "cache" | "retryCount" | "fetchOnMount"> & {
headers?: HeadersInit | (() => HeadersInit);
onError?: (err: {
name: string;
message: string;
code?: number | string;
}) => boolean | Promise<boolean>;
};
/**
* createValync creates a custom `useValync` hook bound to a provided HTTP client function.
* Useful for plugging in your own fetch logic or a library like axios.
*
* The `options.onError` callback receives every API-level error. Returning `true` (or a
* Promise resolving to `true`) triggers a single retry — useful for token-refresh flows.
*
* ⚠️ NOTE:
* Your `client()` function MUST return a Promise resolving to:
*
* ApiResponse<any>
*
* {
* status: "success" | "failed",
* data?: T,
* error?: { name: string; message: string; code?: number }
* }
*
* use `onData` to apply transformation from `any => T` for individual endpoints when necessary.
*/
export declare function createValync({ client, options: _options, }: {
client: (url: string, init: RequestInit) => Promise<ApiResponse<any>>;
options?: CreateValyncOptions;
}): <T>(key: CacheKey, options?: ValyncOptions<T>) => [AsyncValue<T>, (methodOrOpts?: RequestMethod | {
method?: RequestMethod;
body?: RequestBody;
files?: File[] | [string, File][];
}, body?: RequestBody, files?: File[] | [string, File][]) => void, (updater: (prev: T | null) => T) => void, Observer<T>];
/**
* useValync is a client-side data fetching hook that provides async state management
* with caching, optimistic updates, and reactive watching support.
*
* ⚠️ NOTE:
* Your server MUST return a JSON response of the shape:
*
* ApiResponse<T> | ApiResponse<any>
*
* {
* status: "success" | "failed",
* data?: T,
* error?: { name: string; message: string; code?: number }
* }
*
* Use `onData` if `res.data` does not match your expected frontend type or if you wish to
* apply transformation. Returning a plain array or object without the `status` field will
* cause issues.
*/
export declare function useValync<T>(key: CacheKey, options?: ValyncOptions<T>): [
AsyncValue<T>,
(methodOrOpts?: RequestMethod | {
method?: RequestMethod;
body?: RequestBody;
files?: File[] | [string, File][];
}, body?: RequestBody, files?: File[] | [string, File][]) => void,
(updater: (prev: T | null) => T) => void,
Observer<T>
];
//# sourceMappingURL=index.d.ts.map