Applies a function wrapped in a Resource to the Data
value, supporting applicative functor operations.
For Data
, applies the function if the input is Data
; otherwise, returns unchanged.
A new Resource with the applied result or the original non-Data/Failure state.
const fnRes = Resource.Data((x: number) => x + 1, { id: '123' });
const dataRes = Resource.Data(42, { id: '123' });
const result = fnRes.ap(dataRes); // Data<43, { id: '123' }>
const failureFn = Resource.Failure<(x: number) => number>(['fn error']);
const result2 = failureFn.ap(dataRes); // Failure<number> (propagates failure from function resource)
const failureVal = Resource.Failure<number>(['val error']);
const result3 = fnRes.ap(failureVal); // Failure<number> (propagates failure from value resource)
Returns the Data
value or a fallback value for non-Data
states.
Fallback value for non-Data
states.
The Data
value or the fallback.
Transforms the Data
value using a function, preserving the Resource structure.
Acts as a functor operation, similar to Array.map
. Non-Data
variants return unchanged.
A new Resource with the transformed value (for Data
) or the same instance (for others).
Matches the Resource state with a pattern, returning a value.
Enables declarative state handling, similar to a switch
statement.
Object with functions for each Resource state.
The result of the matching function.
Matches the Resource state with a partial pattern, performing side effects.
Partial object with functions for some Resource states.
Static
ofCreates a new Query
Resource.
Optional
params: QOptional parameters (e.g., query params).
Represents a loading state, typically for asynchronous operations.