vercel-labs / next-fetch

Intuitive data fetching for Next.js
https://next-fetch-pi.vercel.app/
592 stars 14 forks source link

Allow defining an endpoint with client-side response validation #3

Open Schniz opened 2 years ago

Schniz commented 2 years ago

The idea behind this feature is to enable long-running clients to react to changes in the backend schemas. Imagine you have an endpoint that returns { version: 1, data: string } and then while the customer is on your app, the server redeploys and now it returns { version: 2, result: string }. You probably want to make a hard-refresh, or at least show an error.

The suggested API would be something like:

import { query } from 'next-swr-endpoints';
export const useNoClientTypecheck = query(myParser, (value) => ...)
export const useWithClientTypecheck = query({ input: myParser, output: anotherParser }, value => ...)
Schniz commented 2 years ago

This can also be handy for stuff like passing Date objects, if the parser is symmetric (Parser<From, To> = { parse(from: From): To, encode(to: To): From }), but Zod unfortunately does not support that syntax AFAIK (while io-ts does)