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

@Param does not allow ParamOptions #306

Closed
IAMtheIAM opened this issue Oct 11, 2017 · 7 comments
Closed

@Param does not allow ParamOptions #306

IAMtheIAM opened this issue Oct 11, 2017 · 7 comments

Comments

@IAMtheIAM
Copy link
Contributor

IAMtheIAM commented Oct 11, 2017

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

    defaults           : {
        paramOptions: {
            required: true // this means that all parameters (@BodyParam, @Param etc in Actions) are required by default
        }
    }

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?

@IAMtheIAM
Copy link
Contributor Author

IAMtheIAM commented Oct 11, 2017

I just noticed PR 289. Does that PR solve this issue I mentioned?

@MichalLytek
Copy link
Contributor

Nope, just @Params and @QueryParams

But Param will not accept the second argument. Can Param signature be modified to allow ParamOptions like the rest?

as it stated:

params are always required, because if they are missing router will not match the route

@IAMtheIAM
Copy link
Contributor Author

IAMtheIAM commented Oct 11, 2017

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

@MichalLytek
Copy link
Contributor

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.

@IAMtheIAM
Copy link
Contributor Author

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);
            }
        }

@MichalLytek
Copy link
Contributor

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.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants