-
Notifications
You must be signed in to change notification settings - Fork 481
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
Registering custom decorators #32
Comments
Hi @mogusbi, |
Is there no way to get this module to automatically pick this up like it does for the standard Feels a bit cumbersome having to include the |
For anyone who is still interested in this, after reading the source code, I finally found out a way to achive this by passing a second paramter ( import { ApiQuery } from '@nestjs/swagger';
export const Pagination = createParamDecorator(
(data: unknown, context: ExecutionContext): PaginationParams => {
const request = context.switchToHttp().getRequest();
// Whatever logic you want to parse params in request
return {
pageSize: parseInt(request.query.pageSize, 10) || DEFAULT_PAGE_SIZE,
page: parseInt(request.query.page, 10) || 1
};
},
[
(target: any, key: string) => {
// Here it is. Use the `@ApiQuery` decorator purely as a function to define the meta only once here.
ApiQuery({
name: 'page',
schema: { default: 1, type: 'number', minimum: 1 },
required: false
})(target, key, Object.getOwnPropertyDescriptor(target, key));
ApiQuery({
name: 'page_size',
schema: { default: DEFAULT_PAGE_SIZE, type: 'number', minimum: 1 },
required: false
})(target, key, Object.getOwnPropertyDescriptor(target, key));
}
]
) Then use the decorator in your controller just like what you used to do, swagger will pick up the extra params that you defined inside the decorator. @Get()
@UseGuards(AuthGuard)
@Roles(...adminGroup)
@ApiQuery({ name: 'id', type: String })
@ApiQuery({ name: 'org', type: String })
async find(
@AuthorizedUser() user: IDTokenClaims,
@Pagination() pagination: PaginationParams
@Query('id') id: string,
@Query('org') organization: string
) {
// Use your customer decorator
console.log(pagination.page)
// Use handler-specific query param
console.log(id)
} |
As explained in nestjs/swagger#32 (comment), it's possible to register swagger metadata in custom decorators by providing an array of `enhancers`. We now add metadata with the `MarkdownBody` decorator: The request needs a `body` with content-type `text/markdown`. Signed-off-by: David Mehren <[email protected]>
As explained in nestjs/swagger#32 (comment), it's possible to register swagger metadata in custom decorators by providing an array of `enhancers`. We now add metadata with the `MarkdownBody` decorator: The request needs a `body` with content-type `text/markdown`. Signed-off-by: David Mehren <[email protected]>
As explained in nestjs/swagger#32 (comment), it's possible to register swagger metadata in custom decorators by providing an array of `enhancers`. We now add metadata with the `MarkdownBody` decorator: The request needs a `body` with content-type `text/markdown`. Signed-off-by: David Mehren <[email protected]>
As explained in nestjs/swagger#32 (comment), it's possible to register swagger metadata in custom decorators by providing an array of `enhancers`. We now add metadata with the `MarkdownBody` decorator: The request needs a `body` with content-type `text/markdown`. Signed-off-by: David Mehren <[email protected]>
As explained in nestjs/swagger#32 (comment), it's possible to register swagger metadata in custom decorators by providing an array of `enhancers`. We now add metadata with the `MarkdownBody` decorator: The request needs a `body` with content-type `text/markdown`. Signed-off-by: David Mehren <[email protected]>
As explained in nestjs/swagger#32 (comment), it's possible to register swagger metadata in custom decorators by providing an array of `enhancers`. We now add metadata with the `MarkdownBody` decorator: The request needs a `body` with content-type `text/markdown`. Signed-off-by: David Mehren <[email protected]>
As explained in nestjs/swagger#32 (comment), it's possible to register swagger metadata in custom decorators by providing an array of `enhancers`. We now add metadata with the `MarkdownBody` decorator: The request needs a `body` with content-type `text/markdown`. Signed-off-by: David Mehren <[email protected]>
I have a custom decorator for pagination using query strings but I have not been able to get Swagger to pick them up
The text was updated successfully, but these errors were encountered: