Comparing version 0.21.0 to 0.22.0
@@ -210,2 +210,25 @@ /// <reference lib="dom"/> | ||
/** | ||
User-defined JSON-parsing function. | ||
Use-cases: | ||
1. Parse JSON via the [`bourne` package](https://github.com/hapijs/bourne) to protect from prototype pollution. | ||
2. Parse JSON with [`reviver` option of `JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). | ||
@default JSON.parse() | ||
@example | ||
``` | ||
import ky from 'ky'; | ||
import bourne from '@hapijs/bourne'; | ||
(async () => { | ||
const parsed = await ky('https://example.com', { | ||
parseJson: text => bourne(text) | ||
}).json(); | ||
})(); | ||
``` | ||
*/ | ||
parseJson?: (text: string) => unknown | ||
/** | ||
Search parameters to include in the request URL. Setting this will override all existing search parameters in the input URL. | ||
@@ -212,0 +235,0 @@ |
28
index.js
@@ -52,5 +52,5 @@ /*! MIT License © Sindre Sorhus */ | ||
const mergeHeaders = (source1, source2) => { | ||
const result = new globals.Headers(source1); | ||
const result = new globals.Headers(source1 || {}); | ||
const isHeadersInstance = source2 instanceof globals.Headers; | ||
const source = new globals.Headers(source2); | ||
const source = new globals.Headers(source2 || {}); | ||
@@ -276,4 +276,4 @@ for (const [key, value] of source) { | ||
if (this._options.searchParams) { | ||
const url = new URL(this.request.url); | ||
url.search = new URLSearchParams(this._options.searchParams); | ||
const searchParams = '?' + new URLSearchParams(this._options.searchParams).toString(); | ||
const url = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, searchParams); | ||
@@ -333,2 +333,8 @@ // To provide correct form boundary, Content-Type header should be deleted each time when new Request instantiated from another one | ||
if (this._options.parseJson) { | ||
response.json = async () => { | ||
return this._options.parseJson(await response.text()); | ||
}; | ||
} | ||
return response; | ||
@@ -343,4 +349,16 @@ }; | ||
this.request.headers.set('accept', this.request.headers.get('accept') || mimeType); | ||
const response = (await result).clone(); | ||
return (type === 'json' && response.status === 204) ? '' : response[type](); | ||
if (type === 'json') { | ||
if (response.status === 204) { | ||
return ''; | ||
} | ||
if (options.parseJson) { | ||
return options.parseJson(await response.text()); | ||
} | ||
} | ||
return response[type](); | ||
}; | ||
@@ -347,0 +365,0 @@ } |
{ | ||
"name": "ky", | ||
"version": "0.21.0", | ||
"version": "0.22.0", | ||
"description": "Tiny and elegant HTTP client based on the browser Fetch API", | ||
@@ -61,5 +61,5 @@ "license": "MIT", | ||
"nyc": "^15.0.0", | ||
"puppeteer": "^3.0.4", | ||
"puppeteer": "^5.1.0", | ||
"rollup": "^2.10.2", | ||
"tsd": "^0.11.0", | ||
"tsd": "^0.13.1", | ||
"xo": "^0.25.3" | ||
@@ -66,0 +66,0 @@ }, |
@@ -349,2 +349,24 @@ <div align="center"> | ||
##### parseJson | ||
Type: `Function`\ | ||
Default: `JSON.parse()` | ||
User-defined JSON-parsing function. | ||
Use-cases: | ||
1. Parse JSON via the [`bourne` package](https://github.com/hapijs/bourne) to protect from prototype pollution. | ||
2. Parse JSON with [`reviver` option of `JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). | ||
```js | ||
import ky from 'ky'; | ||
import bourne from '@hapijs/bourne'; | ||
(async () => { | ||
const parsed = await ky('https://example.com', { | ||
parseJson: text => bourne(text) | ||
}).json(); | ||
})(); | ||
``` | ||
### ky.extend(defaultOptions) | ||
@@ -351,0 +373,0 @@ |
28
umd.js
@@ -58,5 +58,5 @@ (function (global, factory) { | ||
const mergeHeaders = (source1, source2) => { | ||
const result = new globals.Headers(source1); | ||
const result = new globals.Headers(source1 || {}); | ||
const isHeadersInstance = source2 instanceof globals.Headers; | ||
const source = new globals.Headers(source2); | ||
const source = new globals.Headers(source2 || {}); | ||
@@ -282,4 +282,4 @@ for (const [key, value] of source) { | ||
if (this._options.searchParams) { | ||
const url = new URL(this.request.url); | ||
url.search = new URLSearchParams(this._options.searchParams); | ||
const searchParams = '?' + new URLSearchParams(this._options.searchParams).toString(); | ||
const url = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, searchParams); | ||
@@ -339,2 +339,8 @@ // To provide correct form boundary, Content-Type header should be deleted each time when new Request instantiated from another one | ||
if (this._options.parseJson) { | ||
response.json = async () => { | ||
return this._options.parseJson(await response.text()); | ||
}; | ||
} | ||
return response; | ||
@@ -349,4 +355,16 @@ }; | ||
this.request.headers.set('accept', this.request.headers.get('accept') || mimeType); | ||
const response = (await result).clone(); | ||
return (type === 'json' && response.status === 204) ? '' : response[type](); | ||
if (type === 'json') { | ||
if (response.status === 204) { | ||
return ''; | ||
} | ||
if (options.parseJson) { | ||
return options.parseJson(await response.text()); | ||
} | ||
} | ||
return response[type](); | ||
}; | ||
@@ -353,0 +371,0 @@ } |
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
64199
1265
612