flightcontrolhq / superjson

Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.
https://www.flightcontrol.dev?ref=superjson
MIT License
4.01k stars 87 forks source link

Trying to use it with ofetch #278

Closed digoburigo closed 10 months ago

digoburigo commented 10 months ago

I'm trying to use superjson with ofetch Is there something specific to make superjson work with this fetch alternative?

const ofetchOptions: FetchOptions = {
  baseURL: 'https://jsonplaceholder.typicode.com',
  parseResponse: superjson.parse, // this don't work
};
const api = ofetch.create(ofetchOptions);

Already open an issue in their repo.

Skn0tt commented 10 months ago

Hey @digoburigo! You're holding it wrong. The typicode API doesn't return SuperJSON, but normal JSON. That's why SuperJSON will return undefined.

What are you trying to achieve? If the goal is to parse the incoming JSON, use parseResponse: JSON.parse instead.

digoburigo commented 10 months ago

Yes. I tried doing something that it's not possible. That is returning a superjson from a normal json response. That is not the purpose of this library. Sorry for the confusion.