UNPKG

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.79 kB
import { Ref } from "vue"; import { ApiResponse, AsyncValue, AsyncLoading, AsyncError, AsyncData, ValyncOptions, Observer, CacheKey, RequestMethod } from "../core/index"; export type ValyncVueOptions<T> = Omit<ValyncOptions<T>, "init"> & { init?: Ref<RequestInit>; }; /** * 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?: ValyncVueOptions<T>) => [Ref<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?: ValyncVueOptions<T>): readonly [Ref<{ when: <R>(handlers: import("../core/index").Handler<T, R>) => R; isLoading: () => this is AsyncLoading; isData: () => this is AsyncData<T>; isError: () => this is AsyncError; }, AsyncValue<T> | { when: <R>(handlers: import("../core/index").Handler<T, R>) => R; isLoading: () => this is AsyncLoading; isData: () => this is AsyncData<T>; isError: () => this is AsyncError; }>, (methodOrOpts?: RequestMethod | { method?: RequestMethod; body?: BodyInit; }, body?: BodyInit) => void, (updater: (prev: T | null) => T) => void, { listen: (fn: import("../core/index").StateListener<T>) => () => boolean; }]; //# sourceMappingURL=index.d.ts.map