@algoan/nestjs-pagination

中文文档
6.0.2发布于 2023/12/11ISCTypeScriptCommonJS近 30 天下载 5852

NestJS interceptor handling request's pagination

原始使用文档
文档目录5 节
  1. Installation
  2. Requirements
  3. Limits
  4. Quick Start
  5. Usage

Nest Logo

A Nest interceptor to display the Link and the Content Range in the response headers.

NestJS Request Pagination

A simple NestJS interceptor catching query parameters and format a Link Header, based on GitHub pagination API. This module uses format-link-header node module to build the response Link Header.

Installation

npm install --save @algoan/nestjs-pagination

Requirements

  • On this version, the API attached with this interceptor needs to return an object:
{
  "totalResources": 1530,
  "resources": [ { ... }, ..., { ... }]
}
  • The resource has to be specified in the interceptor constructor

Limits

  • This module does not take into account what is returned in the resources property. It may be inconsistent with headers set by the interceptor.

Quick Start

Import LinkHeaderInterceptor next to a controller method.

import { LinkHeaderInterceptor } from '@algoan/nestjs-pagination';
import { Controller, Get, UseInterceptors } from '@nestjs/common';

@Controller()
/**
 * Controller returning a lot of documents
 */
class AppController {
  /**
   * Find all documents
   */
  @UseInterceptors(new LinkHeaderInterceptor({ resource: 'data' }))
  @Get('/data')
  public async findAll(): Promise<{ totalResources: number; resources: DataToReturn[] }> {
    const data: DataToReturn = await model.find(...);
    const count: number = await model.count();

    return { totalResources: count, resources: data };
  }
}

For instance, if you have 1015 resources, calling GET /data?page=4&per_page=100 will return:

Content-Range: data 300-399/1015
Link: </data?page=1&per_page=100>; rel="first", </data?page=11&per_page=100>; rel="last", </data?page=5&per_page=100>; rel="next", </data?page=3&per_page=100>; rel="prev"

If you use MongoDB as your database, we also provide a ParamDecorator you can use to convert the request to a mongo query parameter.

import { LinkHeaderInterceptor, MongoPaginationParamDecorator, MongoPagination, Pageable } from '@algoan/nestjs-pagination';
import { Controller, Get, UseInterceptors } from '@nestjs/common';

@Controller()
/**
 * Controller returning a lot of documents
 */
class AppController {
  /**
   * Find all documents
   */
  @UseInterceptors(new LinkHeaderInterceptor({ resource: 'data' }))
  @Get('/data')
  public async findAll(@MongoPaginationParamDecorator() pagination: MongoPagination ): Promise<Pageable<DataToReturn>> {
    const data: DataToReturn[] = await model.find(pagination);
    const count: number = await model.count(pagination.filter);

    return { totalResources: count, resources: data };
  }
}

Usage

By default, the interceptor and the decorator will look for the query parameters page and per_page (which is 100 by default). You can also change the name of those parameters and the default per page limit if you want, by passing a configuration object. Beware that if you use both the interceptor and the decorator, you need to pass the configuration to both of them.

new LinkHeaderInterceptor({ resource: 'data', pageName: '_page', perPageName: 'numberPerPage', defaultLimit: 50 })

@MongoPaginationParamDecorator({ pageName: '_page', perPageName: 'numberPerPage', defaultLimit: 50  })

You can also have the pagination in the response body by using the PaginationBodyInterceptor. By using this interceptor, the pagination is included in the response body instead of the header.

A paginated data response is returned.

{
  "resources": [],
  "pagination": {
    "next": null,
    "previous": null,
    "first": null,
    "last": null,
    "totalPages": 1,
    "totalResources": 0,
  }
}

Import the PaginationBodyInterceptor next to a controller method.

import { PaginationBodyInterceptor } from '@algoan/nestjs-pagination';
import { Controller, Get, UseInterceptors } from '@nestjs/common';

@Controller()
/**
 * Controller returning a lot of documents
 */
class AppController {
  /**
   * Find all documents
   */
  @UseInterceptors(new PaginationBodyInterceptor({pageName: 'page', perPageName: 'limit'}))
  @Get('/data')
  public async findAll(@MongoPaginationParamDecorator({pageName: 'page', perPageName: 'limit'}) pagination: MongoPagination): Promise<{ totalResources: number; resources: DataToReturn[] }> {
    const data: DataToReturn = await model.find(...);
    const count: number = await model.count();

    return { totalResources: count, resources: data };
  }
}

版本与发布记录

@algoan/nestjs-pagination 共发布 44 个版本,首次发布于 2020/01/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.0.2latest2023/12/112 年前
6.0.12023/11/102 年前
6.0.02023/03/013 年前
5.0.112023/02/253 年前
5.0.102023/02/033 年前
5.0.92023/01/313 年前

关于 @algoan/nestjs-pagination 的常见问题

@algoan/nestjs-pagination 是什么?

NestJS interceptor handling request's pagination

如何安装 @algoan/nestjs-pagination?

在项目目录运行 npm install @algoan/nestjs-pagination,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:rxjs(>=7)、express(>=4)、@nestjs/core(>=7)、@nestjs/common(>=7)、@nestjs/platform-express(>=7)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@algoan/nestjs-pagination 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@algoan/nestjs-pagination 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@algoan/nestjs-pagination 可以商用吗?

@algoan/nestjs-pagination 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@algoan/nestjs-pagination 的下载量有多少?

根据 npm 官方下载统计,@algoan/nestjs-pagination 在 2026/10/06 – 2026/11/04 这 30 天内被下载 5,852 次,日均约 195 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@algoan/nestjs-pagination 还在维护吗?

最新版本 6.0.2 发布于 2023/12/11(2 年前);至今共发布 44 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/01/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。