Skip to content
Myr

Add `stringifyJson` option (#579)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Reverier Xu · committed by GitHub · parent 2af72bf · Browse files · 5 files+73 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>

Diff stats

5 files+73 added−1 removed

  • tests:+20 added−0 removed
  • other:+53 added−1 removed

5 files+73 added−1 removed

modified:source/core/constants.ts+1 added
modified:source/core/Ky.ts+1 added-1 removed
201 unmodified lines
202 this.request = new globalThis.Request(new globalThis.Request(url, {...this.request}), this._options as RequestInit);203 }204205 if (this._options.json !== undefined) {206 this._options.body = JSON.stringify(this._options.json);206 this._options.body = this._options.stringifyJson?.(this._options.json) ?? JSON.stringify(this._options.json);207 this.request.headers.set('content-type', this._options.headers.get('content-type') ?? 'application/json');208 this.request = new globalThis.Request(this.request, {body: this._options.body});209 }210 }
157 unmodified lines
modified:source/types/options.ts+26 added
57 unmodified lines
58 */59 parseJson?: (text: string) => unknown;6061 /**62 User-defined JSON-stringifying function.6364 Use-cases:65 1. Stringify JSON with a custom `replacer` function.6667 @default JSON.stringify()6869 @example70 ```71 import ky from 'ky';72 import {DateTime} from 'luxon';7374 const json = await ky('https://example.com', {75 stringifyJson: data => JSON.stringify(data, (key, value) => {76 if (key.endsWith('_at')) {77 return DateTime.fromISO(value).toSeconds();78 }7980 return value;81 })82 }).json();83 ```84 */85 stringifyJson?: (data: unknown) => string;8687 /**88 Search parameters to include in the request URL. Setting this will override all existing search parameters in the input URL.8990 Accepts any value supported by [`URLSearchParams()`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams).91 */
199 unmodified lines
modified:test/main.ts+20 added
731 unmodified lines
732 });733734 await server.close();735});736737test('stringifyJson option with request.json()', async t => {738 const server = await createHttpTestServer({bodyParser: false});739740 const json = {hello: 'world'};741 const extra = 'extraValue';742743 server.post('/', async (request, response) => {744 const body = await parseRawBody(request);745 t.is(body, JSON.stringify({data: json, extra}));746 response.end();747 });748749 await ky.post(server.url, {750 stringifyJson: data => JSON.stringify({data, extra}),751 json,752 });753754 await server.close();755});
modified:readme.md+25 added
426 unmodified lines
427 parseJson: text => bourne(text)428}).json();429```430431##### stringifyJson432433Type: `Function`\434Default: `JSON.stringify()`435436User-defined JSON-stringifying function.437438Use-cases:4391. Stringify JSON with a custom `replacer` function.440441```js442import ky from 'ky';443import {DateTime} from 'luxon';444445const json = await ky('https://example.com', {446 stringifyJson: data => JSON.stringify(data, (key, value) => {447 if (key.endsWith('_at')) {448 return DateTime.fromISO(value).toSeconds();449 }450451 return value;452 })453}).json();454```455456##### fetch457458Type: `Function`\459Default: `fetch`
268 unmodified lines