تركيب Decorators باستخدام applyDecorators
اجمع Decorators الخاصة بـ swagger والتحقق والمصادقة في Decorator واحد سهل الاستخدام هو @ApiSecureEndpoint
تركيب Decorators باستخدام applyDecorators درس مجاني في NestJS Enterprise Backend APIs على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في NestJS Enterprise Backend APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة NestJS Enterprise Backend APIs 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Decorator Stacking Problem
In a real NestJS enterprise API, almost every route handler ends up carrying a tall stack of decorators: Swagger docs, auth guards, role checks, and response shaping. Repeating that stack on every endpoint is verbose and error-prone.
- Duplication: the same six lines copied onto dozens of handlers.
- Drift: someone forgets
@ApiBearerAuth()on one route and the docs lie. - Noise: the actual business intent is buried under cross-cutting concerns.
This lesson teaches how to collapse a recurring decorator stack into a single reusable @ApiSecureEndpoint() using NestJS's applyDecorators.
// The pain: this stack repeats on every secured route
@Post('transfer')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
@ApiBearerAuth()
@ApiOperation({ summary: 'Move funds between accounts' })
@ApiOkResponse({ description: 'Transfer accepted' })
@ApiUnauthorizedResponse({ description: 'Missing or invalid token' })
async transfer(@Body() dto: TransferDto) {
return this.bank.transfer(dto);
}What applyDecorators Actually Does
applyDecorators is a helper from @nestjs/common. It takes any number of decorators and returns one new decorator that applies all of them in order when used.
- It works with method decorators, class decorators, and property decorators.
- The decorators run top-to-bottom, exactly as if you had written them by hand.
- It does not change behavior — it only composes. Whatever the original stack did, the composed decorator does identically.
Think of it as function composition for the decorator world.
import { applyDecorators, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
export function SecureRoute() {
return applyDecorators(
UseGuards(JwtAuthGuard),
ApiBearerAuth(),
ApiOperation({ summary: 'Protected route' }),
);
}A Custom Decorator Is Just a Function
Before composing, recall the shape of a custom decorator. A method decorator is a function that receives the target, the property key, and the property descriptor. NestJS decorators like UseGuards() are decorator factories: calling them returns such a function.
This plain-TypeScript example shows the mechanics with no framework involved — a logging decorator factory applied to a class method.
function LogCalls(label: string) {
return function (
_target: object,
key: string,
descriptor: PropertyDescriptor,
) {
const original = descriptor.value;
descriptor.value = function (...args: unknown[]) {
console.log(`[${label}] ${key} called`);
return original.apply(this, args);
};
};
}
class Calculator {
@LogCalls('math')
add(a: number, b: number): number {
return a + b;
}
}
const c = new Calculator();
console.log('result =', c.add(2, 3));Composing Without a Helper
To appreciate applyDecorators, see what manual composition looks like in pure TypeScript. A composer returns a single method decorator that loops over the inner decorators and invokes each.
This standalone snippet mirrors how NestJS's applyDecorators works under the hood — running every decorator against the same target.
type MethodDec = (t: object, k: string, d: PropertyDescriptor) => void;
function compose(...decorators: MethodDec[]): MethodDec {
return (target, key, descriptor) => {
for (const dec of decorators) {
dec(target, key, descriptor);
}
};
}
const tag = (name: string): MethodDec => (_t, k) =>
console.log(`applied ${name} to ${k}`);
class Service {
@compose(tag('auth'), tag('swagger'), tag('roles'))
handle(): string {
return 'ok';
}
}
console.log(new Service().handle());Building the First Version of @ApiSecureEndpoint
Now bundle the realistic stack. Our goal decorator @ApiSecureEndpoint() should attach JWT auth, role enforcement, the Swagger bearer scheme, and the common error responses.
UseGuardswires the runtime protection.ApiBearerAuthtells Swagger UI to send the token.- The
ApiUnauthorizedResponse/ApiForbiddenResponsedocument the failure modes once and for all.
import { applyDecorators, UseGuards } from '@nestjs/common';
import {
ApiBearerAuth,
ApiUnauthorizedResponse,
ApiForbiddenResponse,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../auth/roles.guard';
export function ApiSecureEndpoint() {
return applyDecorators(
UseGuards(JwtAuthGuard, RolesGuard),
ApiBearerAuth(),
ApiUnauthorizedResponse({ description: 'Missing or invalid token' }),
ApiForbiddenResponse({ description: 'Insufficient permissions' }),
);
}Using the Composed Decorator
With the composer in place, the controller collapses to a single intent-revealing line per route. The four concerns are still active — they are just declared once inside the factory.
Compare this to scene 1: the same protection and documentation, far less noise.
@Controller('accounts')
export class AccountsController {
constructor(private readonly bank: BankService) {}
@Post('transfer')
@ApiSecureEndpoint()
@ApiOperation({ summary: 'Move funds between accounts' })
async transfer(@Body() dto: TransferDto) {
return this.bank.transfer(dto);
}
}Passing Arguments Into the Composer
The real ergonomic win comes from parameterizing the factory. Because ApiSecureEndpoint is a function, it can accept options and forward them to the inner decorators — for example the required roles and a summary string.
This lets one decorator express the full security + docs contract of a route in a single, readable call.
import { applyDecorators, UseGuards, SetMetadata } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiForbiddenResponse } from '@nestjs/swagger';
export const ROLES_KEY = 'roles';
export function ApiSecureEndpoint(opts: { summary: string; roles?: string[] }) {
return applyDecorators(
UseGuards(JwtAuthGuard, RolesGuard),
SetMetadata(ROLES_KEY, opts.roles ?? []),
ApiBearerAuth(),
ApiOperation({ summary: opts.summary }),
ApiForbiddenResponse({ description: 'Insufficient permissions' }),
);
}Folding in Validation and Response Typing
Enterprise endpoints also document their success payload. applyDecorators can fold in a typed ApiOkResponse, and you can combine it with a serialization interceptor so the contract is enforced both in code and in the docs.
typedrives the Swagger response schema and example.- Adding
UseInterceptors(ClassSerializerInterceptor)guarantees the DTO transforms apply.
import { applyDecorators, UseGuards, UseInterceptors, ClassSerializerInterceptor, Type } from '@nestjs/common';
import { ApiBearerAuth, ApiOkResponse, ApiOperation } from '@nestjs/swagger';
export function ApiSecureEndpoint(opts: {
summary: string;
type: Type<unknown>;
}) {
return applyDecorators(
UseGuards(JwtAuthGuard, RolesGuard),
UseInterceptors(ClassSerializerInterceptor),
ApiBearerAuth(),
ApiOperation({ summary: opts.summary }),
ApiOkResponse({ type: opts.type, description: 'Success' }),
);
}Order Matters for Guards and Interceptors
Within a composed decorator, the relative order of UseGuards and UseInterceptors matters at runtime. NestJS runs guards before interceptors regardless, but when you list multiple guards their execution order follows the array, and multiple interceptors nest in declaration order.
- Put authentication guards before authorization guards so an unauthenticated request short-circuits with 401, not 403.
- Swagger-only decorators (
ApiOperation,ApiOkResponse) have no runtime ordering effect — they only attach metadata.
// JwtAuthGuard first => 401 before RolesGuard ever runs => correct
UseGuards(JwtAuthGuard, RolesGuard)
// Reversed => RolesGuard may read an empty user and throw 403
// for a request that was actually just unauthenticated
UseGuards(RolesGuard, JwtAuthGuard) // avoidKeeping the Decorator Testable
Because a composed decorator is a plain factory function, you can unit-test that it wires the metadata you expect without booting Nest. Use the Reflector or read metadata keys directly off the decorated method.
This standalone TypeScript example demonstrates the underlying idea — reading back metadata that a decorator attached via Reflect.defineMetadata.
import 'reflect-metadata';
function Roles(...roles: string[]) {
return (t: object, k: string) =>
Reflect.defineMetadata('roles', roles, t, k);
}
class Ctrl {
@Roles('admin', 'auditor')
remove() {}
}
const meta = Reflect.getMetadata('roles', Ctrl.prototype, 'remove');
console.log('declared roles =', meta);
console.log('admin allowed =', meta.includes('admin'));When NOT to Compose
Composition is powerful but can hide important details. Reach for a composed decorator only when the stack is genuinely repeated and stable.
- Do compose a fixed security + docs envelope used across many routes.
- Avoid composing highly route-specific details like a unique
ApiOperationsummary or one-off query params — pass those as arguments or leave them inline. - Avoid burying rarely-used behavior; a reader should still grasp what a route does from its decorators.
Good composed decorators reduce noise without becoming a black box.
Quick Check: Composing Decorators
You want a single @ApiSecureEndpoint() that applies a JWT guard, a roles guard, the Swagger bearer scheme, and shared error responses. Which approach is idiomatic in NestJS?
Recap & Takeaways
You learned to collapse a repetitive decorator stack into one ergonomic decorator.
applyDecoratorsfrom@nestjs/commoncomposes any number of decorators into a single one that applies them in order.- A composed decorator is just a factory function, so it can accept options (roles, summary, response type) and forward them to the inner decorators.
- Bundle stable cross-cutting concerns —
UseGuards,ApiBearerAuth, sharedApiUnauthorizedResponse/ApiForbiddenResponse— and keep route-specific details as arguments or inline. - Mind guard ordering: authentication before authorization so a missing token yields 401, not 403.
- Because it is plain TypeScript, the composed decorator stays testable via reflected metadata.
Result: controllers that read as intent (@ApiSecureEndpoint({ summary, roles })) instead of a wall of boilerplate.
الأسئلة الشائعة
هل درس «تركيب Decorators باستخدام applyDecorators» مجاني؟
نعم — نص درس «تركيب Decorators باستخدام applyDecorators» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة NestJS Enterprise Backend APIs، انتقل إلى CoddyKit PRO. تتضمن دورة NestJS Enterprise Backend APIs 4 دروس في المجموع.
ماذا ستتعلم في «تركيب Decorators باستخدام applyDecorators»؟
اجمع Decorators الخاصة بـ swagger والتحقق والمصادقة في Decorator واحد سهل الاستخدام هو @ApiSecureEndpoint تتمرن على NestJS Enterprise Backend APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ NestJS Enterprise Backend APIs؟
لا تُشترط خبرة سابقة. NestJS Enterprise Backend APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «تركيب Decorators باستخدام applyDecorators»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس NestJS Enterprise Backend APIs هذا؟
نعم. كل درس في NestJS Enterprise Backend APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- قراءة سياق الطلب باستخدام Param Decorators
- إرفاق البيانات الوصفية باستخدام SetMetadata وReflector
- تركيب Decorators باستخدام applyDecorators
- Decorators على مستوى الفئة لإعدادات الوظائف المشتركة