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!: use a union type for SignatureType #331

Merged
merged 1 commit into from Sep 23, 2021

Conversation

matthewrobertson
Copy link
Member

@matthewrobertson matthewrobertson commented Sep 22, 2021

This commit refactors the SignatureType enum into an array of strings declared as const. This allows the SignatureType type to be expressed as a union type, which works a bit better when parsing a user provided string.

This is some simple refactoring in preparation for declarative function signatures.

BREAKING CHANGE: exported SignatureType type is converted from an enum to a union type

@@ -16,8 +16,22 @@
// executing the client function.
export const FUNCTION_STATUS_HEADER_FIELD = 'X-Google-Status';

export enum SignatureType {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't agree with using string literals and arrays instead of enums. Enums provide benefits over literal values such as better typing, autocompletion, and less code here. We don't plan an arbitrary signature type until the FF contract changes.

This is also a breaking change to the API, not an internal refactor.

Could we discuss a bit more about the tradeoff in the PR / an issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

We're fine changing to the union types, but we shouldn't completely remove the enum as it's an exported interface, unless we have strong reason to introduce a breaking change.

For example, we had a customer complaint about a similar small interface change:
#216

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we give customer an opportunity to pass the value to use in any meaningful way (the signature type can only be provided as an env var or cli flag), so I don't think we should be too concerned about changing this. I see your point about why this is breaking change now. I will updated the conventional commit tag.

@@ -108,7 +101,7 @@ getUserFunction(CODE_LOCATION, TARGET).then(userFunction => {

SERVER.listen(PORT, () => {
ERROR_HANDLER.register();
if (process.env.NODE_ENV !== NodeEnv.PRODUCTION) {
if (process.env.NODE_ENV !== 'production') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not seeing TS error for misconfigured strings with all.

For example, if I type:

if (process.env.NODE_ENV !== 'prodduction') {

here, I won't get a TS error. I'd rather just be able to type NodeEnv. and see an autocompleted result of options and errors if I mistype.

This commit refactors the SignatureType enum into an array of strings
declared [as const](microsoft/TypeScript#29510).
This allows the SignatureType type to be expressed as a union type,
which works a bit better when parsing a users provided string.

This is some simple refactoring in preparation for declarative function
signatures.

BREAKING CHANGE: exported SignatureType type is converted from an enum
to a union type
@matthewrobertson matthewrobertson changed the title refactor: use a union type for SignatureType refactor!: use a union type for SignatureType Sep 23, 2021
@matthewrobertson matthewrobertson merged commit 9cf46ed into master Sep 23, 2021
@matthewrobertson matthewrobertson deleted the signature-union-type branch December 7, 2021 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants