Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for stringifyJson #579

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Reverier-Xu
Copy link

@Reverier-Xu Reverier-Xu commented Apr 21, 2024

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();

source/core/Ky.ts Outdated Show resolved Hide resolved
test/main.ts Outdated Show resolved Hide resolved
@Reverier-Xu
Copy link
Author

Done. 😉

test/main.ts Outdated Show resolved Hide resolved
test/main.ts Outdated Show resolved Hide resolved
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants