Reputation: 2990
how do I decorate a custom method inside my CrudController so that the Swagger documentation would be shown as the one from getManyBase? Meaning I need to have all of the filter fields.
I tried this way
@Get('/projects')
@UseInterceptors(CrudRequestInterceptor)
@ApiResponse({ status: 200, type: Project, isArray: true })
getManyProjects(@ParsedRequest() req: CrudRequest, @Request() request)
: Promise<GetManyDefaultResponse<Project> | Project[]> {
const { id, role } = request.user;
if (role === UserRoles.User) {
req.parsed.filter.push({
field: 'userId',
operator: 'eq',
value: id,
});
}
return this.projectService.getMany(req);
}
but the Swagger docs shows empty for the query parameters,
while I'm expecting something like getManyBase
.
Funny thing is, the method would work properly if I send the filter
string, but I need Swagger to display them as well.
Advice?
Upvotes: 1
Views: 1488
Reputation: 31
If the constructor approach does not work for you. Then probably your controller has scope: REQUEST. So controller instance is not created while application initialisation. In this case, you can have custom method inside a controller, like
initSwagger() {
const metadata = Swagger.getParams(this.getManyProjects);
const queryParamsMeta = Swagger.createQueryParamsMeta('getManyBase',{
model: { type: MyModel },
query: {
softDelete: false,
},
});
Swagger.setParams([...metadata, ...queryParamsMeta], this.getManyProjects);
}
then in your main entrypoint file you can write:
app.get(YourController).initSwagger();
It will do the trick
Upvotes: 0
Reputation: 1
In my version "@nestjsx/crud": "^5.0.0-alpha.3"
import { Swagger } from '@nestjsx/crud/lib/crud';
...
constructor() {
const metadata = Swagger.getParams(this.getManyProjects);
const queryParamsMeta = Swagger.createQueryParamsMeta('getManyBase',{
model: { type: MyModel },
query: {
softDelete: false,
},
});
Swagger.setParams([...metadata, ...queryParamsMeta], this.getManyProjects);
}
Upvotes: 0
Reputation: 1
<div class="s-prose js-post-body" itemprop="text">
<p>In my version "@nestjsx/crud": "^5.0.0-alpha.3" <a href="https://github.com/nestjsx/crud/blob/master/packages/crud/src/crud/swagger.helper.ts#L484" rel="nofollow noreferrer">nestjsx/crud</a> repo. </p>
<p>If you add something like this to your constructor that should do it:</p>
<pre style="position: relative;"><code>import { Swagger } from '@nestjsx/crud/lib/crud';
...
constructor() {
const metadata = Swagger.getParams(this.getManyProjects);
const queryParamsMeta = Swagger.createQueryParamsMeta('getManyBase',{
model: { type: MyModel },
query: {
softDelete: false,
},
});
Swagger.setParams([...metadata, ...queryParamsMeta], this.getManyProjects);
}
</code><div class="open_grepper_editor" title="Edit & Save To Grepper"></div></pre>
</div>
Upvotes: 0
Reputation: 1624
See this area in the nestjsx/crud repo.
If you add something like this to your constructor that should do it:
import { Swagger } from '@nestjsx/crud/lib/crud';
...
constructor() {
const metadata = Swagger.getParams(this.getManyProjects);
const queryParamsMeta = Swagger.createQueryParamsMeta('getManyBase');
Swagger.setParams([...metadata, ...queryParamsMeta], this.getManyProjects);
}
Upvotes: 1