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.**
56 lines • 2.17 kB
TypeScript
import { ApiResponse, AsyncValue, ValyncOptions, Observer, CacheKey, RequestMethod } from "../core/index";
/**
* createValyn 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.
*
* ⚠️ 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 endpoint when neccessary.
* Returning a plain array or object without the `status` field will cause issues.
*/
export declare function createValyn({ client, options: _options, }: {
client: (url: string, init: RequestInit) => Promise<ApiResponse<any>>;
options?: Pick<ValyncOptions<any>, "cache" | "retryCount" | "fetchOnMount"> & {
headers?: HeadersInit;
};
}): <T>(key: CacheKey, options?: ValyncOptions<T>) => [AsyncValue<T>, (methodOrOpts?: RequestMethod | {
method?: RequestMethod;
body?: BodyInit;
}, body?: BodyInit) => 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,
* so 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?: BodyInit;
}, body?: BodyInit) => void,
(updater: (prev: T | null) => T) => void,
Observer<T>
];
//# sourceMappingURL=index.d.ts.map