1.0.0 • Published 4 years ago

paginated-respose v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

What is this?

This package format JSON-Array data to paginated form to send to client for front-end display.

Installation

Run npm i paginated-response

Usage

const paginatedResponse = require("paginated-response");

let paginated = paginatedResponse(jsonArray, page, perPage);
or;
let paginated = paginatedResponse(jsonArray, page);

Parameters

This package requires three parameters.

jsonArray -> 1st parameter - Main data

pageno -> 2nd parameter - Page no. to fetch

perPage -> 3rd parameter - no of data to send in a page, *Optional default is 10

Example

const paginatedResponse = require("paginated-response");

const jsonArray = [
  {
    _id: "5f78e2112cc17f206c131125",
    title: "bg",
  },
  {
    _id: "5f78e8cc3b076d0017b4e634",
    title: "Tags featured 🥳🥳",
  },
  {
    _id: "5f79af1466ac4d256899249e",
    title: "ad",
  },
  {
    _id: "5f79c6b107e3fa00176c4ba4",
    title: "Gg again",
  },
  {
    _id: "5f79c6d607e3fa00176c4ba5",
    title: "Wow",
  },
];

app.get("/api/posts", (req, res) => {
  const page = req.query.p; //page number requested
  const perPage = 2; //optional, default is 10
  let paginated = paginatedResponse(jsonArray, page, perPage);
  res.json(paginated);
  //see output below
});

Example Request:

fetch("/api/posts?p=3"); //request page no. 3 of posts

Example Response

{
  "page": 3,  //got page number
  "pageCount": 3,  //total pages
  "posts":[   //data of page 3
    {
      _id: "5f79c6d607e3fa00176c4ba5",
      title: "Wow",
    },
  ]
1.0.0

4 years ago