@ptomulik/octokit-paginate-rest-limit
TypeScript icon, indicating that this package has built-in type declarations

1.1.4 • Public • Published

Paginate Rest Limit

tests build code coverage semantic-release @latest

Description

Provides callback function that limits number of entries returned by @octokit/plugin-paginate-rest.js.

The paginate method provided by @octokit/plugin-paginate-rest.js fetches all available entries page-by-page and returns them gathered into a single array. The octokit-paginate-rest-limit.ts allows to reduce the number of entries returned (and pages fetched) when only few first records are required.

Usage

Browsers

Load @ptomulik/octokit-paginate-rest-limit (and related modules) directly from cdn.skypack.dev

<script type="module">
  import { Octokit } from "https://cdn.skypack.dev/@octokit/core";
  import { paginateRest } from "https://cdn.skypack.dev/@octokit/plugin-paginate-rest";
  import { limit, adjust, MapFunction } from "https://cdn.skypack.dev/@ptomulik/octokit-paginate-rest-limit";
</script>
Node

Install @ptomulik/octokit-paginate-rest-limit (and related modules) with

npm install @ptomulik/octokit-paginate-rest-limit @octokit/plugin-paginate-rest @octokit/core

then import modules in your code

import { Octokit } from "@octokit/core";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { limit, adjust, MapFunction } from "@ptomulik/octokit-paginate-rest-limit";

Applying limit

import { Octokit } from "@octokit/core";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { limit, MapFunction } from "@ptomulik/octokit-paginate-rest-limit";

const MyOctokit = Octokit.plugin(paginateRest);
const octokit = new MyOctokit();

type Response = { data: { tag_name: string }[] };

// Retrieve (no more than) 2 releases and print their tag names.
octokit.paginate(
  "GET /repos/{owner}/{repo}/releases",
  {owner: "octokit", repo: "plugin-paginate-rest.js"},
  limit(2) as MapFunction<Response>
).then((releases) => {
  console.log(releases.map((release) => release.tag_name));
});

Example output

[ 'v2.13.3', 'v2.13.2' ]

Applying limit & map function at once.

import { Octokit } from "@octokit/core";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { limit } from "@ptomulik/octokit-paginate-rest-limit";

const MyOctokit = Octokit.plugin(paginateRest);
const octokit = new MyOctokit();

type Response = { data: { tag_name: string }[] };

// Retrieve (no more than) 2 releases and print their tag names.
octokit
  .paginate(
    "GET /repos/{owner}/{repo}/releases",
    { owner: "octokit", repo: "plugin-paginate-rest.js" },
    limit(2, ({ data }: Response) => data.map((release) => release.tag_name))
  )
  .then((names) => {
    console.log(names);
  });

Adjusting request parameters

Pagination parameters may be adjusted with adjust(max, parameters). This operation adjusts per_page property of parameters to avoid fetching redundant records.

import { Octokit } from "@octokit/core";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { limit, adjust, MapFunction } from "@ptomulik/octokit-paginate-rest-limit";

const MyOctokit = Octokit.plugin(paginateRest);
const octokit = new MyOctokit();
const max = 2;

type Response = { data: { tag_name: string }[] };

// Retrieve (no more than) `max` releases and print their tag names.
// The `per_page` is set to `max`.
octokit.paginate(
  "GET /repos/{owner}/{repo}/releases",
  adjust(max, {owner: "octokit", repo: "plugin-paginate-rest.js"}),
  limit(max) as MapFunction<Response>
).then((releases) => {
  console.log(releases.map((release) => release.tag_name));
});

Limitations

  • incompatible with paginate.iterator,

  • will cause bugs when a value (function) returned by limit() is reused; namely, this is WRONG:

      const first = limit(1);
      octokit.paginate(..., first);
      octokit.paginate(..., first); // !!this will not work as one would expect!!

LICENSE

Copyright (c) 2021 by Paweł Tomulik ptomulik@meil.pw.edu.pl

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

none

Package Sidebar

Install

npm i @ptomulik/octokit-paginate-rest-limit

Weekly Downloads

2

Version

1.1.4

License

MIT

Unpacked Size

19.7 kB

Total Files

16

Last publish

Collaborators

  • ptomulik