Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add type parameter to warn function #1009

Merged
merged 8 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@types/fs-extra": "^8.0.1",
"@types/mocha": "^8.0.0",
"@types/ncp": "^2.0.1",
"@types/node": "^14.0.0",
"@types/node": ">=15.6.0",
"@types/node-fetch": "^2.5.4",
"@types/object-hash": "^2.1.0",
"@types/proxyquire": "^1.3.28",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ export {
} from './clientInterface';

export {ServiceError, ChannelCredentials} from '@grpc/grpc-js';
export {warn} from './warnings';
7 changes: 6 additions & 1 deletion src/warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {isBrowser} from './isbrowser';

const emittedWarnings = new Set<string>();

export function warn(code: string, message: string) {
// warnType is the type of warning (e.g. 'DeprecationWarning', 'ExperimentalWarning', etc.)
export function warn(code: string, message: string, warnType?: string) {
// Only show a given warning once
if (emittedWarnings.has(code)) {
return;
Expand All @@ -27,6 +28,10 @@ export function warn(code: string, message: string) {

if (isBrowser()) {
console.warn(message);
} else if (typeof warnType !== 'undefined') {
process.emitWarning(message, {
type: warnType,
});
} else {
process.emitWarning(message);
}
Expand Down
11 changes: 11 additions & 0 deletions test/unit/warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,15 @@ describe('warnings', () => {
stub.restore();
done();
});
it('should include warning type if type is provided', done => {
const stub = sinon.stub(process, 'emitWarning');
warn('codeD', 'messageD-1', 'WarningType1');
assert(
stub.calledWith('messageD-1', {
type: 'WarningType1',
})
);
stub.restore();
done();
});
});