The entity type of items in this collection
import { Entity, Collection } from '@hateoas-ts/resource';
type Post = Entity<
{ id: string; title: string },
{ self: Post; author: User }
>;
type User = Entity<
{ id: string; name: string },
{ self: User; posts: Collection<Post> }
>;
// Fetch a collection
const user = await client.go<User>('/users/123').get();
const postsState = await user.follow('posts').get();
// Access pagination metadata
console.log(`Page ${postsState.data.page.number} of ${postsState.data.page.totalPages}`);
console.log(`Total items: ${postsState.data.page.totalElements}`);
// Iterate collection items
for (const postState of postsState.collection) {
console.log(postState.data.title);
}
// Navigate to next page
const nextPage = await postsState.follow('next').get();
Represents a paginated collection of resources in HAL format.
Collection is a specialized Entity type for paginated lists. It includes:
The collection items are accessible via
state.collectionafter fetching.