Skip to content

Commit 44971f1

Browse files
committed
chore: add typesecurity for grpc health indicator
1 parent c6d8f7c commit 44971f1

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

lib/health-indicator/microservice/grpc.health.spec.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { GRPCHealthIndicator } from './grpc.health';
22
import { checkPackages } from '../../utils/checkPackage.util';
3-
import { Transport } from '@nestjs/microservices';
3+
import { GrpcOptions, Transport } from '@nestjs/microservices';
44
import { UnhealthyResponseCodeError, TimeoutError } from '../../errors';
55
import { HealthCheckError } from '../../health-check/health-check.error';
66
jest.mock('../../utils/checkPackage.util');
@@ -41,21 +41,21 @@ describe('GRPCHealthIndicator', () => {
4141
});
4242
describe('checkService', () => {
4343
it('should return a healthy result', async () => {
44-
const result = await grpc.checkService('grpc', 'test');
44+
const result = await grpc.checkService<GrpcOptions>('grpc', 'test');
4545
expect(result).toEqual({
4646
grpc: { servingStatus: 'SERVING', status: 'up', statusCode: 1 },
4747
});
4848
});
4949

5050
it('should correctly call the ClientProxyFactory with default', async () => {
51-
await grpc.checkService('grpc', 'test');
51+
await grpc.checkService<GrpcOptions>('grpc', 'test');
5252
expect(clientProxyFactoryMock.create.mock.calls[0][0]).toEqual({
5353
options: { package: 'grpc.health.v1', protoPath: expect.anything() },
5454
transport: Transport.GRPC,
5555
});
5656
});
5757
it('should correctly all the ClientProxyFactory with custom options', async () => {
58-
await grpc.checkService('grpc', 'test', {
58+
await grpc.checkService<GrpcOptions>('grpc', 'test', {
5959
protoPath: 'test.proto',
6060
timeout: 100,
6161
package: 'grpc.health.v2',
@@ -70,14 +70,14 @@ describe('GRPCHealthIndicator', () => {
7070
toPromise: (): any => Promise.resolve({ status: 0 }),
7171
}));
7272
try {
73-
await grpc.checkService('grpc', 'test');
73+
await grpc.checkService<GrpcOptions>('grpc', 'test');
7474
} catch (err) {
7575
expect(err instanceof UnhealthyResponseCodeError).toBeTruthy();
7676
}
7777
});
7878
it('should throw an error when the timeout runs out', async () => {
7979
try {
80-
await grpc.checkService('grpc', 'test', { timeout: 0 });
80+
await grpc.checkService<GrpcOptions>('grpc', 'test', { timeout: 0 });
8181
} catch (err) {
8282
expect(err instanceof TimeoutError).toBeTruthy();
8383
}
@@ -87,12 +87,16 @@ describe('GRPCHealthIndicator', () => {
8787
.fn()
8888
.mockImplementation((): any => ({ status: 1 }));
8989

90-
await grpc.checkService('grpc', 'test', { healthServiceCheck });
90+
await grpc.checkService<GrpcOptions>('grpc', 'test', {
91+
healthServiceCheck,
92+
});
9193

9294
expect(healthServiceCheck.mock.calls.length).toBe(1);
9395
});
9496
it('should use the custom healthServiceName', async () => {
95-
await grpc.checkService('grpc', 'test', { healthServiceName: 'health2' });
97+
await grpc.checkService<GrpcOptions>('grpc', 'test', {
98+
healthServiceName: 'health2',
99+
});
96100
expect(grpcClientMock.getService.mock.calls[0][0]).toBe('health2');
97101
});
98102
it('should throw TypeError further in client.getService', async () => {
@@ -101,7 +105,7 @@ describe('GRPCHealthIndicator', () => {
101105
throw error;
102106
});
103107
try {
104-
await grpc.checkService('grpc', 'test');
108+
await grpc.checkService<GrpcOptions>('grpc', 'test');
105109
} catch (err) {
106110
expect(err).toEqual(error);
107111
}
@@ -112,14 +116,14 @@ describe('GRPCHealthIndicator', () => {
112116
throw error;
113117
});
114118
try {
115-
await grpc.checkService('grpc', 'test');
119+
await grpc.checkService<GrpcOptions>('grpc', 'test');
116120
} catch (err) {
117121
expect(err instanceof HealthCheckError).toBeTruthy();
118122
}
119123
});
120124
it('should throw HealthCheckError if the grpc check function fails', async () => {
121125
try {
122-
await grpc.checkService('grpc', 'test', {
126+
await grpc.checkService<GrpcOptions>('grpc', 'test', {
123127
healthServiceCheck: () => {
124128
throw new Error('test');
125129
},

lib/health-indicator/microservice/grpc.health.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,17 @@ export class GRPCHealthIndicator extends HealthIndicator {
148148
* @param {CheckGRPCOptions} [options] Configuration for the request
149149
*
150150
* @example
151-
* grpc.checkService('hero_service', 'hero.health.v1')
151+
* grpc.checkService<GrpcOptions>('hero_service', 'hero.health.v1')
152152
*
153153
* @example
154154
* // Change the timeout
155-
* grpc.checkService('hero_service', 'hero.health.v1', { timeout: 300 })
155+
* grpc.checkService<GrpcOptions>('hero_service', 'hero.health.v1', { timeout: 300 })
156156
*
157157
* @example
158158
* // You can customize the health check
159159
* // by giving these options. Nonetheless it is still seen
160160
* // as best practice to implement the recommended GRPC specs
161-
* grpc.checkService('hero_service', 'hero.health.v1', {
161+
* grpc.checkService<GrpcOptions>('hero_service', 'hero.health.v1', {
162162
* timeout: 500,
163163
* package: 'grpc.health.v2',
164164
* protoPath: join(__dirname, './protos/my-custom-health.v1'),

0 commit comments

Comments
 (0)