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
Provenance
- Commit
2eea67e- Tree
638043d- Author
Reverier Xu <reverier.xu@outlook.com>- Committer
GitHub <noreply@github.com>- Parent
2af72bf
5 files changed+73 added−1 removed
modified:source/core/constants.ts+1 added
48 unmodified lines49505152535455565724 unmodified lines48 unmodified lines
export const kyOptionKeys: KyOptionsRegistry = { json: true, parseJson: true, stringifyJson: true, searchParams: true, prefixUrl: true, retry: true, timeout: true,24 unmodified linesmodified:source/core/Ky.ts+1 added-1 removed
201 unmodified lines202 this.request = new globalThis.Request(new globalThis.Request(url, {...this.request}), this._options as RequestInit);203 }204 205 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 linesmodified:source/types/options.ts+26 added
57 unmodified lines58 */59 parseJson?: (text: string) => unknown;60 61 /**62+ User-defined JSON-stringifying function.63+64+ Use-cases:65+ 1. Stringify JSON with a custom `replacer` function.66+67+ @default JSON.stringify()68+69+ @example70+ ```71+ import ky from 'ky';72+ import {DateTime} from 'luxon';73+74+ 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+ }79+80+ return value;81+ })82+ }).json();83+ ```84+ */85+ stringifyJson?: (data: unknown) => string;86+87+ /**88 Search parameters to include in the request URL. Setting this will override all existing search parameters in the input URL.89 90 Accepts any value supported by [`URLSearchParams()`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams).91 */199 unmodified linesmodified:test/main.ts+20 added
731 unmodified lines732 });733 734 await server.close();735 });736+737+test('stringifyJson option with request.json()', async t => {738+ const server = await createHttpTestServer({bodyParser: false});739+740+ const json = {hello: 'world'};741+ const extra = 'extraValue';742+743+ server.post('/', async (request, response) => {744+ const body = await parseRawBody(request);745+ t.is(body, JSON.stringify({data: json, extra}));746+ response.end();747+ });748+749+ await ky.post(server.url, {750+ stringifyJson: data => JSON.stringify({data, extra}),751+ json,752+ });753+754+ await server.close();755+});modified:readme.md+25 added
426 unmodified lines427 parseJson: text => bourne(text)428 }).json();429 ```430 431+##### stringifyJson432+433+Type: `Function`\434+Default: `JSON.stringify()`435+436+User-defined JSON-stringifying function.437+438+Use-cases:439+1. Stringify JSON with a custom `replacer` function.440+441+```js442+import ky from 'ky';443+import {DateTime} from 'luxon';444+445+const 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+ }450+451+ return value;452+ })453+}).json();454+```455+456 ##### fetch457 458 Type: `Function`\459 Default: `fetch`268 unmodified lines