-
Notifications
You must be signed in to change notification settings - Fork 36
feat: adds RequireFlagsEnabled decorator #1159
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
base: main
Are you sure you want to change the base?
feat: adds RequireFlagsEnabled decorator #1159
Conversation
I think this is a good idea, and it "feels right" to me ergonomically. Make sure you add something to the test-app to test and document this. |
ca912f0
to
a67cc35
Compare
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.
I really like the addition!
Added some optional additions to it. Feel free to add them, otherwise we can add them in another PR :)
Thank you!
/** | ||
* Options for injecting a feature flag into a route handler. | ||
*/ | ||
interface RequireFlagsEnabledProps { |
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.
Also we could have a context factory here too. Basically what we globally have for the OpenFeatureModule:
contextFactory?: ContextFactory; |
But this would be optional to me. We do not have it for the other decorators too:
interface FeatureProps<T extends FlagValue> { |
So to me this is optional for this PR but it would be a great addition.
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.
Sounds good, will do.
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.
Cool, are you still planning to do it @kaushalkapasi? I can not see it yet, but you re-requested a review.
We can definitely leave it out for now if you want.
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.
@lukas-reining I decided not to add this in for now.
Would you be able to elaborate how this would be used and how it would work differently compared to the ability to define the context
(as on line 39)?
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.
No worries!
The difference is, that the factory could use the ExecutionContext
to get request information like headers or IP address and transform that to evaluation context.
This can currently only be done in the transaction context, having it here would allow e.g. to use a specific http header as context value for the specified flags only.
export const RequireFlagsEnabled = (props: RequireFlagsEnabledProps): ClassDecorator & MethodDecorator => | ||
applyDecorators(UseInterceptors(FlagsEnabledInterceptor(props))); |
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.
I think there could be value in similar function like: RequireFlagEquals
.
But this could also be added in another PR.
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.
Yeah! I opted to implement this first as the "easiest" option, a follow up would be to take a flag key and expected value combination to support String, Number and Object type flags.
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.
Yep that makes sense to me. I think most people would use this current decorator, which could basically just be a shorthand for a RequireFlagEquals
implementation. I think instead of a key/value combination though, a lambda predicate might be easier (a lambda taking the EvalutionDetails which will run to decide to run the request or not). For RequireFlagsEnabled
, the lambda would just be evalutationDetails.value === true
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.
@kaushalkapasi do you want to do this in this PR?
Because you requested reviews again but I could not find it yet.
We can also leave it out of here, but it might be really good to have here.
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.
I haven't added the lambda function in this PR. My only concern is that it would complicate the usage of this Boolean only provider.
I would definitely add it when we build a decorator for other flag types (String, Number and Object).
Let me know if you think it should be implemented here and I'd be happy to do so.
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.
If you do it like @toddbaert describes, RequireFlagsEnabled
would simply be an alias for RequireFlagEquals
with evalutationDetails.value === true
.
It would not make the RequireFlagsEnabled
more complicated in this case.
I would prefer to have it.
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.
Sounds good, I'll update this implementation soon and tag you for a re-review when ready!
I will have a look at what is wrong with the e2e test tomorrow @kaushalkapasi :) |
Seems to me like the test-harness git submodule was simply deleted. I've added it back in the latest commit. The e2e tests are working now but you have some small lint errors causing CI failures. |
packages/nest/test/fixtures.ts
Outdated
testBooleanFlag2: { | ||
defaultVariant: 'default', | ||
variants: { default: false, enabled: true }, | ||
disabled: false, | ||
}, |
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.
Could we add another integration test where we use a flag with a simple targeting rule, which requires an evaluation context value (which is injected in an interceptor) to be present? That would ensure that we are using a per-request context as expected in our decorator(s).
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.
Sounds good, will add the test.
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.
I have a couple questions, and one request for an additional test (please let me know if you think this is not necessary) but this looks really cool. Great work!
Happy to approve from my perspective after these are resolved.
aa03a3a
to
ba2c4d6
Compare
Sorry for taking time @kaushalkapasi, many of us have been at KubeCon. |
…er & endpoint access based on boolean flag values Signed-off-by: Kaushal Kapasi <[email protected]>
…s enabled decorator Signed-off-by: Kaushal Kapasi <[email protected]>
Signed-off-by: Kaushal Kapasi <[email protected]>
…text and allow for flags to change the default value for flag evaluation. move getClientForEvaluation method to utils file Signed-off-by: Kaushal Kapasi <[email protected]>
Signed-off-by: Todd Baert <[email protected]>
Signed-off-by: Kaushal Kapasi <[email protected]>
…add tests with targeting rules defined for the InMemoryProvider Signed-off-by: Kaushal Kapasi <[email protected]>
ba2c4d6
to
f056ae6
Compare
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.
It would be great if you could also add something to the README to show the feature and explain the usage, so people know it exists and how to use :)
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.
Implementation looks great. Like @lukas-reining says I think we could use some docs so people know how to use this cool new feature... but I'm approving.
This PR
RequireFlagsEnabled
decorator to allow a simple, reusable way to block access to a specific controller or endpoint based on the value of a list of one, or many, boolean flagsNotes
Follow-up Tasks
RequireFlagsEnabled
decorator & usage examplesHow to test
npx jest --selectProject=nest