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.
Setting custom metadata: A better way - Nest.js Tutorial
From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework
Setting custom metadata: A better way
Previously, we saw how to attach metadata to a route handler, where we used the set metadata decorator. But this way of directly providing metadata to a route handler is not considered a good practice. So, there is an alternate and a better way to provide or attach custom metadata to any route handler. Let's see how we can do that. So first of all, to attach a role value with metadata, instead of specifying a role value directly, a better approach is to create an enum of roles. I'll create a folder, naming it as enum and a TypeScript file, roles.enum.ts, in which I'll create an enum called role and the values user equals user and admin equals admin. We will use these enum values for assigning roles. Now I'm going to create a custom decorator for assigning the metadata. I'm again creating a folder, naming it as custom decorator and a TypeScript file roles.decorator.ts. Here we will import the set metadata decorator from the nestjs common module. Then I'll export a constant named roles,…
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)
-
-
-
-
-
-
-