@hateoas-ts/resource-react - v1.4.0
    Preparing search index...

    Type Alias State<TEntity>

    Represents the state of a REST resource at a specific point in time.

    State is the result of fetching a resource and contains:

    • data: The resource's payload/properties
    • collection: Embedded collection items (for collection resources)
    • links: Available navigation links to related resources
    • actions: Executable forms/templates for state transitions

    State objects are immutable snapshots. Use Resource methods to modify the server state and obtain new State objects.

    const state = await client.go<User>('/users/123').get();

    // Access data
    console.log(state.data.name);

    // Navigate via links
    const postsResource = state.follow('posts');
    const posts = await postsResource.get();

    // Execute actions
    if (state.hasActionFor('edit')) {
    const action = state.actionFor('edit');
    await action.submit({ name: 'New Name' });
    }
    • Resource for fetching and modifying resources
    • Entity for defining resource types
    • Action for executable forms
    type State<TEntity extends Entity = Entity> = {
        collection: StateCollection<TEntity>;
        data: TEntity["data"];
        timestamp: number;
        uri: string;
        action<K extends string | number | symbol>(
            name: K,
        ): Action<TEntity["links"][K]>;
        clone(): State<TEntity>;
        contentHeaders(): Headers;
        follow<K extends string | number | symbol>(
            rel: K,
            variables?: LinkVariables,
        ): Resource<TEntity["links"][K]>;
        getLink<K extends string | number | symbol>(rel: K): Link | undefined;
        hasLink<K extends string | number | symbol>(rel: K): boolean;
        serializeBody(): string | Blob | Buffer<ArrayBufferLike>;
    }

    Type Parameters

    • TEntity extends Entity = Entity

      The entity type defining data shape and available links

    Index

    Properties

    collection: StateCollection<TEntity>

    Represents the collection state of the resource

    Contains an array of State objects for each element in the collection when the entity is a collection type Returns an empty array when the entity is not a collection type Supports navigation and state management for paginated collections

    data: TEntity["data"]

    Represents the body of the HTTP response.

    In the case of a JSON response, this will be deserialized

    timestamp: number

    Timestamp of when the State was first generated

    uri: string

    The URI associated with this state

    Methods

    • Return an action by name.

      If no name is given, the first action is returned. This is useful for formats that only supply 1 action, and no name.

      Type Parameters

      • K extends string | number | symbol

      Parameters

      • name: K

      Returns Action<TEntity["links"][K]>

    • Creates a deep clone of this state object.

      Useful for creating modified copies of state without affecting the original.

      Returns State<TEntity>

      A new State instance with the same data

    • Returns content-related HTTP headers for this state.

      These headers (e.g., Content-Type, Content-Length) describe the resource content and are used when sending the state back to the server.

      Returns Headers

      Headers object containing content-related headers

    • Follows a relationship to create a Resource for navigation.

      This is the primary method for HATEOAS-driven navigation. The returned Resource can be used to fetch the linked resource's state.

      Type Parameters

      • K extends string | number | symbol

        The link relation name (e.g., 'self', 'posts', 'author')

      Parameters

      • rel: K

        The relation type to follow

      • Optionalvariables: LinkVariables

        Optional template variables for URI expansion

      Returns Resource<TEntity["links"][K]>

      A Resource instance for the linked resource

      // Follow a simple link
      const authorResource = postState.follow('author');
      const author = await authorResource.get();

      // Follow with template variables
      const searchResource = state.follow('search', { q: 'hello' });
    • Gets the raw link object for a given relation.

      Type Parameters

      • K extends string | number | symbol

        The link relation name

      Parameters

      • rel: K

        The relation type to retrieve

      Returns Link | undefined

      The Link object or undefined if not found

      follow for navigating to linked resources

    • Checks if a link with the given relation exists.

      Type Parameters

      • K extends string | number | symbol

        The link relation name

      Parameters

      • rel: K

        The relation type to check for

      Returns boolean

      true if the link exists, false otherwise

    • Serializes the state for use in HTTP request bodies.

      For JSON resources, this typically returns JSON.stringify(data). The serialization format depends on the content type.

      Returns string | Blob | Buffer<ArrayBufferLike>

      The serialized body as Buffer, Blob, or string