Unified DTOs for Clients and Servers
Basketry recently improved how Data Transfer Objects (DTOs) are handled across both Express and HTTP client generators. This upgrade simplifies your project by centralizing DTO definitions and enhancing data validation through Zod.
This article will walk you through what changed and how to leverage these improvements in your projects.
Why the change?
Previously, DTO generation was embedded directly in the Express generator, causing inconsistencies between client and server implementations. Additionally, the HTTP client generator lacked formal DTOs or mappers, resulting in bugs for certain clients. That generation has now been decomposed into a new dedicated package, @basketry/typescript-dtos
, and referenced in both client and server generators, ensuring consistent DTO definitions across your application.
This update also improves handling of property casing between DTOs and internal types, significantly reducing integration issues. Moreover, the HTTP client generator now leverages Zod for validation, replacing the deprecated validators previously used. Zod provides powerful, widely-adopted validation that can be used directly in client libraries, ensuring your frontend applications maintain robust data integrity and prevents the need to hand-write validation logic.
By adopting these improvements, your Basketry projects benefit from more consistent implementations, reduced debugging time, and easier maintainability.
Upgrading Your Project
Upgrade dependencies
To upgrade, first install the necessary dependencies:
- Server
- Client
- npm
- yarn
- pnpm
npm install @basketry/typescript-dtos @basketry/express@latest
yarn add @basketry/typescript-dtos @basketry/express@latest
pnpm add @basketry/typescript-dtos @basketry/express@latest
- npm
- yarn
- pnpm
npm install @basketry/typescript-dtos @basketry/typescript-http-client@latest
yarn add @basketry/typescript-dtos @basketry/typescript-http-client@latest
pnpm add @basketry/typescript-dtos @basketry/typescript-http-client@latest
Update your configuration
Next, update your Basketry configuration. Previously, your configuration looked like this:
- Server
- Client
{
"generators": [
"@basketry/express",
"@basketry/typescript",
"@basketry/typescript-dtos",
"@basketry/zod"
],
"options": {
"express": { "validation": "zod" }
}
}
{
"generators": [
"@basketry/typescript",
"@basketry/typescript-dtos",
"@basketry/typescript-http-client",
"@basketry/zod"
],
"options": {
"httpClient": { "validation": "zod" }
}
}
Your updated configuration should now include the @basketry/typescript-dtos generator and specify the DTO role:
- Server
- Client
{
"generators": [
"@basketry/express",
"@basketry/typescript",
"@basketry/typescript-dtos",
"@basketry/zod"
],
"options": {
"dtos": { "role": "server" },
"express": { "validation": "zod" }
}
}
{
"generators": [
"@basketry/typescript",
"@basketry/typescript-dtos",
"@basketry/typescript-http-client",
"@basketry/zod"
],
"options": {
"dtos": { "role": "client" },
"httpClient": { "validation": "zod" }
}
}
Regenerate your code
Finally, regenerate your Basketry code. This will create the new DTOs and update your generated client and/or server code to use them:
- npm
- yarn
- pnpm
npx basketry
yarn dlx basketry
pnpm dlx basketry
This upgrade provides immediate benefits, ensuring consistent client-server interactions, reducing bugs, and stabilizing client implementations. Enhance your project's reliability and maintainability by adopting these improvements today.
This article was hastily written by a human. Editing and proofreading were performed with the assistance of one or more large language models.