@77io/pagination
TypeScript icon, indicating that this package has built-in type declarations

2.0.10 • Public • Published

Pagination

Pagination makes paginated apis easy to work with. It returns an Observable that emits each page.

Given a base url and (optional) headers, easily grab records in a paginated style.

This works with any api that supports the link header.

An example grabbing all of the posts from the Wordpress api.

import Pagination from "@77io/pagination";
import { last, reduce } from "rxjs/operators";

// Setup the basic options
const baseUrl = wordpressRoot + "/wp-json/wp/v2/posts";
const headers = {
  accept: "application/json"
};

const sub = Pagination(baseUrl, headers)
  .pipe(
    reduce((arr: any[], page: any[]) => {
      return arr.concat(page);
    }, []),
    last()
  )
  .subscribe((allPosts: any[]) => {
    console.log("I got all posts!");
    sub.unsubscribe();
  });

An example getting the first page from the Wordpress api.

import Pagination from "@77io/pagination";

// Setup the basic options
const baseUrl = wordpressRoot + "/wp-json/wp/v2/posts";
const headers = {
  accept: "application/json"
};

const sub = Pagination(baseUrl, headers).subscribe((posts: any[]) => {
  console.log("I got the first page of posts!");
  sub.unsubscribe();
});

Readme

Keywords

Package Sidebar

Install

npm i @77io/pagination

Weekly Downloads

34

Version

2.0.10

License

ISC

Unpacked Size

13.5 kB

Total Files

27

Last publish

Collaborators

  • bmanderscheid
  • chris.dziewa
  • davgerd
  • 77matt