-
Notifications
You must be signed in to change notification settings - Fork 65
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
Make defaultReporter available as a named export #140
Conversation
06536ee
to
10504cf
Compare
Awesome, thanks for this PR @af! It will will definitely save me from repeating code in a couple of projects! To be honest, the only way I usually override the reporter is making it throw instead of exiting. Perhaps, the library could expose something like At the moment, ‘reporter that throws’ looks like this for me. It can be more elegant if both reporters could be based on export const customEnvalidReporter: typeof defaultReporter = (opts) => {
const originalConsoleError = console.error;
const originalProcessExit = process.exit;
let errorOutput = "\n";
console.error = (output) => {
errorOutput += output;
};
process.exit = () => undefined as never;
defaultReporter(opts);
process.exit = originalProcessExit;
console.error = originalConsoleError;
if (errorOutput.length > 1) {
throw new Error(errorOutput);
}
}; |
src/index.ts
Outdated
@@ -3,3 +3,4 @@ export * from './errors' | |||
export * from './middleware' | |||
export * from './types' | |||
export * from './validators' | |||
export { default as defaultReporter } from './reporter' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is not related to the PR directly, but might help you in the future a bit. I stopped using default exports for anything except for React components and my DX became noticeably better.
When re-exporting files from a directly, I can now always do:
export * from "./a";
export * from "./b";
export * from "./d";
If there is a file that exports both ‘private’ and ‘public’ things, I split it into two and don’t re-export stuff from the ‘private’ file. In the example above, it would be "./c"
.
Here’s a blog post on this topic:
https://basarat.gitbook.io/typescript/main-1/defaultisbad
I still (#101 (comment)) think we should throw rather than exit, but not just for browser compat. In practice it's the same thing for the consumer unless they explicitly catch and ignore the error. But then that's their choice. That said, exporting the reporter doesn't seem like it should hurt, so not really a comment on this PR as much as the use case 😀 |
@kachkaev Added a couple commits to use a named export and also be able to pass in @SimenB Maybe it's just me being unreasonably picky, but I don't like having the unnecessary traceback when throwing in the reporter. It certainly would be simpler to just throw though. Anyways, you should be able to always throw with this latest commit if you wish :) |
I like how it looks! Thanks so much for making the changes! |
* Make defaultReporter available as a named export * Add extra defaultReporter options, use named export
Potential solution for #137 (comment)