Within my NestJS environment, I have constructed a DTO object as follows:
export class Protocol {
public enabled?: boolean;
public allowed?: boolean;
constructor(enabled: boolean, allowed: boolean) { // With a necessary constructor
this.enabled = enabled;
this.allowed = allowed;
}
}
Now, my goal is to utilize this same Protocol within a RequestDTO for my PATCH request. This can be achieved like so:
In the Controller:
modifySomething(@Param('id') id: string, @Param('uuid') uuid: string, @Body() body: ModifyRequest) {
return this.service.modify(id, uuid, body);
}
Defined in my DTO (ModifyRequest
):
export class Protocol {
public enabled?: boolean;
public allowed?: boolean;
constructor(enabled: boolean, allowed: boolean) { // With a necessary constructor
this.enabled = enabled;
this.allowed = allowed;
}
}
export class ModifyRequest {
public name?: string;
public nfs?: Protocol;
}
The question at hand now becomes:
How do I accurately map the incoming request @Body to this DTO object? Here are some strategies I've explored prior to seeking assistance:
- If the constructor is removed, the mapping functions correctly. However, removing it is not an option due to other dependencies.
- I attempted to use
class-transformer
, implemented as follows:
import { Type } from 'class-transformer';
export class ModifyRequest {
public name?: string;
@Type(()=>Protocol)
public nfs?: Protocol;
}
Unfortunately, this approach did not yield the desired results.
Any guidance on achieving this dual use of Protocol
within both Response (which necessitates a constructor) and RequestDTO would be greatly appreciated.
nestjs