-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'krugi-feature/grpc-server-package-definition'
- Loading branch information
Showing
10 changed files
with
151 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...roservices/errors/invalid-grpc-package-definition-missing-package-definition.exception.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { RuntimeException } from '@nestjs/core/errors/exceptions/runtime.exception'; | ||
|
||
export class InvalidGrpcPackageDefinitionMissingPackageDefinitionException extends RuntimeException { | ||
constructor() { | ||
super( | ||
`Invalid gRPC configuration. protoPath or packageDefinition must be defined.`, | ||
); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/microservices/errors/invalid-grpc-package-definition-mutex.exception.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { RuntimeException } from '@nestjs/core/errors/exceptions/runtime.exception'; | ||
|
||
export class InvalidGrpcPackageDefinitionMutexException extends RuntimeException { | ||
constructor() { | ||
super( | ||
`Invalid gRPC configuration. Both protoPath and packageDefinition cannot be defined at the same time.`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { InvalidGrpcPackageDefinitionMissingPackageDefinitionException } from '../errors/invalid-grpc-package-definition-missing-package-definition.exception'; | ||
import { InvalidGrpcPackageDefinitionMutexException } from '../errors/invalid-grpc-package-definition-mutex.exception'; | ||
import { GrpcOptions } from '../interfaces'; | ||
|
||
export function getGrpcPackageDefinition( | ||
options: GrpcOptions['options'], | ||
grpcProtoLoaderPackage: any, | ||
) { | ||
const file = options['protoPath']; | ||
const packageDefinition = options['packageDefinition']; | ||
|
||
if (file && packageDefinition) { | ||
throw new InvalidGrpcPackageDefinitionMutexException(); | ||
} | ||
if (!file && !packageDefinition) { | ||
throw new InvalidGrpcPackageDefinitionMissingPackageDefinitionException(); | ||
} | ||
|
||
return ( | ||
packageDefinition || | ||
grpcProtoLoaderPackage.loadSync(file, options['loader']) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { expect } from 'chai'; | ||
import { InvalidGrpcPackageDefinitionMissingPackageDefinitionException } from '../../errors/invalid-grpc-package-definition-missing-package-definition.exception'; | ||
import { InvalidGrpcPackageDefinitionMutexException } from '../../errors/invalid-grpc-package-definition-mutex.exception'; | ||
import { getGrpcPackageDefinition } from '../../helpers/grpc-helpers'; | ||
|
||
const grpcProtoLoaderPackage = { loadSync: (a, b) => 'withLoader' }; | ||
|
||
describe('getGrpcPackageDefinition', () => { | ||
describe('when missing both protoPath and packageDefinition', () => { | ||
it('should throw InvalidGrpcPackageDefinitionMissingPackageDefinitionException', () => { | ||
expect(() => | ||
getGrpcPackageDefinition( | ||
{ | ||
package: 'somePackage', | ||
}, | ||
grpcProtoLoaderPackage, | ||
), | ||
).to.throw(InvalidGrpcPackageDefinitionMissingPackageDefinitionException); | ||
}); | ||
}); | ||
|
||
describe('when both protoPath and packageDefinition are defined', () => { | ||
it('should throw InvalidGrpcPackageDefinitionMutexException', () => { | ||
expect(() => | ||
getGrpcPackageDefinition( | ||
{ | ||
package: 'somePackage', | ||
protoPath: 'some/path', | ||
packageDefinition: {}, | ||
}, | ||
grpcProtoLoaderPackage, | ||
), | ||
).to.throw(InvalidGrpcPackageDefinitionMutexException); | ||
}); | ||
}); | ||
|
||
describe('when only protoPath is defined', () => { | ||
it('should not throw any exception', () => { | ||
expect(() => | ||
getGrpcPackageDefinition( | ||
{ | ||
package: 'somePackage', | ||
protoPath: 'some/path', | ||
}, | ||
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters