sindresorhus / ky

🌳 Tiny & elegant JavaScript HTTP client based on the Fetch API
MIT License
13.61k stars 364 forks source link

feat: add support for `stringifyJson` #579

Closed Reverier-Xu closed 4 months ago

Reverier-Xu commented 5 months ago

When sending an HTTP request, this option allows the user to specify how the request data should be serialized as a string.

For example, it will be useful when users want to change the default serialization behavior using JSON.stringify(_, replacer).

import ky from 'ky';
import { DateTime } from 'luxon';

const json = await ky('https://example.com', {
  stringifyJson: data => JSON.stringify(data, (key, value) => {
    if (key.endsWith('_at')) {
      return DateTime.fromISO(value).toSeconds();
    }
    return value;
  })
}).json();
Reverier-Xu commented 5 months ago

Done. 😉