Comparing version 1.2.4 to 1.3.0
@@ -38,2 +38,3 @@ export const supportsRequestStreams = (() => { | ||
parseJson: true, | ||
stringifyJson: true, | ||
searchParams: true, | ||
@@ -40,0 +41,0 @@ prefixUrl: true, |
@@ -172,3 +172,3 @@ import { HTTPError } from '../errors/HTTPError.js'; | ||
if (this._options.json !== undefined) { | ||
this._options.body = JSON.stringify(this._options.json); | ||
this._options.body = this._options.stringifyJson?.(this._options.json) ?? JSON.stringify(this._options.json); | ||
this.request.headers.set('content-type', this._options.headers.get('content-type') ?? 'application/json'); | ||
@@ -175,0 +175,0 @@ this.request = new globalThis.Request(this.request, { body: this._options.body }); |
@@ -48,2 +48,27 @@ import type { LiteralUnion, Required } from './common.js'; | ||
/** | ||
User-defined JSON-stringifying function. | ||
Use-cases: | ||
1. Stringify JSON with a custom `replacer` function. | ||
@default JSON.stringify() | ||
@example | ||
``` | ||
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(); | ||
``` | ||
*/ | ||
stringifyJson?: (data: unknown) => string; | ||
/** | ||
Search parameters to include in the request URL. Setting this will override all existing search parameters in the input URL. | ||
@@ -50,0 +75,0 @@ |
{ | ||
"name": "ky", | ||
"version": "1.2.4", | ||
"version": "1.3.0", | ||
"description": "Tiny and elegant HTTP client based on the browser Fetch API", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -158,2 +158,4 @@ <div align="center"> | ||
In addition to all the [`fetch` options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options), it supports these options: | ||
##### method | ||
@@ -430,2 +432,27 @@ | ||
##### stringifyJson | ||
Type: `Function`\ | ||
Default: `JSON.stringify()` | ||
User-defined JSON-stringifying function. | ||
Use-cases: | ||
1. Stringify JSON with a custom `replacer` function. | ||
```js | ||
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(); | ||
``` | ||
##### fetch | ||
@@ -432,0 +459,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
149425
1169
728