get-set-action
Version:
A React State Management Library similar and alternative to mobx, redux.etc. it's the simplest, smallest and fastest state management library for react with dev tools support.
84 lines (83 loc) • 3.51 kB
TypeScript
type StatusType = "intial" | "loading" | "success" | "error" | "cancelled";
export type ActionType<T, E, P extends Record<string, any> = {}> = {
run: (payload: P, signal?: AbortSignal) => Promise<T>;
abort: () => void;
onRun: (subscriber: any) => (subscriber: any) => void;
onSuccess: (subscriber: any) => (subscriber: any) => void;
subscribe: (subscriber: any) => (subscriber: any) => void;
onError: (subscriber: any) => (subscriber: any) => void;
getState: () => ActionReturnType<T, E, P>;
intercept: (interceptor: (props: P) => P) => void;
interceptAfter: (interceptor: (data: any) => T) => void;
interceptError: (interceptor: (error: any) => E) => void;
};
export declare function asyncAction<T, E, P extends Record<string, any>>(fn: (payload: P, signal?: AbortSignal) => any, options?: {
debounce?: number;
throttle?: number;
}): ActionType<T, E, P> & ActionReturnType<T, E, P>;
type ActionReturnType<T, E, P extends Record<string, any>> = {
data: T | null;
error: E | null;
isRunning: boolean;
isSuccess: boolean;
isError: boolean;
isDebounced: boolean;
isThrottled: boolean;
isDebouncing: boolean;
status: StatusType;
abortController: AbortController;
currentPage: number;
countPerPage: number;
extraState: any;
filters: any;
payload: P;
setData: (data: T | null) => void;
setError: (error: E | null) => void;
setPage: (page: number) => void;
setCountPerPage: (count: number) => void;
setExtraState: (state: any) => void;
setFilters: (filters: any) => void;
setStatus: (status: StatusType) => void;
run: (payload: P, signal?: AbortSignal) => Promise<T>;
abort: () => void;
};
export declare function useAction<T, E, P extends Record<string, any>>(action: ActionType<T, E, P>): ActionReturnType<T, E, P>;
export declare function useOnMount(callback: () => void): void;
export declare function useOnRun(callback: () => void, deps: ActionType<any, any, any>[]): void;
export declare function useOnSuccess(callback: () => void, deps: ActionType<any, any, any>[]): void;
export declare function useOnError(callback: () => void, deps: ActionType<any, any, any>[]): void;
type ActionFunction<T, P> = (props: P) => T;
interface EnhancedAction<T, P> extends ActionFunction<T, P> {
intercept: (interceptor: (props: P) => P) => void;
subscribe: (callback: (result: T) => void) => () => void;
callCount: number;
callCountTotal: number;
retry: () => void;
getState: () => T;
forwardCall: (callback: (props: P) => T) => void;
}
export declare function action<T, P extends Record<string, any>>(fn: ActionFunction<T, P>): EnhancedAction<T, P>;
export declare const interceptorFetch: ((payload: FetchPayloadType) => FetchPayloadType) & {
set: (fn: (payload: FetchPayloadType) => FetchPayloadType) => void;
};
export declare const interceptorFetchSuccess: ((data: any) => any) & {
set: (fn: (data: any) => any) => void;
};
export declare const interceptorFetchError: ((error: any) => any) & {
set: (fn: (error: any) => any) => void;
};
type FetchPayloadType = {
url: string;
qsObj?: Record<string, any>;
} & RequestInit;
export declare function Fetch(options?: {
debounce?: number;
throttle?: number;
}): ActionType<unknown, unknown, {
url: string;
qsObj?: Record<string, any>;
} & RequestInit> & ActionReturnType<unknown, unknown, {
url: string;
qsObj?: Record<string, any>;
} & RequestInit>;
export {};