News for hackers. Stuff that matters.
Re:Modern Node.js Patterns by farkin88 on Sunday August 03, 2025 @06:29PM (#44780384)
Type safety for API calls is huge. I haven't used ts-rest but the compile-time validation approach sounds solid. Way better than runtime surprises. How's the experience in practice? Do you find the schema definition overhead worth it or does it feel heavy for simpler endpoints?
Re:Modern Node.js Patterns by _heimdall on Sunday August 03, 2025 @08:01PM (#44780917)
I always try to throw schema validation of some kind in API calls for any codebase I really need to be reliable.

For prototypes I'll sometimes reach for tRPC. I don't like the level of magic it adds for a production app, but it is really quick to prototype with and we all just use RPC calls anyway.

For procudtion I'm most comfortable with zod, but there are quite a few good options. I'll have a fetchApi or similar wrapper call that takes in the schema + fetch() params and validates the response.

Re:Modern Node.js Patterns by pnappa on Sunday August 03, 2025 @08:11PM (#44780968)
How do you supply the schema on the other side?

I found that keeping the frontend & backend in sync was a challenge so I wrote a script that reads the schemas from the backend and generated an API file in the frontend.

Re:Modern Node.js Patterns by koolba on Sunday August 03, 2025 @10:25PM (#44781585)
Write them both in TypeScript and have both the request and response shapes defined as schemas for each API endpoint.

The server validates request bodies and produces responses that match the type signature of the response schema.

The client code has an API where it takes the request body as its input shape. And the client can even validate the server responses to ensure they match the contract.

It’s pretty beautiful in practice as you make one change to the API to say rename a field, and you immediately get all the points of use flagged as type errors.

Re:Modern Node.js Patterns by jvuygbbkuurx on Sunday August 03, 2025 @11:17PM (#44781834)
This will break old clients. Having a deployment stategy taking that into account is important.
Re:Modern Node.js Patterns by _heimdall on Sunday August 03, 2025 @10:44PM (#44781666)
I'll almost always lean on separate packages for any shared logic like that (at least if I can use the same language on both ends).

For JS/TS, I'll have a shared models package that just defines the schemas and types for any requests and responses that both the backend and frontend are concerned with. I can also define migrations there if model migrations are needed for persistence or caching layers.

It takes a bit more effort, but I find it nicer to own the setup myself and know exactly how it works rather than trusting a tool to wire all that up for me, usually in some kind of build step or transpiration.

Re:Modern Node.js Patterns by exhaze on Sunday August 03, 2025 @11:05PM (#44781783)
There are a few ways, but I believe SSOT (single source of truth) is key, as others basically said. Some ways:

1. Shared TypeScript types

2. tRPC/ts-rest style: Automagic client w/ compile+runtime type safety

3. RTK (redux toolkit) query style: codegen'd frontend client

I personally I prefer #3 for its explicitness - you can actually review the code it generates for a new/changed endpoint. It does come w/ downside of more code + as codebase gets larger you start to need a cache to not regenerate the entire API every little change.

Overall, I find the explicit approach to be worth it, because, in my experience, it saves days/weeks of eng hours later on in large production codebases in terms of not chasing down server/client validation quirks.

Re:Modern Node.js Patterns by jedwards1211 on Sunday August 03, 2025 @11:28PM (#44781888)
What is a validation quirk that would happen when using server side Zod schemas that somehow doesn’t happen with a codegened client?
Re:Modern Node.js Patterns by jedwards1211 on Sunday August 03, 2025 @11:21PM (#44781851)
The schema definition is more efficient than writing input validation from scratch anyway so it’s completely win/win unless you want to throw caution to the wind and not do any validation
Re:Modern Node.js Patterns by strogonoff on Monday August 04, 2025 @03:08AM (#44782846)
Effect provides a pretty good engine for compile-time schema validation that can be composed with various fetching and processing pipelines, with sensible error handling for cases when external data fails to comply with the schema or when network request fails.