dbader / node-datadog-metrics

Buffered metrics reporting via the DataDog HTTP API
https://dbader.org/blog/monitoring-your-nodejs-app-with-datadog
MIT License
142 stars 58 forks source link

Publish TypeScript types, user newer class syntax #104

Closed Mr0grog closed 2 years ago

Mr0grog commented 2 years ago

This converts most of the function/class comments in the code to JSDoc-style comments (adding some more detailed descriptions/types along the way) and uses TypeScript to build type definition files from the comments. This allows us to publish types directly from this package instead of making TypeScript users install @types/datadog-metrics, which is maintained separately, by hand, and isn’t always be up-to-date.

It also converts old-style classes to the newer, ES6+ class syntax. The TypeScript compiler generates clearer and less redundant type definitions when classes are used, so this actually has a meaningful impact beyond just code style.

When building types, the TypeScript compiler can perform limited validation of the actual JavaScipt code (making sure it plausibly matches the types in the comments, and where types are specified, making sure the code matches what the types say can be done). I’ve turned that on and added a CI step to generate types, so we should see errors if the doc comments change in an incorrect way, or if a function signature changes without also updating the doc comments describing the types.

Finally, I’ve added an NPM lifecycle script to automatically build types when publishing.

You can manually build types by running npm run build-types or clear out any generated code types by running npm run clean. The type definitions are build in the dist directory.

Note this does not do type checking on the test files — that would be a good idea, but would require a bunch more changes, and this PR is already too big (e.g. the should assertions don’t work, you need to use expect instead).

Fixes #83, fixes #86.

Mr0grog commented 2 years ago

We also get nice type checks in CI now! This is what shows up in the "files" tab of a PR (I made this error intentionally to test it):

Screen Shot 2022-10-13 at 1 03 19 PM