@@ -14,6 +14,15 @@ export interface RequestState<T = unknown> {
1414 data ?: T ;
1515}
1616
17+ /**
18+ * Creates a new RequestState with default values.
19+ * - loading: false
20+ * - success: false
21+ * - error: null
22+ * @template T Type of the request payload
23+ * @param initialData Optional initial data to attach
24+ * @returns Fresh request state instance
25+ */
1726export function createDefaultRequestState < T > ( initialData ?: T ) : RequestState < T > {
1827 return {
1928 loading : false ,
@@ -23,6 +32,15 @@ export function createDefaultRequestState<T>(initialData?: T): RequestState<T> {
2332 } ;
2433}
2534
35+ /**
36+ * Marks the request as loading and clears previous success/error flags.
37+ * Optionally primes the state with new data (useful for optimistic UI updates).
38+ * Note: data is only set when a truthy value is provided.
39+ * @template T
40+ * @param req Previous request state
41+ * @param data Optional data to set while loading
42+ * @returns Updated request state in loading state
43+ */
2644export function onRequestLoad < T > (
2745 req : RequestState < T > ,
2846 data ?: T
@@ -36,6 +54,14 @@ export function onRequestLoad<T>(
3654 } ;
3755}
3856
57+ /**
58+ * Marks the request as successfully completed with the given payload.
59+ * Clears error and stops loading.
60+ * @template T
61+ * @param req Previous request state
62+ * @param data Resulting data for the request
63+ * @returns Updated request state in success state
64+ */
3965export function onRequestSuccess < T > (
4066 req : RequestState < T > ,
4167 data ?: T
@@ -49,6 +75,14 @@ export function onRequestSuccess<T>(
4975 } ;
5076}
5177
78+ /**
79+ * Marks the request as failed with the provided error information.
80+ * Clears success and stops loading. Falls back to a default error when none is provided.
81+ * @template T
82+ * @param req Previous request state
83+ * @param errorInfo Structured error details
84+ * @returns Updated request state in error state
85+ */
5286export function onRequestError < T > (
5387 req : RequestState < T > ,
5488 errorInfo ?: ErrorInfo
0 commit comments