Phantom Stories Library - v0.0.8
    Preparing search index...

    Interface ResourceMethods<T, Q>

    Interface for Resource methods, implementing functor, applicative functor, and monad operations.

    interface ResourceMethods<T, Q> {
        ap: <A, B, QVal>(
            resourceWithValue: Resource<A, QVal>,
        ) => Resource<B, QVal>;
        chain: <R, P>(fn: (x: Data<T, Q>) => Resource<R, P>) => Resource<R, P>;
        getDataOr: (value: T) => T;
        map: <R>(fn: (x: T) => R) => Resource<R, Q>;
        mapSafe: <R>(fn: (x: T) => R) => Resource<R, Q>;
        matchWith: <R>(pattern: ResourcePattern<T, Q, R>) => R;
        matchWithPartial: (pattern: Partial<ResourcePattern<T, Q, void>>) => void;
        of: <R>(value: R) => Resource<R, Q>;
        update: <P>(params: P) => Query<P>;
    }

    Type Parameters

    • T
    • Q

    Implemented by

    Index

    Properties

    ap: <A, B, QVal>(resourceWithValue: Resource<A, QVal>) => Resource<B, QVal>

    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.

    Type declaration

      • <A, B, QVal>(resourceWithValue: Resource<A, QVal>): Resource<B, QVal>
      • Type Parameters

        • A
        • B
        • QVal

        Parameters

        • resourceWithValue: Resource<A, QVal>

          Resource containing the value to apply the function to.

        Returns Resource<B, QVal>

        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)
    chain: <R, P>(fn: (x: Data<T, Q>) => Resource<R, P>) => Resource<R, P>

    Chains a function that returns a Resource, enabling monadic composition. For Data, applies the function; for others, returns unchanged.

    Type declaration

      • <R, P>(fn: (x: Data<T, Q>) => Resource<R, P>): Resource<R, P>
      • Type Parameters

        • R
        • P

        Parameters

        • fn: (x: Data<T, Q>) => Resource<R, P>

          Function that takes a Data and returns a new Resource.

        Returns Resource<R, P>

        The Resource returned by fn (for Data) or the same instance (for others).

    const data = Resource.Data(42, { id: '123' });
    const result = data.chain(d => Resource.Data(d.value * 2, d.params)); // Data<84, { id: '123' }>
    getDataOr: (value: T) => T

    Returns the Data value or a fallback value for non-Data states.

    Type declaration

      • (value: T): T
      • Parameters

        • value: T

          Fallback value for non-Data states.

        Returns T

        The Data value or the fallback.

    const data = Resource.Data(42);
    const value = data.getDataOr(0); // 42
    const failure = Resource.Failure(['error']);
    const fallback = failure.getDataOr(0); // 0
    map: <R>(fn: (x: T) => R) => Resource<R, Q>

    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.

    Type declaration

      • <R>(fn: (x: T) => R): Resource<R, Q>
      • Type Parameters

        • R

        Parameters

        • fn: (x: T) => R

          Function to transform the Data value.

        Returns Resource<R, Q>

        A new Resource with the transformed value (for Data) or the same instance (for others).

    const data = Resource.Data(42, { id: '123' });
    const result = data.map(x => x + 1); // Data<43, { id: '123' }>
    mapSafe: <R>(fn: (x: T) => R) => Resource<R, Q>

    Safely transforms the Data value, catching errors and returning Failure if the function throws. Useful for error-prone operations (e.g., parsing JSON).

    Type declaration

      • <R>(fn: (x: T) => R): Resource<R, Q>
      • Type Parameters

        • R

        Parameters

        • fn: (x: T) => R

          Function to transform the Data value.

        Returns Resource<R, Q>

        A new Data with the transformed value or Failure if an error occurs.

    const data = Resource.Data('{"name": "Jane"}', { id: '123' });
    const result = data.mapSafe(JSON.parse); // Data<{ name: 'Jane' }> or Failure
    matchWith: <R>(pattern: ResourcePattern<T, Q, R>) => R

    Matches the Resource state with a pattern, returning a value. Enables declarative state handling, similar to a switch statement.

    Type declaration

      • <R>(pattern: ResourcePattern<T, Q, R>): R
      • Type Parameters

        • R

        Parameters

        Returns R

        The result of the matching function.

    const resource = Resource.Data(42, { id: '123' });
    const result = resource.matchWith({
    Data: ({ value }) => value * 2,
    Query: () => 0,
    Empty: () => 0,
    Failure: () => 0,
    }); // 84
    matchWithPartial: (pattern: Partial<ResourcePattern<T, Q, void>>) => void

    Matches the Resource state with a partial pattern, performing side effects.

    Type declaration

      • (pattern: Partial<ResourcePattern<T, Q, void>>): void
      • Parameters

        • pattern: Partial<ResourcePattern<T, Q, void>>

          Partial object with functions for some Resource states.

        Returns void

    Resource.Failure(['error']).matchWithPartial({
    Failure: ({ messages }) => console.log(messages),
    });
    of: <R>(value: R) => Resource<R, Q>

    Lifts a value into a Data Resource, preserving existing params (if any). Acts as the monadic pure operation.

    Type declaration

      • <R>(value: R): Resource<R, Q>
      • Type Parameters

        • R

        Parameters

        • value: R

          Value to lift into Data.

        Returns Resource<R, Q>

        A new Data Resource with the value and existing params.

    const data = Resource.Data(42, { id: '123' });
    const result = data.of(100); // Data<100, { id: '123' }>
    update: <P>(params: P) => Query<P>

    Transitions to a Query state with new parameters.

    Type declaration

      • <P>(params: P): Query<P>
      • Type Parameters

        • P

        Parameters

        • params: P

          New parameters for the Query.

        Returns Query<P>

        A new Query Resource.

    const data = Resource.Data(42, { id: '123' });
    const query = data.update({ id: '456' }); // Query<{ id: '456' }>