Skip to main content

TypeScript Decorator Patterns for NestJS

Build custom TypeScript decorators for authentication, caching, validation, logging, and rate limiting in NestJS applications.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt generates a library of custom TypeScript decorators for a NestJS [app_type]. It specifies nine concrete decorators — @Auth(roles), @Cacheable(ttl, key) backed by [cache_backend], @Validate(schema), @Log(level), @RateLimit(max, window), @Retry(attempts, backoff) for [retry_targets], @Timeout(ms), and @Audit(action) — plus correct decorator composition and unit tests for each using NestJS testing utilities.

The structure works because cross-cutting concerns like auth, caching, and logging clutter business logic when handled inline. Decorators move that noise to the method signature, but they are easy to get subtly wrong: execution order when several stack on one method, and TypeScript metadata reflection setup, are the two failure points. By demanding composition tests and explicit reflection configuration, the prompt forces the output to confront exactly those pitfalls rather than producing decorators that work alone but break together.

When to use it

  • When building a NestJS [app_type] and you want auth and caching out of controllers
  • When the same cross-cutting logic is copy-pasted across many service methods
  • When you need method-level caching against [cache_backend] with clean invalidation
  • When external calls in [retry_targets] need consistent retry-with-backoff behavior
  • When you want an audit trail recorded declaratively rather than by hand each time
  • When standardizing logging, timeouts, and rate limits across a growing team
  • When you want method-level concerns expressed declaratively at the signature instead of buried inside each handler

Example output

Expect a set of TypeScript decorator implementations using factory patterns and reflect-metadata, each with a usage example and a NestJS unit test. You get @Auth, @Cacheable backed by [cache_backend], @Validate, @Log, @RateLimit, @Retry scoped to [retry_targets], @Timeout, and @Audit, plus the compiler configuration that makes decorator metadata available at runtime. The output includes the metadata reflection setup and at least one composition test showing the correct execution order when multiple decorators stack on a single method, rather than isolated snippets that work alone but break when combined in a real [app_type] controller.

Pro tips

  • Pin down [cache_backend] early; Redis-backed @Cacheable needs an invalidation story, so ask the prompt to show how cached entries are cleared
  • Treat the composition tests as non-negotiable — getting @Auth, @RateLimit, and @Cacheable to stack in the right order is the part teams get wrong
  • Scope [retry_targets] precisely; retrying non-idempotent database writes can double-apply side effects, so be explicit about what is safe to retry
  • Confirm the output enables emitDecoratorMetadata and experimentalDecorators and imports reflect-metadata once at the entry point
  • Keep @Validate(schema) using Zod as specified so DTO validation stays type-safe end to end
  • Iterate decorator by decorator; generating all nine at once tends to produce shallow tests, so refine each with its own edge cases

Frequently Asked Questions

Does decorator execution order actually matter here?
Yes, significantly. When you stack decorators like `@Auth`, `@RateLimit`, and `@Cacheable` on one method, the order determines whether auth runs before caching, for example. The prompt requests composition tests precisely because getting this order wrong is a common, subtle bug.
What caching backend does the @Cacheable decorator use?
It uses whatever you set in `[cache_backend]`, defaulting to Redis. The prompt also asks for automatic invalidation, so request that the generated decorator shows how stale entries are cleared, since a cache without an invalidation story causes stale-data bugs.
Will this set up TypeScript metadata reflection correctly?
The prompt explicitly asks for metadata reflection setup and decorator factory patterns. You should confirm the output enables the decorator compiler options and imports reflect-metadata once at the entry point, as missing this is the most frequent setup failure.
Is the @Retry decorator safe for any method?
Only for idempotent operations. The `[retry_targets]` variable lets you scope retries to safe calls like external APIs and read transactions. Retrying non-idempotent writes can apply side effects more than once, so be deliberate about what you include.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Node.js & TypeScript Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support