Skip to content

Commit

Permalink
refactor(microservices): a few minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Dec 18, 2023
1 parent 0a6853c commit 8d08184
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { RuntimeException } from '@nestjs/core/errors/exceptions/runtime.exception';

export class InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException extends RuntimeException {
export class InvalidGrpcPackageDefinitionMissingPackageDefinitionException extends RuntimeException {
constructor() {
super('protoPath or packageDefinition must be defined');
super(
`Invalid gRPC configuration. protoPath or packageDefinition must be defined.`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RuntimeException } from '@nestjs/core/errors/exceptions/runtime.excepti
export class InvalidGrpcPackageDefinitionMutexException extends RuntimeException {
constructor() {
super(
'Both protoPath and packageDefinition were provided, but only one can be defined',
`Invalid gRPC configuration. Both protoPath and packageDefinition cannot be defined at the same time.`,
);
}
}
11 changes: 5 additions & 6 deletions packages/microservices/helpers/grpc-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GrpcOptions } from '../interfaces';
import { InvalidGrpcPackageDefinitionMissingPackageDefinitionException } from '../errors/invalid-grpc-package-definition-missing-package-definition.exception';
import { InvalidGrpcPackageDefinitionMutexException } from '../errors/invalid-grpc-package-definition-mutex.exception';
import { InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException } from '../errors/invalid-grpc-package-definition-missing-package-definition.exception';
import { GrpcOptions } from '../interfaces';

export function getGrpcPackageDefinition(
options: GrpcOptions['options'],
Expand All @@ -9,12 +9,11 @@ export function getGrpcPackageDefinition(
const file = options['protoPath'];
const packageDefinition = options['packageDefinition'];

if ([file, packageDefinition].every(x => x != undefined)) {
if (file && packageDefinition) {
throw new InvalidGrpcPackageDefinitionMutexException();
}

if ([file, packageDefinition].every(x => x == undefined)) {
throw new InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException();
if (!file && !packageDefinition) {
throw new InvalidGrpcPackageDefinitionMissingPackageDefinitionException();
}

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/microservices/server/server-grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import { Transport } from '../enums';
import { InvalidGrpcPackageException } from '../errors/invalid-grpc-package.exception';
import { InvalidProtoDefinitionException } from '../errors/invalid-proto-definition.exception';
import { ChannelOptions } from '../external/grpc-options.interface';
import { getGrpcPackageDefinition } from '../helpers';
import { CustomTransportStrategy, MessageHandler } from '../interfaces';
import { GrpcOptions } from '../interfaces/microservice-configuration.interface';
import { Server } from './server';
import { getGrpcPackageDefinition } from '../helpers';

let grpcPackage: any = {};
let grpcProtoLoaderPackage: any = {};
Expand Down
92 changes: 50 additions & 42 deletions packages/microservices/test/helpers/grpc-helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,64 @@
import { expect } from 'chai';
import { getGrpcPackageDefinition } from '../../helpers/grpc-helpers';
import { InvalidGrpcPackageDefinitionMissingPackageDefinitionException } from '../../errors/invalid-grpc-package-definition-missing-package-definition.exception';
import { InvalidGrpcPackageDefinitionMutexException } from '../../errors/invalid-grpc-package-definition-mutex.exception';
import { InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException } from '../../errors/invalid-grpc-package-definition-missing-package-definition.exception';
import { getGrpcPackageDefinition } from '../../helpers/grpc-helpers';

const grpcProtoLoaderPackage = { loadSync: (a, b) => 'withLoader' };

describe('getGrpcPackageDefinition', () => {
it('missing both protoPath and packageDefinition', () => {
expect(() =>
getGrpcPackageDefinition(
{
package: 'somePackage',
},
grpcProtoLoaderPackage,
),
).to.throw(InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException);
describe('when missing both protoPath and packageDefinition', () => {
it('should throw InvalidGrpcPackageDefinitionMissingPackageDefinitionException', () => {
expect(() =>
getGrpcPackageDefinition(
{
package: 'somePackage',
},
grpcProtoLoaderPackage,
),
).to.throw(InvalidGrpcPackageDefinitionMissingPackageDefinitionException);
});
});

it('got both protoPath and packageDefinition', () => {
expect(() =>
getGrpcPackageDefinition(
{
package: 'somePackage',
protoPath: 'some/path',
packageDefinition: {},
},
grpcProtoLoaderPackage,
),
).to.throw(InvalidGrpcPackageDefinitionMutexException);
describe('when both protoPath and packageDefinition are defined', () => {
it('should throw InvalidGrpcPackageDefinitionMutexException', () => {
expect(() =>
getGrpcPackageDefinition(
{
package: 'somePackage',
protoPath: 'some/path',
packageDefinition: {},
},
grpcProtoLoaderPackage,
),
).to.throw(InvalidGrpcPackageDefinitionMutexException);
});
});

it('success with protoPath', () => {
expect(() =>
getGrpcPackageDefinition(
{
package: 'somePackage',
protoPath: 'some/path',
},
grpcProtoLoaderPackage,
),
).to.not.throw(Error);
describe('when only protoPath is defined', () => {
it('should not throw any exception', () => {
expect(() =>
getGrpcPackageDefinition(
{
package: 'somePackage',
protoPath: 'some/path',
},
grpcProtoLoaderPackage,
),
).to.not.throw(Error);
});
});

it('success with packageDef', () => {
expect(() =>
getGrpcPackageDefinition(
{
package: 'somePackage',
packageDefinition: {},
},
grpcProtoLoaderPackage,
),
).to.not.throw(Error);
describe('when only packageDefinition is defined', () => {
it('should not throw any exception', () => {
expect(() =>
getGrpcPackageDefinition(
{
package: 'somePackage',
packageDefinition: {},
},
grpcProtoLoaderPackage,
),
).to.not.throw(Error);
});
});
});
13 changes: 3 additions & 10 deletions packages/microservices/test/server/server-grpc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { Logger } from '@nestjs/common';
import { expect } from 'chai';
import { join } from 'path';
import {
async,
Observable,
of,
ReplaySubject,
Subject,
throwError,
} from 'rxjs';
import { ReplaySubject, Subject, throwError } from 'rxjs';
import * as sinon from 'sinon';
import { CANCEL_EVENT } from '../../constants';
import { InvalidGrpcPackageException } from '../../errors/invalid-grpc-package.exception';
import { GrpcMethodStreamingType } from '../../index';
import { ServerGrpc } from '../../server';
import { InvalidProtoDefinitionException } from '../../errors/invalid-proto-definition.exception';
import * as grpcHelpers from '../../helpers/grpc-helpers';
import { GrpcMethodStreamingType } from '../../index';
import { ServerGrpc } from '../../server';

class NoopLogger extends Logger {
log(message: any, context?: string): void {}
Expand Down

0 comments on commit 8d08184

Please sign in to comment.