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

Improve types #839

Merged
merged 6 commits into from
Aug 20, 2020
Merged

Improve types #839

merged 6 commits into from
Aug 20, 2020

Conversation

Nevon
Copy link
Collaborator

@Nevon Nevon commented Aug 15, 2020

This does two things:

Constrain the parameter types for cluster.fetchTopicOffsets

cluster.fetchTopicOffsets now has two optional parameters, fromBeginning and fromTimestamp. In reality, these are mutually exclusive. There is no case in which you would want to pass in both. Simultaneously, there is also no case in which you would not want to pass in one of them. I.e. they are mutually exclusive but required.

// Would have compiled but failed at runtime
await cluster.fetchTopicOffsets([{
  topic: 'foo',
  partitions: [{ partition: 1 }]
}])

// Would compile but behave strangely at runtime
await cluster.fetchTopicOffsets([{
  topic: 'foo',
  partitions: [{ partition: 1 }]
  fromBeginning: true,
  timestamp: 1597492814792
}])

With the updated type definition, neither of those cases are possible anymore.

Screenshot 2020-08-15 at 14 04 06

Screenshot 2020-08-15 at 14 05 21

Add JSDoc annotations for internal factory methods for Consumer, Producer and Admin

Some of these previously had return annotations but not all of them, and none of them had parameter annotations so everything was any, pretty much.

Unfortunately I couldn't find a way to get JSDoc to understand that params.instrumentationEmitter should be an instance of InstrumentationEventEmitter. The typedef in there didn't make any sense, so I removed that. It should just be a matter of importing the module and then just using the classname, but it doesn't seem to work in VSCode at least.

Also, there is absolutely no way to reference an enum in a @param, which I noticed when trying to type isolationLevel. Best I can do is number, which is the underlying type. You can constrain the possible values by putting literals in there (@param {(0|1|2)} isolationLevel), but that is ridiculous.

Cluster.fetchTopicOffsets accepts either fromBeginning OR fromTimestamp,
not both. While it technically would work to pass both in, it doesn't
make sense.

This change makes it so that you need to include one of them, but not both.
@Nevon Nevon requested review from tulios and JaapRood August 15, 2020 12:09
src/admin/index.js Outdated Show resolved Hide resolved
src/consumer/index.js Outdated Show resolved Hide resolved
@Nevon Nevon merged commit 983c3d7 into master Aug 20, 2020
@Nevon Nevon deleted the improve-fetch-topic-offset-types branch August 20, 2020 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants