posva / mande

<700 bytes convenient and modern wrapper around fetch with smart extensible defaults
https://mande.esm.is
MIT License
1.19k stars 42 forks source link

[Feature Request] Support JSON.stringify replacer function #382

Closed michaelnwani closed 1 year ago

michaelnwani commented 1 year ago

Firstly, thank you for making mande and continuing to support it 🙂 this is the best fetch library available. I really appreciate it.

My proposal here is to add a config option for dev's to pass a replacer function to the JSON.stringify that is used when making API calls. This would really help when resolving cyclic reference issues.

Example inspired from MDN:

const replacer = () => {
  const seen = new WeakSet();
  return (key, value) => {
    if (typeof value === "object" && value !== null) {
      if (seen.has(value)) {
        return;
      }
      seen.add(value);
    }
    return value;
  };
};

JSON.stringify(circularReference, replacer());

Maybe we can set it during the mande initialization, so it would look something like:

import { mande } from 'mande'
const users = mande('/api/users',{replacer})
posva commented 1 year ago

Maybe even allow a custom JSON.sringify()