Skip to content

Commit 933067e

Browse files
Merge branch 'master' into docs/async-local-storage-recipe
2 parents 3bc6fb3 + bedce54 commit 933067e

File tree

261 files changed

+14449
-22195
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

261 files changed

+14449
-22195
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
16.16.0
1+
18.14.0

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(The MIT License)
22

3-
Copyright (c) 2017-2022 Kamil Myśliwiec <http://kamilmysliwiec.com>
3+
Copyright (c) 2017-2023 Kamil Myśliwiec <http://kamilmysliwiec.com>
44

55
Permission is hereby granted, free of charge, to any person obtaining
66
a copy of this software and associated documentation files (the

content/cli/libraries.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Any functionality that is suitable for re-use is a candidate for being managed a
1919
To get started with creating a library, run the following command:
2020

2121
```bash
22-
nest g library my-library
22+
$ nest g library my-library
2323
```
2424

2525
When you run the command, the `library` schematic prompts you for a prefix (AKA alias) for the library:
@@ -78,7 +78,7 @@ As with application-type projects, libraries each have their own `tsconfig.lib.j
7878
You can build the library with the CLI command:
7979
8080
```bash
81-
nest build my-library
81+
$ nest build my-library
8282
```
8383
8484
#### Using libraries

content/cli/workspaces.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ To enable monorepo mode, you start with a _standard mode_ structure, and add **p
2626
If we run:
2727

2828
```bash
29-
nest new my-project
29+
$ nest new my-project
3030
```
3131

3232
We've constructed a _standard mode_ structure, with a folder structure that looks like this:
@@ -49,8 +49,8 @@ We've constructed a _standard mode_ structure, with a folder structure that look
4949
We can convert this to a monorepo mode structure as follows:
5050

5151
```bash
52-
cd my-project
53-
nest generate app my-app
52+
$ cd my-project
53+
$ nest generate app my-app
5454
```
5555

5656
At this point, `nest` converts the existing structure to a **monorepo mode** structure. This results in a few important changes. The folder structure now looks like this:
@@ -197,8 +197,9 @@ These properties specify the compiler to use as well as various options that aff
197197
These properties specify the default generate options to be used by the `nest generate` command.
198198

199199
| Property Name | Property Value Type | Description |
200-
| ------------- | ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
200+
|---------------|---------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
201201
| `spec` | boolean _or_ object | If the value is boolean, a value of `true` enables `spec` generation by default and a value of `false` disables it. A flag passed on the CLI command line overrides this setting, as does a project-specific `generateOptions` setting (more below). If the value is an object, each key represents a schematic name, and the boolean value determines whether the default spec generation is enabled / disabled for that specific schematic. |
202+
| `flat` | boolean | If true, all generate commands will generate a flat structure |
202203

203204
The following example uses a boolean value to specify that spec file generation should be disabled by default for all projects:
204205

@@ -211,6 +212,17 @@ The following example uses a boolean value to specify that spec file generation
211212
}
212213
```
213214

215+
The following example uses a boolean value to specify flat file generation should be the default for all projects:
216+
217+
```javascript
218+
{
219+
"generateOptions": {
220+
"flat": true
221+
},
222+
...
223+
}
224+
```
225+
214226
In the following example, `spec` file generation is disabled only for `service` schematics (e.g., `nest generate service...`):
215227

216228
```javascript

content/controllers.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ getDocs(@Query('version') version) {
268268

269269
Routes with static paths won't work when you need to accept **dynamic data** as part of the request (e.g., `GET /cats/1` to get cat with id `1`). In order to define routes with parameters, we can add route parameter **tokens** in the path of the route to capture the dynamic value at that position in the request URL. The route parameter token in the `@Get()` decorator example below demonstrates this usage. Route parameters declared in this way can be accessed using the `@Param()` decorator, which should be added to the method signature.
270270

271+
> info **Hint** Routes with parameters should be declared after any static paths. This prevents the parameterized paths from intercepting traffic destined for the static paths.
272+
271273
```typescript
272274
@@filename()
273275
@Get(':id')

content/discover/who-uses.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@
268268
"https://polygon-software.ch",
269269
"https://bewith.io",
270270
"https://swetrix.com",
271-
"https://swyftlogistics.com"
271+
"https://swyftlogistics.com",
272+
"https://evershop.io",
273+
"https://e-design.ca",
274+
"https://growthmill.com",
275+
"https://tarken.com.br"
272276
]
273277
}

content/exception-filters.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Out of the box, this action is performed by a built-in **global exception filter
1515
}
1616
```
1717

18-
> info **Hint** The global exception filter partially supports the `http-errors` library. Basically, any thrown exception containing the `statusCode` and `message` property will be properly populated and send back as a response (instead of the default `InternalServerErrorException` for unrecognized exceptions).
18+
> info **Hint** The global exception filter partially supports the `http-errors` library. Basically, any thrown exception containing the `statusCode` and `message` properties will be properly populated and sent back as a response (instead of the default `InternalServerErrorException` for unrecognized exceptions).
1919
2020
#### Throwing standard exceptions
2121

@@ -60,16 +60,24 @@ in the `response` argument. To override the entire JSON response body, pass an o
6060
The second constructor argument - `status` - should be a valid HTTP status code.
6161
Best practice is to use the `HttpStatus` enum imported from `@nestjs/common`.
6262

63-
Here's an example overriding the entire response body:
63+
There is a **third** constructor argument (optional) - `options` - that can be used to provide an error [cause](https://nodejs.org/en/blog/release/v16.9.0/#error-cause). This `cause` object is not serialized into the response object, but it can be useful for logging purposes, providing valuable information about the inner error that caused the `HttpException` to be thrown.
64+
65+
Here's an example overriding the entire response body and providing an error cause:
6466

6567
```typescript
6668
@@filename(cats.controller)
6769
@Get()
6870
async findAll() {
69-
throw new HttpException({
70-
status: HttpStatus.FORBIDDEN,
71-
error: 'This is a custom message',
72-
}, HttpStatus.FORBIDDEN);
71+
try {
72+
await this.service.findAll()
73+
} catch (error) {
74+
throw new HttpException({
75+
status: HttpStatus.FORBIDDEN,
76+
error: 'This is a custom message',
77+
}, HttpStatus.FORBIDDEN, {
78+
cause: error
79+
});
80+
}
7381
}
7482
```
7583

@@ -130,6 +138,22 @@ Nest provides a set of standard exceptions that inherit from the base `HttpExcep
130138
- `GatewayTimeoutException`
131139
- `PreconditionFailedException`
132140

141+
All the built-in exceptions can also provide both an error `cause` and an error description using the `options` parameter:
142+
143+
```typescript
144+
throw new BadRequestException('Something bad happened', { cause: new Error(), description: 'Some error description' })
145+
```
146+
147+
Using the above, this is how the response would look:
148+
149+
```json
150+
{
151+
"message": "Something bad happened",
152+
"error": "Some error description",
153+
"statusCode": 400,
154+
}
155+
```
156+
133157
#### Exception filters
134158

135159
While the base (built-in) exception filter can automatically handle many cases for you, you may want **full control** over the exceptions layer. For example, you may want to add logging or use a different JSON schema based on some dynamic factors. **Exception filters** are designed for exactly this purpose. They let you control the exact flow of control and the content of the response sent back to the client.
@@ -182,6 +206,8 @@ export class HttpExceptionFilter {
182206

183207
> info **Hint** All exception filters should implement the generic `ExceptionFilter<T>` interface. This requires you to provide the `catch(exception: T, host: ArgumentsHost)` method with its indicated signature. `T` indicates the type of the exception.
184208
209+
> warning **Warning** If you are using `@nestjs/platform-fastify` you can use `response.send()` instead of `response.json()`. Don't forget to import the correct types from `fastify`.
210+
185211
The `@Catch(HttpException)` decorator binds the required metadata to the exception filter, telling Nest that this particular filter is looking for exceptions of type `HttpException` and nothing else. The `@Catch()` decorator may take a single parameter, or a comma-separated list. This lets you set up the filter for several types of exceptions at once.
186212

187213
#### Arguments host
@@ -323,6 +349,8 @@ export class AllExceptionsFilter implements ExceptionFilter {
323349
}
324350
```
325351

352+
> warning **Warning** When combining an exception filter that catches everything with a filter that is bound to a specific type, the "Catch anything" filter should be declared first to allow the specific filter to correctly handle the bound type.
353+
326354
#### Inheritance
327355

328356
Typically, you'll create fully customized exception filters crafted to fulfill your application requirements. However, there might be use-cases when you would like to simply extend the built-in default **global exception filter**, and override the behavior based on certain factors.

content/faq/errors.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Probably the most common error message is about Nest not being able to resolve d
1010
Nest can't resolve dependencies of the <provider> (?). Please make sure that the argument <unknown_token> at index [<index>] is available in the <module> context.
1111
1212
Potential solutions:
13+
- Is <module> a valid NestJS module?
1314
- If <unknown_token> is a provider, is it part of the current <module>?
1415
- If <unknown_token> is exported from a separate @Module, is that module imported within <module>?
1516
@Module({
@@ -56,6 +57,7 @@ This likely happens when your project end up loading two Node modules of the pac
5657
Solutions:
5758
5859
- For **Yarn** Workspaces, use the [nohoist feature](https://classic.yarnpkg.com/blog/2018/02/15/nohoist) to prevent hoisting the package `@nestjs/core`.
60+
- For **pnpm** Workspaces, set `@nestjs/core` as a peerDependencies in your other module and `"dependenciesMeta": {{ '{' }}"other-module-name": {{ '{' }}"injected": true{{ '}}' }}` in the app package.json where the module is imported. see: [dependenciesmetainjected](https://pnpm.io/package_json#dependenciesmetainjected)
5961
6062
#### "Circular dependency" error
6163
@@ -82,3 +84,25 @@ Along with just manually verifying your dependencies are correct, as of Nest 8.1
8284
<figure><img src="/assets/injector_logs.png" /></figure>
8385
8486
In the above image, the string in yellow is the host class of the dependency being injected, the string in blue is the name of the injected dependency, or its injection token, and the string in purple is the module in which the dependency is being searched for. Using this, you can usually trace back the dependency resolution for what's happening and why you're getting dependency injection problems.
87+
88+
#### "File change detected" loops endlessly
89+
90+
Windows users who are using TypeScript version 4.9 and up may encounter this problem.
91+
This happens when you're trying to run your application in watch mode, e.g `npm run start:dev` and see an endless loop of the log messages:
92+
93+
```bash
94+
XX:XX:XX AM - File change detected. Starting incremental compilation...
95+
XX:XX:XX AM - Found 0 errors. Watching for file changes.
96+
```
97+
98+
When you're using the NestJS CLI to start your application in watch mode it is done by calling `tsc --watch`, and as of version 4.9 of TypeScript, a [new strategy](https://devblogs.microsoft.com/typescript/announcing-typescript-4-9/#file-watching-now-uses-file-system-events) for detecting file changes is used which is likely to be the cause of this problem.
99+
In order to fix this problem, you need to add a setting to your tsconfig.json file after the `"compilerOptions"` option as follows:
100+
101+
```bash
102+
"watchOptions": {
103+
"watchFile": "fixedPollingInterval"
104+
}
105+
```
106+
107+
This tells TypeScript to use the polling method for checking for file changes instead of file system events (the new default method), which can cause issues on some machines.
108+
You can read more about the `"watchFile"` option in [TypeScript documentation](https://www.typescriptlang.org/tsconfig#watch-watchDirectory).

content/faq/raw-body.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
One of the most common use-case for having access to the raw request body is performing webhook signature verifications. Usually to perform webhook signature validations the unserialized request body is required to calculate an HMAC hash.
44

5+
> warning **Warning** This feature can be used only if the built-in global body parser middleware is enabled, ie., you must not pass `bodyParser: false` when creating the app.
6+
57
#### Use with Express
68

79
First enable the option when creating your Nest Express application:
@@ -35,7 +37,7 @@ First enable the option when creating your Nest Fastify application:
3537
```typescript
3638
const app = await NestFactory.create<NestFastifyApplication>(
3739
AppModule,
38-
new FastifyAdapter()
40+
new FastifyAdapter(),
3941
{
4042
rawBody: true,
4143
}

content/faq/request-lifecycle.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Interceptors, for the most part, follow the same pattern as guards, with one cat
3434

3535
#### Pipes
3636

37-
Pipes follow the standard global to controller to route bound sequence, with the same first in first out in regards to the `@usePipes()` parameters. However, at a route parameter level, if you have multiple pipes running, they will run in the order of the last parameter with a pipe to the first. This also applies to the route level and controller level pipes. For example, if we have the following controller:
37+
Pipes follow the standard global to controller to route bound sequence, with the same first in first out in regards to the `@UsePipes()` parameters. However, at a route parameter level, if you have multiple pipes running, they will run in the order of the last parameter with a pipe to the first. This also applies to the route level and controller level pipes. For example, if we have the following controller:
3838

3939
```typescript
4040
@UsePipes(GeneralValidationPipe)

0 commit comments

Comments
 (0)