typescript 38 lines · 6 steps

A reusable paginated Swagger decorator in NestJS

A generic factory that bundles Swagger decorators to document any paginated endpoint from a single model type.

Explained by highlit
1import { Type, applyDecorators } from '@nestjs/common';
2import {
3 ApiExtraModels,
4 ApiOkResponse,
5 ApiQuery,
6 getSchemaPath,
7} from '@nestjs/swagger';
8 
9export class PaginationMetaDto {
10 page: number;
11 limit: number;
12 totalItems: number;
13 totalPages: number;
14}
15 
16export const ApiPaginatedResponse = <TModel extends Type<unknown>>(
17 model: TModel,
18 description?: string,
19) =>
20 applyDecorators(
21 ApiExtraModels(PaginationMetaDto, model),
22 ApiQuery({ name: 'page', required: false, type: Number, example: 1 }),
23 ApiQuery({ name: 'limit', required: false, type: Number, example: 20 }),
24 ApiOkResponse({
25 description: description ?? `Paginated list of ${model.name}`,
26 schema: {
27 type: 'object',
28 required: ['data', 'meta'],
29 properties: {
30 data: {
31 type: 'array',
32 items: { $ref: getSchemaPath(model) },
33 },
34 meta: { $ref: getSchemaPath(PaginationMetaDto) },
35 },
36 },
37 }),
38 );
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1applyDecorators lets you compose several decorators into one reusable unit.
  2. 2A generic decorator factory adapts Swagger docs to any model type without repetition.
  3. 3getSchemaPath with ApiExtraModels wires up $ref schemas so nested DTOs render correctly.

Related explainers

typescript
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
import localeFrExtra from '@angular/common/locales/extra/fr';
import localeDe from '@angular/common/locales/de';

Locale-aware bootstrapping in Angular

i18n localization dependency-injection
Intermediate 8 steps
typescript
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import * as Joi from 'joi';
 

Validating env config at boot in NestJS

configuration schema-validation environment-variables
Intermediate 8 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { InjectRepository } from '@nestjs/typeorm';

A cache-aside country lookup in NestJS

cache-aside dependency-injection batch-lookup
Intermediate 8 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core';
 
interface Preferences {
  theme: 'light' | 'dark';

A signal-based preferences store in Angular

signals state-management persistence
Intermediate 7 steps
typescript
import { useEffect, useState } from "react";
 
interface Section {
  id: string;

Building a scroll-spy hook in React

custom-hooks intersectionobserver dom-observation
Intermediate 8 steps
typescript
import { Injectable, Scope, Inject, NotFoundException } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';
import { DataSource } from 'typeorm';

Per-tenant database connections in NestJS

multi-tenancy connection-pooling dependency-injection
Advanced 8 steps

Share this explainer

Here's the card — post it anywhere.

A reusable paginated Swagger decorator in NestJS — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code