Skip to content
Discussion options

You must be logged in to vote

Request objects are immutable and while they don't have a body property, they do have various methods for reading the body.

Ky allows you to return a new request from a beforeRequest hook. So something like this should work...

import snakecaseKeys from 'snakecase-keys';
import ky from 'ky';

const api = ky.extend({
    hooks : {
        beforeRequest : [
            async (request, options) => {
                const json = await request.json();
                const snaked = snakecaseKeys(json);
                return new Request(request.url, {
                    ...options,
                    body : snaked
                });
            }
        ]
    }
});

You could further improve…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@finnbuga
Comment options

@finnbuga
Comment options

@sholladay
Comment options

Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants