Skip to content

Commit f8c3d1f

Browse files
committed
params: Add documentation for array query param and query params
1 parent ea25180 commit f8c3d1f

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,28 @@ getUsers(@QueryParam("limit") limit: number) {
378378
}
379379
```
380380

381+
You can use `isArray` option to get a query param array. This will cast the query param :
382+
383+
```typescript
384+
@Get("/users/by-multiple-ids")
385+
getUsers(@QueryParam("ids", { isArray: true}) ids: string[]) {
386+
}
387+
```
388+
389+
`GET /users/by-multiple-ids?ids=a``ids = ['a']`
390+
`GET /users/by-multiple-ids?ids=a&ids=b``ids = ['a', 'b']`
391+
392+
You can combine use `isArray` option with `type` option to get a query param array of one type. This will cast the query param :
393+
394+
```typescript
395+
@Get("/users/by-multiple-ids")
396+
getUsers(@QueryParam("ids", { isArray: true, type: Number}) ids: number[]) {
397+
}
398+
```
399+
400+
`GET /users/by-multiple-ids?ids=1``ids = [1]`
401+
`GET /users/by-multiple-ids?ids=1&ids=3.5``ids = [1, 3.5]`
402+
381403
If you want to inject all query parameters use `@QueryParams()` decorator.
382404
The bigest benefit of this approach is that you can perform validation of the params.
383405

@@ -402,12 +424,17 @@ class GetUsersQuery {
402424
@IsBoolean()
403425
isActive: boolean;
404426

427+
@IsArray()
428+
@IsNumber(undefined, { each: true })
429+
@Type(() => Number)
430+
ids: number[];
405431
}
406432

407433
@Get("/users")
408434
getUsers(@QueryParams() query: GetUsersQuery) {
409435
// here you can access query.role, query.limit
410436
// and others valid query parameters
437+
// query.ids will be an array, of numbers, even with one element
411438
}
412439
```
413440

@@ -1535,7 +1562,7 @@ export class QuestionController {
15351562

15361563
| Signature | Example | Description |
15371564
| ---------------------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
1538-
| `@Authorized(roles?: string\|string[])` | `@Authorized("SUPER_ADMIN")` get() | Checks if user is authorized and has given roles on a given route. `authorizationChecker` should be defined in routing-controllers options. | |
1565+
| `@Authorized(roles?: string\|string[])` | `@Authorized("SUPER_ADMIN")` get() | Checks if user is authorized and has given roles on a given route. `authorizationChecker` should be defined in routing-controllers options. |
15391566
| `@CurrentUser(options?: { required?: boolean })` | get(@CurrentUser({ required: true }) user: User) | Injects currently authorized user. `currentUserChecker` should be defined in routing-controllers options. |
15401567
| `@Header(headerName: string, headerValue: string)` | `@Header("Cache-Control", "private")` get() | Allows to explicitly set any HTTP header returned in the response. |
15411568
| `@ContentType(contentType: string)` | `@ContentType("text/csv")` get() | Allows to explicitly set HTTP Content-Type returned in the response. |

0 commit comments

Comments
 (0)