Skip to content

Commit

Permalink
docs(examples): Add NestJS example (#688)
Browse files Browse the repository at this point in the history
Uses Node.js SDK
Uses a Nest Guard with external default Arcjet configuration file Allows each controller to augment the Arcjet configuration
  • Loading branch information
bendechrai authored Apr 27, 2024
1 parent 330b317 commit f9cbc35
Show file tree
Hide file tree
Showing 21 changed files with 9,407 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/nodejs-nestjs/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ARCJET_KEY=
25 changes: 25 additions & 0 deletions examples/nodejs-nestjs/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
56 changes: 56 additions & 0 deletions examples/nodejs-nestjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# compiled output
/dist
/node_modules
/build

# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

# Tests
/coverage
/.nyc_output

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# temp directory
.temp
.tmp

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
4 changes: 4 additions & 0 deletions examples/nodejs-nestjs/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
77 changes: 77 additions & 0 deletions examples/nodejs-nestjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<a href="https://arcjet.com" target="_arcjet-home">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://arcjet.com/arcjet-logo-minimal-dark-mark-all.svg">
<img src="https://arcjet.com/arcjet-logo-minimal-light-mark-all.svg" alt="Arcjet Logo" height="128" width="auto">
</picture>
</a>

# Arcjet Protection with NestJS for Node.js

This example shows how to use Arcjet to protect [NestJS](https://nestjs.com/) apps using the Node.js SDK.

## How to use

1. From the root of the project, install the SDK dependencies.

```bash
npm ci
```

2. Enter this directory and install the example's dependencies.

```bash
cd examples/nodejs-nestjs
npm ci
```

3. Rename `.env.example` to `.env` and add your Arcjet key.

4. Start the server.

```bash
npm start
```

5. Visit `http://localhost:3000/` in a browser.

6. Visit `http://localhost:3000/protected` in a browser and refresh the page to trigger the rate limit.

7. Test shield by making this request 5 or more times:

```bash
curl -v -H "x-arcjet-suspicious: true" http://localhost:3000
```

## How it works

[ArcjetGuard](src/arcjet/arcjet.guard.ts) is a NestJS Guard, which is a type of middleware that is used to determine whether a request should be handled by the route handler or not. Guards have a single responsibility. They determine whether a request will be handled by the route handler, depending on certain conditions like permissions or roles.

ArcjetGuard is defined as a provider in one or more modules, such as [AppModule](src/app.module.ts) and [ProtectedModule](src/protected/protected.module.ts). At this point, it's also possible to augment the rules from the default values by prefixing the ArcjetGuard provider definition with an extra, inline provider definition, as shown here. Check the [Node.js SDK Configuration documentation](https://docs.arcjet.com/reference/nodejs#configuration) for information on the rules available.
```json
providers: [
ProtectedService,
{
provide: 'ARCJET_RULES',
useValue: [
fixedWindow({
mode: "LIVE",
window: "1m",
max: 1,
}),
],
},
ArcjetGuard,
]
```
Finally, ArcjetGuard is defined in controllers with the following line:
```js
...
@Controller()
@UseGuards(ArcjetGuard)
export class AppController {
...
```
8 changes: 8 additions & 0 deletions examples/nodejs-nestjs/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}
Loading

0 comments on commit f9cbc35

Please sign in to comment.