next-fetch icon indicating copy to clipboard operation
next-fetch copied to clipboard

Allow defining an endpoint with client-side response validation

Open Schniz opened this issue 3 years ago • 1 comments

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 avatar Jun 30 '22 08:06 Schniz

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)

Schniz avatar Aug 11 '22 08:08 Schniz