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

feat: add `stringify` option #399

Closed masterHAWK99 closed 1 year ago

masterHAWK99 commented 1 year ago

What kind of change does this PR introduce? (check at least one)

Does this PR introduce a breaking change? (check one)

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

Other information:

Close #382

masterHAWK99 commented 1 year ago

Thanks! Let's instead add a new option stringify that defaults to JSON.stringify. That way no breaking change

What do you mean? Changing the replacer parameter to stringify?

I came up with something like this:

export function mande(
  baseURL: string,
  stringify: (data: any) => string = JSON.stringify,
  passedInstanceOptions: OptionsRaw = {},
  fetchPolyfill?: Window['fetch']
): MandeInstance {
posva commented 1 year ago

No, let's keep the same arguments (non breaking) and add a new property to the options

masterHAWK99 commented 1 year ago

Like that?

function _fetch(...) {
//...
if (method[0] === 'P' && data)
      mergedOptions.body = passedInstanceOptions.stringify
        ? passedInstanceOptions.stringify(data)
        : JSON.stringify(data)
}
export interface OptionsRaw<R extends ResponseAsTypes = ResponseAsTypes>
  extends Omit<Options<R>, 'headers' | 'signal'> {
 //...
  stringify?: (data: any) => string
}
posva commented 1 year ago

Yes

posva commented 1 year ago

Thanks, I couldn't push so I merged locally