Skip to content

Commit a1736f2

Browse files
Merge branch 'BrunnerLivio-feature/terminus-v7'
2 parents 32ff5af + 99f820f commit a1736f2

File tree

2 files changed

+283
-167
lines changed

2 files changed

+283
-167
lines changed

content/migration.md

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,164 @@ In order to migrate your existing application, simply rename all the `type-graph
7171
- use `Type` (imported from `@nestjs/common`) instead of `ClassType` (imported from `type-graphql`)
7272
- move methods that require `@Args()` from object types (classes annotated with `@ObjectType()` decorator) under resolver classes (and use `@ResolveField()` decorator instead of `@Field()`)
7373

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.
81+
82+
```typescript
83+
@@filename()
84+
// Before
85+
@Injectable()
86+
export class TerminusOptionsService implements TerminusOptionsFactory {
87+
constructor(
88+
private dns: DNSHealthIndicator,
89+
) {}
90+
91+
createTerminusOptions(): TerminusModuleOptions {
92+
const healthEndpoint: TerminusEndpoint = {
93+
url: '/health',
94+
healthIndicators: [
95+
async () => this.dns.pingCheck('google', 'https://google.com'),
96+
],
97+
};
98+
return {
99+
endpoints: [healthEndpoint],
100+
};
101+
}
102+
}
103+
104+
@Module({
105+
imports: [
106+
TerminusModule.forRootAsync({
107+
useClass: TerminusOptionsService
108+
})
109+
]
110+
})
111+
export class AppModule { }
112+
113+
// After
114+
@Controller('health')
115+
export class HealthController {
116+
constructor(
117+
private health: HealthCheckService,
118+
private dns: DNSHealthIndicator,
119+
) { }
120+
121+
@Get()
122+
@HealthCheck()
123+
healthCheck() {
124+
return this.health.check([
125+
async () => this.dns.pingCheck('google', 'https://google.com'),
126+
]);
127+
}
128+
}
129+
130+
@Module({
131+
imports: [
132+
TerminusModule
133+
]
134+
})
135+
export class AppModule { }
136+
137+
@@switch
138+
139+
// Before
140+
@Injectable()
141+
@Dependencies(DNSHealthIndicator)
142+
export class TerminusOptionsService {
143+
constructor(
144+
private dns,
145+
) {}
146+
147+
createTerminusOptions() {
148+
const healthEndpoint = {
149+
url: '/health',
150+
healthIndicators: [
151+
async () => this.dns.pingCheck('google', 'https://google.com'),
152+
],
153+
};
154+
return {
155+
endpoints: [healthEndpoint],
156+
};
157+
}
158+
}
159+
160+
@Module({
161+
imports: [
162+
TerminusModule.forRootAsync({
163+
useClass: TerminusOptionsService
164+
})
165+
]
166+
})
167+
export class AppModule { }
168+
169+
// After
170+
@Controller('/health')
171+
@Dependencies(HealthCheckService, DNSHealthIndicator)
172+
export class HealthController {
173+
constructor(
174+
private health,
175+
private dns,
176+
) { }
177+
178+
@Get('/')
179+
@HealthCheck()
180+
healthCheck() {
181+
return this.health.check([
182+
async () => this.dns.pingCheck('google', 'https://google.com'),
183+
])
184+
}
185+
}
186+
187+
@Module({
188+
controllers: [
189+
HealthController
190+
],
191+
imports: [
192+
TerminusModule
193+
]
194+
})
195+
export class AppModule { }
196+
```
197+
198+
> 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`.
217+
218+
```typescript
219+
@@filename(custom.health)
220+
// Before
221+
import { HealthCheckError } from '@godaddy/terminus';
222+
// After
223+
import { HealthCheckError } from '@nestjs/terminus';
224+
```
225+
226+
Once you have fully migrated, make sure you uninstall `@godaddy/terminus`.
227+
228+
```bash
229+
npm uninstall --save @godaddy/terminus
230+
```
231+
74232
#### HTTP exceptions body
75233
76234
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
127285
128286
```typescript
129287
new ValidationPipe({
130-
exceptionFactory: errors => new BadRequestException(errors),
288+
exceptionFactory: (errors) => new BadRequestException(errors),
131289
});
132290
```
133291

0 commit comments

Comments
 (0)