-
Notifications
You must be signed in to change notification settings - Fork 393
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
@Param does not allow ParamOptions #306
Comments
I just noticed PR 289. Does that PR solve this issue I mentioned? |
Nope, just
as it stated:
|
I see. The use case I had was to reduce redundancy in having one route for with route param and one without. Like this: @Get(route)
@Get(`${route}/:userName`)
getOne(
@QueryParam("bearerToken") bearerToken: string, @QueryParam("mockData", { required: false }) mockData: boolean, @Param("userName", { required: false }) userName: string) {
if (!bearerToken) {
return { "message": "Error: Authentication required. Operation not allowed" };
} else {
return this._callApi(bearerToken, mockData, userName);
}
} That way I don't have to totally duplicate the code just for a route param |
So you call api with undefined? For me this should be separate routes - one that does not care about userName and the one that use it and return user-specific data. |
OK, yes I ended up making two routes. Thanks for clarification. So a Param aka "RouteParam" should always be required, otherwise make a different route. Got it. @Get(route)
get(@HeaderParam("Authorization") bearerToken: string,
@QueryParam("mockData", { required: false }) mockData: boolean) {
if (!bearerToken) {
return { "message": "Error: Authentication required. Operation not allowed" };
} else {
return this._callApi(bearerToken, mockData);
}
}
// @Get(route)
@Get(`${route}/:userName`)
getOne(
@HeaderParam("Authorization") bearerToken: string,
@QueryParam("mockData", { required: false }) mockData: boolean,
@Param("userName") userName: string) {
if (!bearerToken) {
return { "message": "Error: Authentication required. Operation not allowed" };
} else {
return this._callApi(bearerToken, mockData, userName);
}
} |
You should consider moving: if (!bearerToken) {
return { "message": "Error: Authentication required. Operation not allowed" };
} check into middleware mounted per route or per controller. That will reduce code duplication. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Signature for Param is
export declare function Param(name: string): Function;
But signature for QueryParam is
export declare function QueryParam(name: string, options?: ParamOptions): Function;
This is problematic because, by default, in useExpressServer() i have set
so that all params are required. Then I manually override with
@QueryParam("mockData", { required: false }) mockData: boolean
But Param will not accept the second argument.
Can Param signature be modified to allow ParamOptions like the rest?
The text was updated successfully, but these errors were encountered: