@hateoas-ts/resource - v1.4.3
    Preparing search index...

    Interface Client

    A HATEOAS client for navigating HAL-compliant REST APIs.

    The Client provides the entry point for resource navigation and supports middleware for request interception (authentication, logging, etc.).

    import { createClient, Entity } from '@hateoas-ts/resource';

    type User = Entity<{ id: string; name: string }, { self: User }>;

    const client = createClient({ baseURL: 'https://api.example.com' });
    const user = await client.go<User>('/users/123').get();
    console.log(user.data.name);
    interface Client {
        cache: Pick<Cache, "get">;
        go<TEntity extends Entity<any, Record<string, any>>>(
            link?: string | NewLink,
        ): Resource<TEntity>;
        registerContentType(
            mimeType: string,
            factory: StateFactory,
            quality?: string,
        ): void;
        use(middleware: FetchMiddleware, origin?: string): void;
    }
    Index

    Properties

    Methods

    Properties

    cache: Pick<Cache, "get">

    Methods

    • Navigate to a resource by path or link object.

      Type Parameters

      • TEntity extends Entity<any, Record<string, any>>

        The entity type for the target resource

      Parameters

      • Optionallink: string | NewLink

        Path relative to baseURL or a NewLink object with href and templated properties

      Returns Resource<TEntity>

      A Resource instance for the target endpoint

      // Navigate by path
      const userResource = client.go<User>('/users/123');
      const user = await userResource.get();

      // Navigate with link object (for templated URIs)
      const resource = client.go<User>({ href: '/users/{id}', templated: true });
    • Registers or overrides a content-type parser at runtime.

      Use this to plug in parsers for media types such as JSON:API, Siren, Collection+JSON, HTML, or any custom type.

      Parameters

      • mimeType: string
      • factory: StateFactory
      • Optionalquality: string

      Returns void

    • Add a fetch middleware to intercept requests and responses.

      Middlewares are executed in order for each fetch() call. Use middlewares for authentication, logging, error handling, or request transformation.

      Parameters

      • middleware: FetchMiddleware

        Middleware function that receives request and next function

      • Optionalorigin: string

        Optional origin filter. Use '*' for all origins (default), or specify a host like 'https://api.example.com'

      Returns void

      // Add authentication middleware
      client.use(async (request, next) => {
      request.headers.set('Authorization', `Bearer ${token}`);
      return next(request);
      });

      // Add logging middleware for specific origin
      client.use(async (request, next) => {
      console.log('Request:', request.url);
      const response = await next(request);
      console.log('Response:', response.status);
      return response;
      }, 'https://api.example.com');