@nest-extended/decorators 0.0.2-beta-13 → 0.0.2-beta-15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
# @nest-extended/decorators
|
|
2
2
|
|
|
3
|
-
This package provides
|
|
3
|
+
This package provides reusable parameter decorators for NestJS applications in the **NestExtended** ecosystem. These decorators simplify request handling, authentication, and body modification.
|
|
4
4
|
|
|
5
5
|
## Key Features
|
|
6
6
|
|
|
7
|
-
- **`@User()`**:
|
|
8
|
-
- **`@Public()`**:
|
|
9
|
-
- **`@ModifyBody()`**:
|
|
7
|
+
- **`@User()`**: Parameter decorator that retrieves the current user from the request object (`req.user`). Works with JWT guards that attach the authenticated user to the request.
|
|
8
|
+
- **`@Public()`**: Method decorator that marks a route as public. Sets `isPublic` metadata which `AuthGuard` checks to bypass JWT verification. Uses the `IS_PUBLIC_KEY` constant.
|
|
9
|
+
- **`@ModifyBody(...fns)`**: Parameter decorator that intercepts and modifies the request body before it reaches the handler. Accepts one or more modifier functions that receive the full request and can mutate `req.body`.
|
|
10
|
+
- **`setCreatedBy(key?)`**: Built-in modifier function for `@ModifyBody()`. Sets `body[key]` (default: `'createdBy'`) to `req.user._id`. Can be customized with any key (e.g., `setCreatedBy('updatedBy')`).
|
|
10
11
|
|
|
11
12
|
## Installation
|
|
12
13
|
|
|
@@ -18,7 +19,7 @@ npm install @nest-extended/decorators
|
|
|
18
19
|
|
|
19
20
|
```typescript
|
|
20
21
|
import { User, Public, ModifyBody, setCreatedBy } from '@nest-extended/decorators';
|
|
21
|
-
import { Controller, Get, Post } from '@nestjs/common';
|
|
22
|
+
import { Controller, Get, Post, Patch } from '@nestjs/common';
|
|
22
23
|
|
|
23
24
|
@Controller('cats')
|
|
24
25
|
export class CatsController {
|
|
@@ -29,7 +30,22 @@ export class CatsController {
|
|
|
29
30
|
@Post()
|
|
30
31
|
create(@ModifyBody(setCreatedBy()) body: CreateDto) { ... }
|
|
31
32
|
|
|
33
|
+
@Patch('/:id')
|
|
34
|
+
update(@ModifyBody(setCreatedBy('updatedBy')) body: PatchDto) { ... }
|
|
35
|
+
|
|
32
36
|
@Get('profile')
|
|
33
37
|
getProfile(@User() user: any) { ... }
|
|
34
38
|
}
|
|
35
39
|
```
|
|
40
|
+
|
|
41
|
+
## Exported API
|
|
42
|
+
|
|
43
|
+
| Export | Type | Description |
|
|
44
|
+
|---|---|---|
|
|
45
|
+
| `User` | Decorator | Extract `req.user` from request |
|
|
46
|
+
| `Public` | Decorator | Mark route as public (skip auth) |
|
|
47
|
+
| `IS_PUBLIC_KEY` | Const | Metadata key used by `@Public()` (`'isPublic'`) |
|
|
48
|
+
| `ModifyBody` | Decorator | Modify request body with transform functions |
|
|
49
|
+
| `setCreatedBy` | Function | Body modifier — sets `createdBy`/custom key to user ID |
|
|
50
|
+
| `RequestBody<TBody, TUser>` | Type | Typed request with `body` and `user` |
|
|
51
|
+
| `ModifyBodyFn<TBody, TUser>` | Type | Function signature for `@ModifyBody()` transforms |
|