Skip to content

Commit

Permalink
Merge pull request #41 from nestjs/6.0.0
Browse files Browse the repository at this point in the history
6.0.0
  • Loading branch information
BrunnerLivio authored Mar 17, 2019
2 parents bdaad32 + 52e212b commit 1ff5ff0
Show file tree
Hide file tree
Showing 25 changed files with 657 additions and 642 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Import the Terminus module with the following options for a **database health ch
```ts

const getTerminusOptions = (
db: DatabaseHealthIndicator,
db: TypeOrmHealthIndicator,
): TerminusModuleOptions => ({
endpoints: [
{
Expand All @@ -62,8 +62,8 @@ const getTerminusOptions = (
// Make sure TypeOrmModule is available in the module context
TypeOrmModule.forRoot({ ... }),
TerminusModule.forRootAsync({
// Inject the DatabaseHealthIndicator provided by nestjs/terminus
inject: [DatabaseHealthIndicator],
// Inject the TypeOrmHealthIndicator provided by nestjs/terminus
inject: [TypeOrmHealthIndicator],
useFactory: db => getTerminusOptions(db),
}),
],
Expand Down
10 changes: 5 additions & 5 deletions e2e/health-checks/typeorm.health.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { INestApplication } from '@nestjs/common';
import { DatabaseHealthIndicator, TerminusModuleOptions } from '../../lib';
import { TypeOrmHealthIndicator, TerminusModuleOptions } from '../../lib';

import Axios from 'axios';
import { bootstrapModule } from '../helper/bootstrap-module';
Expand All @@ -9,7 +9,7 @@ describe('TypeOrm Database Health', () => {
let port: number;

const getTerminusOptions = (
db: DatabaseHealthIndicator,
db: TypeOrmHealthIndicator,
): TerminusModuleOptions => ({
endpoints: [
{
Expand All @@ -22,7 +22,7 @@ describe('TypeOrm Database Health', () => {
it('should check if the typeorm is available', async () => {
[app, port] = await bootstrapModule(
{
inject: [DatabaseHealthIndicator],
inject: [TypeOrmHealthIndicator],
useFactory: getTerminusOptions,
},
true,
Expand All @@ -40,8 +40,8 @@ describe('TypeOrm Database Health', () => {
it('should throw an error if runs into timeout error', async () => {
[app, port] = await bootstrapModule(
{
inject: [DatabaseHealthIndicator],
useFactory: (db: DatabaseHealthIndicator): TerminusModuleOptions => ({
inject: [TypeOrmHealthIndicator],
useFactory: (db: TypeOrmHealthIndicator): TerminusModuleOptions => ({
endpoints: [
{
url: '/health',
Expand Down
6 changes: 4 additions & 2 deletions e2e/helper/bootstrap-module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DynamicModule, INestApplication } from '@nestjs/common';
import { FastifyAdapter, NestFactory } from '@nestjs/core';
import { NestFactory } from '@nestjs/core';
import { FastifyAdapter } from '@nestjs/platform-fastify';
import { ExpressAdapter } from '@nestjs/platform-express';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MongooseModule } from '@nestjs/mongoose';
import * as portfinder from 'portfinder';
Expand Down Expand Up @@ -64,7 +66,7 @@ export async function bootstrapModule(
): Promise<[INestApplication, number]> {
const app = await NestFactory.create(
ApplicationModule.forRoot(options, useDb, useMongoose),
useFastify ? new FastifyAdapter() : null,
useFastify ? new FastifyAdapter() : new ExpressAdapter(),
);

if (tcpPort) {
Expand Down
7 changes: 4 additions & 3 deletions e2e/terminus.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { INestApplication, DynamicModule } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { TerminusModule } from '../lib/terminus.module';
import { TerminusLibProvider } from '../lib/terminus-lib.provider';
import { ApplicationReferenceHost } from '@nestjs/core';
import { HttpAdapterHost } from '@nestjs/core';
import {
TerminusOptionsFactory,
TerminusModuleOptions,
Expand Down Expand Up @@ -40,8 +40,9 @@ describe('Terminus', () => {
.compile();

app = module.createNestApplication();
httpServer = app.get<ApplicationReferenceHost>(ApplicationReferenceHost)
.applicationRef.httpServer;
httpServer = app
.get<HttpAdapterHost>(HttpAdapterHost)
.httpAdapter.getHttpServer();
await app.init();
return app;
}
Expand Down
26 changes: 0 additions & 26 deletions lib/health-indicators/abstract/health-indicator.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { ConnectionNotFoundError, TimeoutError } from '../../errors';
import { HealthIndicatorResult } from '../../interfaces';
import { TimeoutError as PromiseTimeoutError } from '../../utils';
import { DatabasePingCheckSettings } from '../databse-ping-check-settings.interface';
import { HealthIndicator } from './health-indicator';
import { HealthIndicator } from '../health-indicator';

/**
* Abstract AbstractDatabaseHealthIndicator
* Abstract DatabaseHealthIndicator
*/
export abstract class AbstractDatabaseHealthIndicator extends HealthIndicator {
export abstract class DatabaseHealthIndicator extends HealthIndicator {
/**
* Constructor with the connection
* @param connection The underlying Connection instance from TypeOrm or Mongoose connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { Injectable, Optional } from '@nestjs/common';
import { InjectConnection } from '@nestjs/mongoose';
import { Connection } from 'mongoose';
import { promiseTimeout } from '../../utils';
import { AbstractDatabaseHealthIndicator } from '../abstract/abstract-database-health-indicator';
import { DatabaseHealthIndicator } from './database-health-indicator';

@Injectable()
export class MongooseHealthIndicator extends AbstractDatabaseHealthIndicator {
export class MongooseHealthIndicator extends DatabaseHealthIndicator {
/**
* Initializes the typeorm indicator
*
* @param connection The typeorm connection of the application context
*
* @public
*/
constructor(@Optional() @InjectConnection() readonly connection: Connection) {
super(connection);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Injectable, Optional } from '@nestjs/common';
import { Connection } from 'typeorm';
import { promiseTimeout } from '../../utils';
import { AbstractDatabaseHealthIndicator } from '../abstract/abstract-database-health-indicator';
import { DatabaseHealthIndicator } from './database-health-indicator';

/**
* The TypeOrmeHealthIndicator contains health indicators
* which are used for health checks related to typeorm
*/
@Injectable()
export class DatabaseHealthIndicator extends AbstractDatabaseHealthIndicator {
export class TypeOrmHealthIndicator extends DatabaseHealthIndicator {
/**
* Initializes the typeorm indicator
* @param connection The typeorm connection of the application context
*
* @public
*/
constructor(@Optional() readonly connection: Connection) {
super(connection);
Expand Down
6 changes: 3 additions & 3 deletions lib/health-indicators/dns/dns.health.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, HttpService } from '@nestjs/common';
import { HealthIndicatorResult } from '../../interfaces';
import { AxiosResponse, AxiosRequestConfig, AxiosError } from 'axios';
import { HealthIndicator } from '../abstract/health-indicator';
import { HealthIndicator } from '../health-indicator';
import { HealthCheckError } from '@godaddy/terminus';

/**
Expand All @@ -14,6 +14,8 @@ export class DNSHealthIndicator extends HealthIndicator {
/**
* Initializes the health indicator
* @param httpService The HttpService provided by Nest
*
* @public
*/
constructor(private readonly httpService: HttpService) {
super();
Expand Down Expand Up @@ -69,9 +71,7 @@ export class DNSHealthIndicator extends HealthIndicator {
* @throws {HealthCheckError} In case the health indicator failed
*
* @example
* ```TypeScript
* dnsHealthIndicator.pingCheck('google', 'https://google.com', { timeout: 800 })
* ```
*/
async pingCheck(
key: string,
Expand Down
38 changes: 38 additions & 0 deletions lib/health-indicators/health-indicator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { HealthIndicatorResult } from '..';

/**
* Represents an abstract health indicator
* with common functionalities
*
* @public
*/
export abstract class HealthIndicator {
/**
* Generates the health indicator result object
* @param key The key which will be used as key for the result object
* @param isHealthy Whether the health indicator is healthy
* @param data Additional data which will get appended to the result object
*
* @example
* class MyHealthIndicator extends HealthIndicator {
* public check(key: string) {
* // Replace with the actual check
* const isHealthy = true;
* // Returns { [key]: { status: 'up', message: 'Up and running' } }
* return super.getStatus(key, isHealthy, { message: 'Up and running' });
* }
* }
*/
protected getStatus(
key: string,
isHealthy: boolean,
data?: { [key: string]: unknown },
): HealthIndicatorResult {
return {
[key]: {
status: isHealthy ? 'up' : 'down',
...data,
},
};
}
}
5 changes: 3 additions & 2 deletions lib/health-indicators/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './typeorm/typeorm.health';
export * from './dns/dns.health';
export * from './mongoose/mongoose.health';
export * from './database/mongoose.health';
export * from './database/typeorm.health';
export * from './microservice/microservice.health';
export * from './health-indicator';
6 changes: 3 additions & 3 deletions lib/health-indicators/microservice/microservice.health.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { HealthIndicatorResult } from '../../interfaces';
import { HealthIndicator } from '../abstract/health-indicator';
import { HealthIndicator } from '../health-indicator';
import { HealthCheckError } from '@godaddy/terminus';
import { ClientProxyFactory, ClientOptions } from '@nestjs/microservices';
import {
Expand All @@ -16,6 +16,8 @@ export type MicroserviceHealthIndicatorOptions = ClientOptions & {
/**
* The MicroserviceHealthIndicator is a health indicators
* which is used for health checks related to microservices
*
* @public
*/
@Injectable()
export class MicroserviceHealthIndicator extends HealthIndicator {
Expand Down Expand Up @@ -69,12 +71,10 @@ export class MicroserviceHealthIndicator extends HealthIndicator {
* @throws {HealthCheckError} If the microservice is not reachable
*
* @example
* ```TypeScript
* microservice.pingCheck('tcp', {
* transport: Transport.TCP,
* options: { host: 'localhost', port: 3001 },
* })
* ```
*/
async pingCheck(
key: string,
Expand Down
10 changes: 5 additions & 5 deletions lib/terminus-bootstrap.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { TerminusBootstrapService } from './terminus-bootstrap.service';
import { ApplicationReferenceHost } from '@nestjs/core';
import { HttpAdapterHost } from '@nestjs/core';
import { TerminusEndpoint, TerminusModuleOptions } from './interfaces';
import { HealthCheckError } from '@godaddy/terminus';

const httpServer = jest.fn();

const refhostMock = {
applicationRef: {
httpAdapter: {
getHttpServer: jest.fn().mockImplementation(() => httpServer),
},
};
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('TerminusBootstrapService', () => {
const bootstrapService = new TerminusBootstrapService(
options,
terminus,
refhostMock as ApplicationReferenceHost,
refhostMock as HttpAdapterHost<any>,
);

expect(terminus).not.toHaveBeenCalled();
Expand All @@ -61,7 +61,7 @@ describe('TerminusBootstrapService', () => {
const bootstrapService = new TerminusBootstrapService(
{ ...options, logger },
terminus,
refhostMock as ApplicationReferenceHost,
refhostMock as HttpAdapterHost<any>,
);

bootstrapService.onApplicationBootstrap();
Expand All @@ -79,7 +79,7 @@ describe('TerminusBootstrapService', () => {
bootstrapService = new TerminusBootstrapService(
options,
terminus,
refhostMock as ApplicationReferenceHost,
refhostMock as HttpAdapterHost<any>,
);
});

Expand Down
9 changes: 5 additions & 4 deletions lib/terminus-bootstrap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
HealthIndicatorFunction,
TerminusEndpoint,
} from './interfaces';
import { ApplicationReferenceHost } from '@nestjs/core';
import { HttpAdapterHost } from '@nestjs/core';
import { Server } from 'http';
import { HealthCheckError, Terminus, HealthCheckMap } from '@godaddy/terminus';

Expand Down Expand Up @@ -38,8 +38,9 @@ export class TerminusBootstrapService implements OnApplicationBootstrap {
constructor(
@Inject(TERMINUS_MODULE_OPTIONS)
private readonly options: TerminusModuleOptions,
@Inject(TERMINUS_LIB) private readonly terminus: Terminus,
private readonly refHost: ApplicationReferenceHost,
@Inject(TERMINUS_LIB)
private readonly terminus: Terminus,
private readonly refHost: HttpAdapterHost<any>,
) {}

/**
Expand Down Expand Up @@ -157,7 +158,7 @@ export class TerminusBootstrapService implements OnApplicationBootstrap {
*/
public onApplicationBootstrap() {
// httpServer for express, instance.server for fastify
this.httpServer = this.refHost.applicationRef.getHttpServer();
this.httpServer = this.refHost.httpAdapter.getHttpServer();
this.bootstrapTerminus();
}
}
Loading

0 comments on commit 1ff5ff0

Please sign in to comment.