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

feat: add cast array to expected array query params #677

Merged
merged 5 commits into from
Aug 4, 2021

Conversation

BenjD90
Copy link
Contributor

@BenjD90 BenjD90 commented Mar 12, 2021

Description

Checklist

  • the pull request title describes what this PR does (not a vague title like Update index.md)
  • the pull request targets the default branch of the repository (develop)
  • the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • tests are added for the changes I made (if any source code was modified)
  • documentation added or updated
  • I have run the project locally and verified that there are no errors → tests pass

Fixes

Fixes #389, Fixes #320, Fixes #123

The idea is to parse query parameters as it is used to be. I added some exemple in the README, and in the tests :

You can use isArray option to get a query param array. This will cast the query param :

@Get("/users/by-multiple-ids")
getUsers(@QueryParam("ids", { isArray: true}) ids: string[]) {
}

GET /users/by-multiple-ids?ids=aids = ['a']
GET /users/by-multiple-ids?ids=a&ids=bids = ['a', 'b']

You can combine use isArray option with type option to get a query param array of one type. This will cast the query param :

@Get("/users/by-multiple-ids")
getUsers(@QueryParam("ids", { isArray: true, type: Number}) ids: number[]) {
}

GET /users/by-multiple-ids?ids=1ids = [1]
GET /users/by-multiple-ids?ids=1&ids=3.5ids = [1, 3.5]

For QueryParams :

enum Roles {
    Admin = "admin",
    User = "user",
    Guest = "guest",
}

class GetUsersQuery {

    @IsPositive()
    limit: number;

    @IsAlpha()
    city: string;

    @IsEnum(Roles)
    role: Roles;

    @IsBoolean()
    isActive: boolean;

    @IsArray()
    @IsNumber(undefined, { each: true })
    @Type(() => Number)
    ids: number[];
}

@Get("/users")
getUsers(@QueryParams() query: GetUsersQuery) {
    // here you can access query.role, query.limit
    // and others valid query parameters
    // query.ids will be an array, of numbers, even with one element
}

@BenjD90 BenjD90 marked this pull request as ready for review March 25, 2021 13:47
@BenjD90 BenjD90 changed the title Add cast array to expected array query params feat: add cast array to expected array query params Mar 25, 2021
@BenjD90
Copy link
Contributor Author

BenjD90 commented Mar 29, 2021

@NoNameProvided @nolazybits Is it possible to review this PR ?

thanks,
Benjamin

nolazybits
nolazybits previously approved these changes Mar 29, 2021
Copy link
Contributor

@nolazybits nolazybits left a comment

Choose a reason for hiding this comment

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

Looks good to me but that would be my first review here. I might have missed something. @NoNameProvided might want to double check + I don't think I have write access to this repo (required to have a valid approval) to it won't help unblock this MR

@BenjD90
Copy link
Contributor Author

BenjD90 commented Mar 29, 2021

I found a bug on boolean management if only one is sent as query param, I'll complete the test with more boolean cases

@BenjD90
Copy link
Contributor Author

BenjD90 commented Mar 29, 2021

The bug is now fixed 😃
I have added the test @QueryParams should give a proper values from request's query with validate whitelist option on for this specific case.

@jotamorais jotamorais merged commit 08e7aa0 into typestack:develop Aug 4, 2021
@github-actions
Copy link

github-actions bot commented Sep 4, 2021

This pull request 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 Sep 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
3 participants