From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework
Unlock this course with a free trial
Join today to access over 25,200 courses taught by industry experts.
Defining custom metadata - Nest.js Tutorial
From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework
Defining custom metadata
Next, provides an ability to attach our own custom set of data that is metadata to any route handler methods or classes. And that metadata can then be accessed by guards, interceptors or other decorators to alter the behavior or make any decision in the application. So let's see how we can set custom metadata. I am continuing with the previous example. I opened the app controller file and here I'll attach a custom metadata to get route handler by using the set metadata decorator. This decorator allows us to add custom metadata to a route handler. It takes two arguments, a key value of type string and a pair value. So here let me set the key as roles and the value as admin. This is how the key and value that is the metadata is attached to a route handler. And we typically follow the syntax for giving the value like this only that is a square bracket and a string value. If I hover on the decorator, there you can see the syntax in the description as well. So it's an array of values. Now…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Introduction to guards4m 14s
-
(Locked)
Understanding guard5m 33s
-
(Locked)
Understanding ExecutionContext6m 50s
-
(Locked)
Accessing arguments with getArgs()3m 42s
-
(Locked)
Limiting controller access with guard4m 6s
-
(Locked)
Understanding switchToHttp() method4m 59s
-
(Locked)
Assignment: API key authorization6m 10s
-
(Locked)
Applying multiple guards3m 14s
-
(Locked)
Defining custom metadata5m 6s
-
(Locked)
Setting custom metadata: A better way3m 25s
-
(Locked)
Applying role for specific handlers10m 3s
-
(Locked)
Applying multiple roles1m 35s
-
(Locked)
Applying global guards4m 46s
-
(Locked)
-
-
-
-
-
-
-