-
Notifications
You must be signed in to change notification settings - Fork 154
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
[#1756] Add JSON validation and migrate api.js to TypeScript #1903
Conversation
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.
Learned a lot from your code as I don't have much prior experience using TypeScript! Regarding file structuring, I think we could move the files user.ts
and segment.ts
from the utils
directory to the types
directory for better organization.
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.
The JSON validation addition looks good to me! Although, I am of the opinion that the changes to eslint should be in a separate PR following the good practice of limiting each PR to tackling a single issue
I agree, that would definitely help the organization/structure of the files! Since it will also require changing some |
Thanks, I'll open a separate PR for the eslint changes. For now, I added two lines in |
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.
Really impressive work given how fast you did it. I like the organisation of the files that you added. Codes are also arranged in an easy-to-follow manner.
One small request will be to add the function parameter types wherever appropriate. That way when the functions are called, we can avoid any type error directly. Do let me know your thoughts as well.
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.
Thanks for the work. LGTM!
@vvidday Sorry, there are some file conflicts in this branch. Are you able to resolve them? |
@dcshzj Sorry, have just resolved the conflicts! |
The following links are for previewing this pull request:
|
Fixes #1756
Should not be merged before #1905
Proposed commit message
Other information
Organization
The files containing types are in the
src/types
folder, with the following layout:The files in the
zod
subdirectory are zod schemas that are used to validate the parsed objects fromauthorship.json
,commits.json
andsummary.json
, whereas the other two files contain regular typescript interfaces, hence the distinction.window.ts
contains type definitions for all the global variables stored in thewindow
object (window.XXX
), and all the other 'normal' types are intypes.ts
. Please let me know if you have any suggestions for better organization!api.ts
Changes to this file were mostly related to adding types/typecasts to make it work with the typescript compiler. The only functional change is the three
xxxSchema.parse(json)
underapi.loadSummary
,api.loadCommits
andapi.loadAuthorship
. This change will make it so that if the object doesn't match the schema (which can be either due to a modified.json
file or a change to the schema in the backend of any of the three.json
files), the app will throw an error instead of passing on the data. This will make it so the error is surfaced immediately instead of being passed on to other components and silently breaking elsewhere.The reason why I'm migrating the whole file in this PR is because adding JSON validation requires the migration of most of the file (everything in
window.api
), and the only 'extra' aspects are just the type definitions of the other global variables (window.XXX
). If we want to separate this into 2 PRs, it will be a bit weird since we will be migrating half the file, and as far as I can tell there isn't an easy way to get typescript to ignore a section of the file (see microsoft/TypeScript#19573)Changes to eslint
The plugin added in line 9 allows proper resolving of typescript file imports, the change of rule from line 51 onwards makes the linter allow us to import typescript files like
import ... from ../file
instead ofimport ... from ../file.ts
(which throws an error). The side effect is that the typescript imports in the.vue
files must also be changed to remove the.ts
, which is why this PR also modifies vue files. Although, if it is better, I can temporarily ignore the rule here and open a separate PR to change this.