-
Notifications
You must be signed in to change notification settings - Fork 44
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
Improve param decorators #34
Conversation
Pull Request Test Coverage Report for Build 97
💛 - Coveralls |
Could you add a note to the readme about the Other than that it looks pretty cool! Thanks :) |
Readme is updated with the description and examples. |
No, this is behavior of the current nestjs-config version (1.2.4) and also it reproduces in the 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.
Thank you 😃
Another side note, can you just add a good tip in the ReadMe file that as a good practice, one should make the injected
@ConfigParam(...)
parameter in the method to be optional.
So that if he is trying to call it manually, not have to add a null
or make it's type of any
.
Thank you again 😃.
src/__tests__/config.module.spec.ts
Outdated
providers: [ComponentTest], | ||
}).compile(); | ||
const componentTest = module.get<ComponentTest>(ComponentTest); | ||
expect(componentTest.testConfig(null, null)).toEqual({ serverPort: 2000, stubPort: 2000 }); |
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.
As a side note, to get rid of null
, null
You could make the parameters optional ?
So you don't have to to pass it to the method.
src/utils.ts
Outdated
if (!param.configKey) { | ||
args[param.parameterIndex] = args[param.parameterIndex]; | ||
continue; | ||
if (param.configKey) { |
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.
Nice, bravoo 👏
PR updated. @shekohex , I've updates parameter types in the tests and Readme as you asked. |
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.
Well done 😃
This PR fixes 2 bugs in
ConfigParam()
andConfigurable()
decoratorConfigParam()
decorator set metadata to the target:Reflect.defineMetadata(CONFIG_PARAMS, existingParameters, target)
, but always retrieved it from target's property:Reflect.getMetadata(CONFIG_PARAMS, target, propertyKey)
. So previous metadata was deleted if decorator was called 2 times or more on the same method.Utility
applyParamsMetadataDecorator()
set config param to the argument only if the argument exists and it's an object (some unsafe condition as for me). SoConfigParam()
decorator worked only theConfigService()
was injected on the argument's place.But it's a fact that ConfigService may be not injected in all required places (check additional tests).
So I also added improvements to the
applyParamsMetadataDecorator()
utility.P.S. Probably it is't a question to the
nestjs-config
and not a topic of this PR, but whenConfigurable()
decorator applied to the controller method and it was placed to top of order - it breaks other decorators.I think it's because it replaces original method with own function.
It will be nice to have some note in the Readme that
Configurable()
decorator must be the last.