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.**

66 lines 3.01 kB
import { Ref } from "vue"; import { ApiResponse, AsyncValue, AsyncLoading, AsyncError, AsyncData, ValyncOptions, Observer, CacheKey, RequestMethod, RequestBody } from "../core/index"; export type ValyncVueOptions<T> = Omit<ValyncOptions<T>, "init"> & { init?: Ref<RequestInit>; }; export type CreateValyncVueOptions = 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` composable 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>. */ export declare function createValync({ client, options: _options, }: { client: (url: string, init: RequestInit) => Promise<ApiResponse<any>>; options?: CreateValyncVueOptions; }): <T>(key: CacheKey, options?: ValyncVueOptions<T>) => [Ref<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 composable 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 } * } */ export declare function useValync<T>(key: CacheKey, options?: ValyncVueOptions<T>): readonly [Ref<{ when: <R = undefined>(handlers: import("../core/index").Handler<T, R>) => R; unwrap: () => import("ts-results-es").Option<T>; unwrapErr: () => import("../core/index").ApiError; isLoading: () => this is AsyncLoading; isData: () => this is AsyncData<T>; isError: () => this is AsyncError; }, AsyncValue<T> | { when: <R = undefined>(handlers: import("../core/index").Handler<T, R>) => R; unwrap: () => import("ts-results-es").Option<T>; unwrapErr: () => import("../core/index").ApiError; isLoading: () => this is AsyncLoading; isData: () => this is AsyncData<T>; isError: () => this is AsyncError; }>, (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