Skip to content

Use typing to force parsing all query parameters#72

Merged
JosephMarinier merged 2 commits intodevfrom
joseph/typing-force-query-parameters
May 18, 2022
Merged

Use typing to force parsing all query parameters#72
JosephMarinier merged 2 commits intodevfrom
joseph/typing-force-query-parameters

Conversation

@JosephMarinier
Copy link
Contributor

@JosephMarinier JosephMarinier commented May 17, 2022

Description:

Checklist:

You should check all boxes before the PR is ready. If a box does not apply, check it to acknowledge
it.

  • PRE-COMMIT. You ran pre-commit on all commits, or else, you
    ran pre-commit run --all-files at the end.
  • FRONTEND TYPES. Regenerate the front-ent types if you played with types and routes.
    Run cd webapp && yarn types while the back-end is running.
  • USER CHANGES. The changes are added to CHANGELOG.md and the documentation, if they impact
    our users.
  • DEV CHANGES.
    • Update the documentation if this PR changes how to develop/launch on the app.
    • Update the README files and our wiki for any big design decisions, if relevant.
    • Add unit tests, docstrings, typing and comments for complex sections.

as we were overwriting with the default value. This used to make sense as we were reusing the `DEFAULT_SPACING` constant elsewhere, but we no longer do.
});
const convertSearchParams = <T>(
q: URLSearchParams,
conversions: Required<{ [Key in keyof T]: (key: string | null) => T[Key] }>
Copy link
Contributor Author

@JosephMarinier JosephMarinier May 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the important bit: The Required ensures that a conversion function is defined for all query parameters (all keyof T) defined in the different Query...State types, even if they have a ?:. This ensures that q.get() gets called with only parameter names from a Query...State type. This replaces the 12 calls to q.get() with 12 string constants, which seemed error-prone.

sort: (q.get(SORT) || undefined) as UtterancesSortableColumn | undefined,
descending: q.get(DESCENDING) !== null || undefined,
});
const convertStringArray = <T extends string>(s: string | null) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The T is to seamlessly support string literals.

Copy link

@christyler3030 christyler3030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems very smart. Typescript rules.

@JosephMarinier JosephMarinier merged commit 186e067 into dev May 18, 2022
@JosephMarinier JosephMarinier deleted the joseph/typing-force-query-parameters branch May 18, 2022 15:31
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.

2 participants