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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1applyDecorators lets you compose several decorators into one reusable unit.
- 2A generic decorator factory adapts Swagger docs to any model type without repetition.
- 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.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-reusable-paginated-swagger-decorator-in-nestjs-explained-typescript-4419/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.