Skip to content

Commit 0556c64

Browse files
committed
Update docs for nestjs-any-guard: feat: init any-guard project
1 parent ad9522b commit 0556c64

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

.page

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ nestjs-command
2020
nestjs-seeder
2121
nestjs-testing
2222
typeorm-migrations
23+
nestjs-any-guard

pages/nestjs-any-guard/index.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
displayed_sidebar: docs
3+
title: "@hodfords/nestjs-any-guard"
4+
---
5+
<p align="center">
6+
<a href="http://opensource.hodfords.uk" target="blank"><img src="https://opensource.hodfords.uk/img/logo.svg" width="320" alt="Nest Logo" /></a>
7+
</p>
8+
9+
<p align="center">
10+
Nestjs-AnyGuard is a custom guard for NestJS that allows you to apply multiple guards to a single route. It will pass the request to the first guard that returns true, and if none of the guards return true, it will throw a ForbiddenException.
11+
</p>
12+
13+
## Installation 🤖
14+
15+
To begin using it, we first install the required dependencies.
16+
17+
```
18+
npm install @hodfords/nestjs-any-guard
19+
```
20+
## Usage 📖
21+
22+
### AnyGuard Decorator
23+
**AnyGuard** is a custom decorator that allows you to apply multiple guards to a single route. It will pass the request to the first guard that returns true, and if none of the guards return true, it will throw an ForbiddenException.
24+
```typescript
25+
@Post('example')
26+
@AnyGuard(Guard1, Guard2, Guard3)
27+
async example(@Body() body: any) {
28+
return body;
29+
}
30+
```
31+
32+
### Custom Guards
33+
**FirstSuccessGuard** is a custom guard that checks if any of the provided guards return true. If any guard returns true, it allows the request to proceed; otherwise, it throws a ForbiddenException.
34+
35+
```typescript
36+
37+
@Post('example')
38+
@UseGuards(FirstSuccessGuard(Guard1, Guard2, Guard3))
39+
async example(@Body() body: any) {
40+
return body;
41+
}
42+
43+
```
44+
45+
**HasHeaderGuard** checks if a specific header is present in the request. If the header is present and its value is 'true', it allows the request to proceed. Otherwise, it denies access.
46+
It is useful when you use with API-Gateway
47+
48+
```typescript
49+
@Post('example')
50+
@UseGuards(HasHeaderGuard('permission-passed'))
51+
async example(@Body() body: any) {
52+
return body;
53+
}
54+
```
55+
56+
57+
## License
58+
59+
This project is licensed under the MIT License

0 commit comments

Comments
 (0)