You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/components.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -203,3 +203,11 @@ This is how our directory structure should look now:
203
203
<divclass="item">main.ts</div>
204
204
</div>
205
205
</div>
206
+
207
+
#### Manual instantiation
208
+
209
+
Thus far, we've discussed how Nest automatically handles most of the details of resolving dependencies. In certain circumstances, you may need to step outside of the built-in Dependency Injection system and manually retrieve or instantiate providers. We briefly discuss two such topics below.
210
+
211
+
To get existing instances, or instantiate providers dynamically, you can use [Module reference](https://docs.nestjs.com/fundamentals/module-ref).
212
+
213
+
To get providers within the `bootstrap()` function (for example for standalone applications without controllers, or to utilize a configuration service during bootstrapping) see [Standalone applications](https://docs.nestjs.com/standalone-applications).
Copy file name to clipboardExpand all lines: content/fundamentals/lifecycle-events.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ The following diagram depicts the sequence of key application lifecycle events,
15
15
Lifecycle events happen during application bootstrapping and shutdown. Nest calls registered lifecycle hook methods on `modules`, `injectables` and `controllers` at each of the following lifecycle events (**shutdown hooks** need to be enabled first, as described [below](https://docs.nestjs.com/fundamentals/lifecycle-events#application-shutdown)). As shown in the diagram above, Nest also calls the appropriate underlying methods to begin listening for connections, and to stop listening for connections.
|`onModuleInit()`| Called once the host module's dependencies have been resolved. |
20
20
|`onApplicationBootstrap()`| Called once all modules have been initialized, but before listening for connections. |
21
21
|`onModuleDestroy()`| Called after a termination signal (e.g., `SIGTERM`) has been received. |
@@ -66,6 +66,8 @@ async onModuleInit() {
66
66
67
67
The `beforeApplicationShutdown()` and `onApplicationShutdown()` hooks are called in the **terminating** phase (in response to system signals such as `SIGTERM`). This feature is often used with [Kubernetes](https://kubernetes.io/), [Heroku](https://www.heroku.com/) or similar services.
68
68
69
+
> warning **warning** Due to inherent platform limitations, NestJS has limited support for application shutdown hooks on Windows. You can expect `SIGINT` to work, as well as `SIGBREAK` and to some extent `SIGHUP` - [read more](https://nodejs.org/api/process.html#process_signal_events). However `SIGTERM` will never work on Windows because killing a process in the task manager is unconditional, "i.e., there's no way for an application to detect or prevent it". Here's some [relevant documentation](http://docs.libuv.org/en/v1.x/signal.html) from libuv to learn more about how `SIGINT`, `SIGBREAK` and others are handled on Windows. Also, see Node.js documentation of [Process Signal Events](https://nodejs.org/api/process.html#process_signal_events)
70
+
69
71
To use these hooks you must activate a listener which listens to shutdown signals.
70
72
71
73
```typescript
@@ -81,6 +83,8 @@ async function bootstrap() {
81
83
bootstrap();
82
84
```
83
85
86
+
> info **Info**`enableShutdownHooks` consumes memory by starting listeners. In cases where you are running multiple Nest apps in a single Node process (e.g., when running parallel tests with Jest), Node may complain about excessive listener processes. For this reason, `enableShutdownHooks` is not enabled by default. Be aware of this condition when you are running multiple instances in a single Node process.
87
+
84
88
When the application receives a termination signal it will call any registered `beforeApplicationShutdown()`, then `onApplicationShutdown()` methods (in the sequence described above) with the corresponding signal as the first parameter. If a registered function awaits an asynchronous call (returns a promise), Nest will not continue in the sequence until the promise is resolved or rejected.
Copy file name to clipboardExpand all lines: content/graphql/enums.md
+13-4Lines changed: 13 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,13 +64,22 @@ export enum AllowedColor {
64
64
}
65
65
```
66
66
67
-
Sometimes a backend forces a different value for an enum internally than in the public API. In this example the API contains `RED`, however in resolvers we may use `#f00` instead (read more [here](https://www.apollographql.com/docs/apollo-server/schema/scalars-enums/#internal-values)). To accomplish this, declare a resolver class for the `AllowedColor` enum:
67
+
Sometimes a backend forces a different value for an enum internally than in the public API. In this example the API contains `RED`, however in resolvers we may use `#f00` instead (read more [here](https://www.apollographql.com/docs/apollo-server/schema/scalars-enums/#internal-values)). To accomplish this, declare a resolver object for the `AllowedColor` enum:
Copy file name to clipboardExpand all lines: content/migration.md
+159-1Lines changed: 159 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,164 @@ In order to migrate your existing application, simply rename all the `type-graph
71
71
- use `Type` (imported from `@nestjs/common`) instead of `ClassType` (imported from `type-graphql`)
72
72
- move methods that require `@Args()` from object types (classes annotated with `@ObjectType()` decorator) under resolver classes (and use `@ResolveField()` decorator instead of `@Field()`)
73
73
74
+
#### Terminus
75
+
76
+
In the version 7 major release of `@nestjs/terminus`, a new simplified API has been introduced
77
+
to run health checks. The previously required peer dependency `@godaddy/terminus` has been removed, which allows us to integrate our health checks automatically into Swagger! Read more about the removal of `@godaddy/terminus`[here](https://github.com/nestjs/terminus/issues/340).
78
+
79
+
For most users, the biggest change will be the removal of the `TerminusModule.forRootAsync` function. With the next major version, this function will be completely removed.
80
+
To migrate to the new API, you will need to create a new controller, which will handle your health checks.
> warning **Warning** If you have set a [Global Prefix](faq#global-prefix) in your Nest application and you have not used the `useGlobalPrefix` Terminus option, the URL of your health check will change. Make sure to update the reference to that URL, or use the legacy Terminus API until [nestjs/nest#963](https://github.com/nestjs/nest/issues/963) is fixed.
199
+
200
+
If you are forced to use the legacy API, you can also disable deprecation messages for the time being.
201
+
202
+
```typescript
203
+
TerminusModule.forRootAsync({
204
+
useFactory: () => ({
205
+
disableDeprecationWarnings: true,
206
+
endpoints: [
207
+
// ...
208
+
]
209
+
})
210
+
}
211
+
```
212
+
213
+
You should enable shutdown hooks in your `main.ts` file. The Terminus integration will listen on POSIX signals such as SIGTERM (see the [Application shutdown chapter](fundamentals/lifecycle-events#application-shutdown) for more information). When enabled, the health check route(s) will automatically respond with a Service Unavailable (503) HTTP error response when the server is shutting down.
214
+
215
+
With the removal of `@godaddy/terminus`, you will need to update your `import` statements
216
+
to use `@nestjs/terminus` instead. Most notable is the import of the `HealthCheckError`.
Once you have fully migrated, make sure you uninstall `@godaddy/terminus`.
227
+
228
+
```bash
229
+
npmuninstall--save @godaddy/terminus
230
+
```
231
+
74
232
#### HTTP exceptions body
75
233
76
234
Previously, the generated response bodies for the `HttpException` class and other exceptions derived from it (e.g., `BadRequestException` or `NotFoundException`) were inconsistent. In the latest major release, these exception responses will follow the same structure.
@@ -127,7 +285,7 @@ If you prefer the previous approach, you can restore it by setting the `exceptio
Copy file name to clipboardExpand all lines: content/pipes.md
+1-93Lines changed: 1 addition & 93 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -445,97 +445,5 @@ We leave the implementation of this pipe to the reader, but note that like all o
445
445
446
446
#### The built-in ValidationPipe
447
447
448
-
Fortunately, you don't have to build these pipes on your own since the `ValidationPipe`and the`ParseIntPipe`are provided by Nest out-of-the-box. (Keep in mind that `ValidationPipe`requires both `class-validator` and `class-transformer` packages to be installed).
448
+
Fortunately, you don't have to build these pipes on your own since the `ValidationPipe`(along with`ParseIntPipe`, `ParseBoolPipe`, `ParseArrayPipe` and `ParseUUIDPipe`) are provided by Nest out-of-the-box. The built-in `ValidationPipe`offers more options than in the sample we built in this chapter, which has been kept basic for the sake of illustrating the basic mechanics of a pipe. You can find full details, along with lots of examples [here](/techniques/validation).
449
449
450
-
The built-in `ValidationPipe` offers more options than in the sample we built in this chapter, which has been kept basic for the sake of illustrating the basic mechanics of a pipe. You can find lots of examples [here](/techniques/validation).
451
-
452
-
One such option is `transform`. Recall the earlier discussion about deserialized body objects being vanilla JavaScript objects (i.e., not having our DTO type). So far, we've used the pipe to validate our payload. You may recall that in the process, we used `class-transform` to temporarily convert our plain object into a typed object so that we could do the validation. The built-in ValidationPipe can also, optionally, return this converted object. We enable this behavior by passing in a configuration object to the pipe. For this option, pass a config object with the field `transform` with a value `true` as shown below:
453
-
454
-
```typescript
455
-
@@filename(cats.controller)
456
-
@Post()
457
-
@UsePipes(newValidationPipe({ transform: true }))
458
-
asynccreate(@Body() createCatDto: CreateCatDto) {
459
-
this.catsService.create(createCatDto);
460
-
}
461
-
```
462
-
463
-
> info **Hint** The `ValidationPipe` is imported from the `@nestjs/common` package.
464
-
465
-
Because this pipe is based on the `class-validator` and `class-transformer` libraries, there are many additional options available. Like the `transform` option above, you configure these settings via a configuration object passed to the pipe. Following are the built-in options:
In addition to these, all `class-validator` options (inherited from the `ValidatorOptions` interface) are available:
476
-
477
-
<table>
478
-
<tr>
479
-
<th>Option</th>
480
-
<th>Type</th>
481
-
<th>Description</th>
482
-
</tr>
483
-
<tr>
484
-
<td><code>skipMissingProperties</code></td>
485
-
<td><code>boolean</code></td>
486
-
<td>If set to true, validator will skip validation of all properties that are missing in the validating object.</td>
487
-
</tr>
488
-
<tr>
489
-
<td><code>whitelist</code></td>
490
-
<td><code>boolean</code></td>
491
-
<td>If set to true, validator will strip validated (returned) object of any properties that do not use any validation decorators.</td>
492
-
</tr>
493
-
<tr>
494
-
<td><code>forbidNonWhitelisted</code></td>
495
-
<td><code>boolean</code></td>
496
-
<td>If set to true, instead of stripping non-whitelisted properties validator will throw an exception.</td>
497
-
</tr>
498
-
<tr>
499
-
<td><code>forbidUnknownValues</code></td>
500
-
<td><code>boolean</code></td>
501
-
<td>If set to true, attempts to validate unknown objects fail immediately.</td>
502
-
</tr>
503
-
<tr>
504
-
<td><code>disableErrorMessages</code></td>
505
-
<td><code>boolean</code></td>
506
-
<td>If set to true, validation errors will not be returned to the client.</td>
507
-
</tr>
508
-
<tr>
509
-
<td><code>errorHttpStatusCode</code></td>
510
-
<td><code>number</code></td>
511
-
<td>This setting allows you to specify which exception type will be used in case of an error. By default it throws <code>BadRequestException</code>.</td>
512
-
</tr>
513
-
<tr>
514
-
<td><code>exceptionFactory</code></td>
515
-
<td><code>Function</code></td>
516
-
<td>Takes an array of the validation errors and returns an exception object to be thrown.</td>
517
-
</tr>
518
-
<tr>
519
-
<td><code>groups</code></td>
520
-
<td><code>string[]</code></td>
521
-
<td>Groups to be used during validation of the object.</td>
522
-
</tr>
523
-
<tr>
524
-
<td><code>dismissDefaultMessages</code></td>
525
-
<td><code>boolean</code></td>
526
-
<td>If set to true, the validation will not use default messages. Error message always will be <code>undefined</code> if
527
-
its not explicitly set.</td>
528
-
</tr>
529
-
<tr>
530
-
<td><code>validationError.target</code></td>
531
-
<td><code>boolean</code></td>
532
-
<td>Indicates if target should be exposed in <code>ValidationError</code></td>
533
-
</tr>
534
-
<tr>
535
-
<td><code>validationError.value</code></td>
536
-
<td><code>boolean</code></td>
537
-
<td>Indicates if validated value should be exposed in <code>ValidationError</code>.</td>
538
-
</tr>
539
-
</table>
540
-
541
-
> info **Notice** Find more information about the `class-validator` package in its [repository](https://github.com/typestack/class-validator).
0 commit comments