#How to dynamically type @ApiProperty({type: ()=>T})
12 messages · Page 1 of 1 (latest)
You can’t, basically
You would have to know exactly all the shapes for that data field
Well, you technically can, through a function that returns a concrete class definition
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
I meant you can do it with types only
But you can do this: https://aalonso.dev/blog/how-to-generate-generics-dtos-with-nestjsswagger-422g
Thank you for the feedback. Unfortunately, I'm not able to get it working. Getting this error row new Error(A circular dependency has been detected (property key: "${key}"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").);
^
Error: A circular dependency has been detected (property key: "data"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
Somehow, it's working now. This is the final implementation.
export const ApiResponseConfig = <TModel extends Type<any>>(model: TModel, status: number) =>{ return applyDecorators( ApiExtraModels(ApiResponseModel, model), ApiResponse({ status: status ?? 200, schema: { allOf: [ { $ref: getSchemaPath(ApiResponseModel) }, { properties: { data: { type: 'array', items: { $ref: getSchemaPath(model) }, }, }, }, ], }, }) )}
`export class ApiResponseModel<T>{
// data?: T;
@ApiProperty()
status?: string
@ApiProperty()
success: boolean
@ApiProperty()
message: string
@ApiProperty()
error?: Boolean
}`
@ApiResponseConfig(GetWalletModel, 201)