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

Allow defining an endpoint without a server-side runtime parsing

Open Schniz opened this issue 3 years ago • 1 comments
trafficstars

When I asked for feedback, one person told me he does not like the fact this library forces you to declare a type in advance using zod, alternative libraries, or pass any object with the function parse.

So their idea was:

export const useMyHook = query((value: { untrusted: string }) => {
  return "Hello, world!"
});

I am not so sure about it because I think we should have good defaults and suggest better and safer code. Maybe we can do something like:

import { unsafe } from 'next-swr-endpoints';

export const useMyHook = query(unsafe, (value: { untrusted: string }) => {
 ...
}));

// or as a function, because then I think that we can declare it as `unsafe<T>(): T`
export const useMyHook = query(unsafe(), (value: { untrusted: string }) => {
 ...
}));

Schniz avatar Jun 30 '22 08:06 Schniz

+1 for not adding this.

If people really want this, they can add their own unsafe validator like this:

const unsafe = <T,>() => ({ parse: (v: unknown) => v as T })

export const useMyHook = query(unsafe<{ untrusted: string }>(), (value) => {
  ...
}));

export const useMyHook = query(unsafe<any>(), (value: { untrusted: string }) => {
  ...
}));

jguddas avatar Jul 08 '22 18:07 jguddas