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

refactor: introduce branded types #61

Closed
wants to merge 48 commits into from
Closed

refactor: introduce branded types #61

wants to merge 48 commits into from

Conversation

ComradeVanti
Copy link
Collaborator

Introduce branded types for many of the simple types in the project. This has two primary effects/goals:

  1. This is a good basis to do better validation of cli arguments. This is not implemented in this PR but will be soon.
  2. It exposed a lot of placed in the code where the incorrect type was used. For example name@version was used where only name should have been allowed etc.

Tests were added for all new types.

This PR also includes some helpers for constructing PkgInfo and PkgManifest objects for tests, as well as some other minor changes.

Add type information indicating that result code will always be either 0 or 1
Use information found in test usage to adjust types throughout the project. Also type constants in tests
Add utility functions to de-duplicate the common logic of adding packages and search results to the mock package registry in tests
Split utilities file into mock-work-dir and mock-console
Hide implementation detail of what mock console is used from tests
Add helpers  to check for correct manifest in add cmd tests
Move logic for, creating, resetting and querying mock console content into dedicated functions
To avoid warnings. Only dev dependencies
Move manifest assertion helpers to own module so they can be reused
prepare for allowing to chain multiple assertions on same manifest
Use manifest assertions in cmd remove tests. Also add a few new ones
Use manifest assertions in manifest tests
There should never be a version inside the name
Add branded type for reverse-domain-name. This showed a lot of cases where pkg-name type was used, where reverse-domain-name should have been used. Fixed these cases
@ComradeVanti
Copy link
Collaborator Author

Note: This is an extension of #60, so the first few commits are the same

Extends project tsconfig, but without emits
Rename to just domain-name since it should also work for regular domain names. Also it's a little shorter that way.

The rules for what makes a valid name also changes a little bit. Now single segment names like just "com" are also valid. We don't need to do that much validation.
Add a builder function for making pkg-info objects for tests.
Includes validation logic and auto-sets derived properties, such as also setting the package _id to the name
Builder does validation and allows for shorter test setups
Fix errors from passing strings to functions expecting domain names. Fix by using name fields from packages or asserting validity using "as"
The response object used by npm-registry-fetch is node-fetch Response, not request Response
Function acts on domain-types and so should be in a different file
Introduce package-url type which represents an url pointing to a package. Also add a few tests
Add type-checking for semantic-version strings. Currently not much validation
Package at specific version
@ComradeVanti
Copy link
Collaborator Author

Ok, I kind of messed up this PR branch, I'll redo it an open it again

@ComradeVanti ComradeVanti deleted the branded-types-refactor branch November 27, 2023 14:54
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.

1 participant