Skip to content

Commit

Permalink
feat(docs): add @param.filter and @param.where to OpenAPI decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Feb 27, 2020
1 parent 4b367d8 commit 282c371
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions docs/site/decorators/Decorators_openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -794,3 +794,63 @@ This decorator does not affect the top-level `tags` section defined in the
[OpenAPI Tag Object specification](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#tag-object).
This decorator only affects the spec partial generated at the class level. You
may find that your final tags also include a tag for the controller name.

## Shortcuts for Filter and Where params

CRUD APIs often expose REST endpoints that take `filter` and `where` query
parameters. For example:

```ts
class TodoController {
async find(
@param.query.object('filter', getFilterSchemaFor(Todo))
filter?: Filter<Todo>,
): Promise<Todo[]> {
return this.todoRepository.find(filter);
}

async findById(
@param.path.number('id') id: number,
@param.query.object('filter', getFilterSchemaFor(Todo))
filter?: Filter<Todo>,
): Promise<Todo> {
return this.todoRepository.findById(id, filter);
}

async count(
@param.query.object('where', getWhereSchemaFor(Todo)) where?: Where<Todo>,
): Promise<Count> {
return this.todoRepository.count(where);
}
}
```

To simplify the parameter decoration for `filter` and `where`, we introduce two
sugar decorators:

- `@param.filter`: For a `filter` query parameter
- `@param.where`: For a `where` query parameter

Now the code from above can be refined as follows:

```ts
class TodoController {
async find(
@param.filter(Todo)
filter?: Filter<Todo>,
): Promise<Todo[]> {
return this.todoRepository.find(filter);
}

async findById(
@param.path.number('id') id: number,
@param.filter(Todo, {exclude: 'where'}) filter?: FilterExcludingWhere<Todo>,
): Promise<Todo> {
return this.todoRepository.findById(id, filter);
}

async count(@param.where(Todo) where?: Where<Todo>): Promise<Count> {
return this.todoRepository.count(where);
}
}
```

0 comments on commit 282c371

Please sign in to comment.