Open utterances-bot opened 2 years ago
that's what I call clean code!!
beautiful code, thank you very much!
Seems like it doesn't work with the newest version of nestjs/swagger. I got an error:
TypeError: Cannot destructure property 'prototype' of 'type' as it is undefined.
I also found a question with the same problem. Any ideas?
Extremely cleaver! Before see your post, I never think about abstract DTO because I thought it was difficult to generate open API documentation. But create a custom decorator to generate it is an excellent idea!
Wonderful! Thanks.
I had an issue with the swagger docs not recognising UserDto as a model so "data" was empty in the docs example response.
If anyone has the same issue, just add "model" to the ApiExtraModels in the decorator, i.e.
...
return applyDecorators(
ApiExtraModels(PageDto, model),
ApiOkResponse({
description: "Successfully received model list",
...
amazing clean code. thanks for sharing.
When i console.log(pageOptionsDto.skip)
it just return undefined
. Idk why this happening
.skip(pageOptionsDto.skip)
@TraLeeee make sure you have entry in your bootstrap file as below.
app.useGlobalPipes(new ValidationPipe({ transform: true }));
For full overview check the tutorial again to find a proper place to do that.
@Jarzebowsky I figured out that I need to expose that skip method using @Expose() decorator.
And thanks for your reply, at the moment I got this error. My main.ts
already add app.useGlobalPipes(new ValidationPipe({ transform: true }));
.
@TraLeeee if you need any assistance regarding an issue, please just share a repo with me (if possible) so I can help out.
Great one! Thank you!
import { applyDecorators, Type } from '@nestjs/common';
import { ApiExtraModels, ApiOkResponse, getSchemaPath } from '@nestjs/swagger';
import { PaginationAble } from '../dto';
export const ApiPaginatedResponse = <TModel extends Type<any>>(
model: TModel,
) => {
return applyDecorators(
ApiExtraModels(PaginationAble, model),
// ^^^^^
ApiOkResponse({
description: 'Successfully received model list',
schema: {
allOf: [
{ $ref: getSchemaPath(PaginationAble<TModel>) },
{
properties: {
data: {
type: 'array',
items: { $ref: getSchemaPath(model) },
},
},
},
],
},
}),
);
};
@pietrzakadrian that's a great tutorial, thanks a lot for your work.
suggestion: in e.g. nest.js docs, when tutorial is made by building some feature, it really helps when file names / files structure are given on top of code snippets. It really helps to understand the structure and makes it much easier to follow the tutorial.
Thanks again, very useful post :+1:
How to create pagination in Nest.js with TypeORM + Swagger - Software Engineering Blog | Adrian Pietrzak
Adrian Pietrzak is a software engineer from Poland. He mostly works with Node.js, React and React Native.
https://pietrzakadrian.com/blog/how-to-create-pagination-in-nestjs-with-typeorm-swagger