Best Practices for Sharing DTOs Between Client and Server

0
5
Asked By CuriousCoder42 On

I'm working on a project using Angular on the client side and Nest.js on the server, and I'm focusing on how to effectively share Data Transfer Objects (DTOs) between them. My goal is to maintain a single file for each entity that can be used across both sides. I currently have about five or six different DTOs just for handling login and access. I'm not exposing any internal DTOs, only the ones needed for I/O operations. How can I efficiently manage and share these DTOs?

2 Answers

Answered By FrontendFanatic77 On

I’ve faced a similar situation but I'm using a Nest and React setup in a monorepo structure. I created a separate workspace for types and enums. Just keep in mind that for Nest to use enums correctly, you’ll have to set type to 'module' and make sure to build the files before they get accepted, even though you’re working with TypeScript. It’s a bit of a workaround but it works!

InputOutputGenius -

I need to type my inputs and outputs as well. Something like my_function(data: InputDataDTO): OutputDataDTO would be super helpful!

Answered By CodeMaster89 On

One approach is to create an OpenAPI specification for your project. This way, you can use a code generator to automatically generate the needed DTOs for both the client and server, which can save a lot of time in the long run.

DevNinja101 -

Make sure you properly annotate your API endpoints. It will help in generating client HTTP or fetch services too!

TechSavvy123 -

That sounds interesting! Can you share an example or a link to get started with that?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.