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

88 lines 2.9 kB
import { Option } from "ts-results-es"; import { DefaultValyncClient } from "./util.js"; export { DefaultValyncClient }; export type RequestBody = BodyInit | Record<string, unknown>; export type ValyncOptions<T> = { init?: Omit<RequestInit, "signal">; files?: File[] | [string, File][]; cache?: boolean; fetchOnMount?: boolean; retryCount?: number; onData?: (data: any) => T; watch?: any[]; initialData?: ApiResponse<T>; fetchInterval?: number; onSuccess?: (data: T) => void; onError?: (err: ApiErrorResponse["error"]) => void; }; export type CacheKey = string | ({ url: string; } & Record<string, any>); export declare function normalizeKey(key: CacheKey): string; export type ApiError = { name: string; message: string; code?: number | string; }; export type ApiErrorResponse = { status: "failed"; error: ApiError; }; export type ApiSuccessResponse<T> = { status: "success"; data: T; }; export type ApiResponse<T> = ApiSuccessResponse<T> | ApiErrorResponse; export type Handler<T, R = undefined> = { loading?: () => R; error?: (err: ApiError) => R; data?: (value: Option<T>) => R; }; export type RequestMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; export type StateListener<T> = (val: AsyncValue<T>) => void; export interface Observer<T> { addListener: (fn: StateListener<T>) => void; removeListener: (fn: StateListener<T>) => void; } export declare class AsyncObserver<T> { private _current; constructor(_current: AsyncValue<T>); private listeners; listen(fn: StateListener<T>): void; removeListener(fn: StateListener<T>): boolean; observer(): Observer<T>; set(val: AsyncValue<T>): void; } export declare abstract class AsyncValue<T> { abstract when<R = undefined>(handlers: Handler<T, R>): R; abstract unwrap(): Option<T>; abstract unwrapErr(): ApiError; isLoading(): this is AsyncLoading; isData(): this is AsyncData<T>; isError(): this is AsyncError; } export declare class AsyncLoading<T = unknown> extends AsyncValue<T> { when<R>(h: Handler<T, R>): R; unwrap(): Option<T>; unwrapErr(): ApiError; } export declare class AsyncError<T = unknown> extends AsyncValue<T> { error: ApiError; constructor(error: ApiError); when<R>(h: Handler<T, R>): R; unwrap(): Option<T>; unwrapErr(): ApiError; } export declare class AsyncData<T> extends AsyncValue<T> { value: Option<T>; constructor(value: Option<T>); when<R>(h: Handler<T, R>): R; unwrap(): Option<T>; unwrapErr(): ApiError; } export declare function buildRequestBody(body?: RequestBody | null, files?: File[] | [string, File][]): { body: BodyInit | null | undefined; isMultipart: boolean; }; export declare function mergeHeaders(base?: HeadersInit, extra?: HeadersInit): Record<string, string>; //# sourceMappingURL=index.d.ts.map