Comparing version 0.19.1 to 0.20.0
@@ -17,7 +17,7 @@ /// <reference lib="dom"/> | ||
export type BeforeRetryHook = (options: { | ||
request: Request, | ||
response: Response, | ||
options: NormalizedOptions, | ||
error: Error, | ||
retryCount: number, | ||
request: Request; | ||
response: Response; | ||
options: NormalizedOptions; | ||
error: Error; | ||
retryCount: number; | ||
}) => void | Promise<void>; | ||
@@ -158,3 +158,3 @@ | ||
*/ | ||
export interface Options extends RequestInit { | ||
export interface Options extends Omit<RequestInit, 'headers'> { | ||
/** | ||
@@ -168,2 +168,39 @@ HTTP method used to make the request. | ||
/** | ||
HTTP headers used to make the request. | ||
You can pass a `Headers` instance or a plain object. | ||
You can remove a header with `.extend()` by passing the header with an `undefined` value. | ||
@example | ||
``` | ||
import ky from 'ky'; | ||
const url = 'https://sindresorhus.com'; | ||
const original = ky.create({ | ||
headers: { | ||
rainbow: 'rainbow', | ||
unicorn: 'unicorn' | ||
} | ||
}); | ||
const extended = original.extend({ | ||
headers: { | ||
rainbow: undefined | ||
} | ||
}); | ||
const response = await extended(url).json(); | ||
console.log('rainbow' in response); | ||
//=> false | ||
console.log('unicorn' in response); | ||
//=> true | ||
``` | ||
*/ | ||
headers?: HeadersInit | {[key: string]: undefined}; | ||
/** | ||
Shortcut for sending JSON. Use this instead of the `body` option. | ||
@@ -298,7 +335,7 @@ | ||
export interface ResponsePromise extends Promise<Response> { | ||
arrayBuffer(): Promise<ArrayBuffer>; | ||
arrayBuffer: () => Promise<ArrayBuffer>; | ||
blob(): Promise<Blob>; | ||
blob: () => Promise<Blob>; | ||
formData(): Promise<FormData>; | ||
formData: () => Promise<FormData>; | ||
@@ -328,5 +365,5 @@ // TODO: Use `json<T extends JSONValue>(): Promise<T>;` when it's fixed in TS. | ||
*/ | ||
json<T>(): Promise<T>; | ||
json: <T>() => Promise<T>; | ||
text(): Promise<string>; | ||
text: () => Promise<string>; | ||
} | ||
@@ -376,3 +413,3 @@ | ||
*/ | ||
get(url: Input, options?: Options): ResponsePromise; | ||
get: (url: Input, options?: Options) => ResponsePromise; | ||
@@ -385,3 +422,3 @@ /** | ||
*/ | ||
post(url: Input, options?: Options): ResponsePromise; | ||
post: (url: Input, options?: Options) => ResponsePromise; | ||
@@ -394,3 +431,3 @@ /** | ||
*/ | ||
put(url: Input, options?: Options): ResponsePromise; | ||
put: (url: Input, options?: Options) => ResponsePromise; | ||
@@ -403,3 +440,3 @@ /** | ||
*/ | ||
delete(url: Input, options?: Options): ResponsePromise; | ||
delete: (url: Input, options?: Options) => ResponsePromise; | ||
@@ -412,3 +449,3 @@ /** | ||
*/ | ||
patch(url: Input, options?: Options): ResponsePromise; | ||
patch: (url: Input, options?: Options) => ResponsePromise; | ||
@@ -421,3 +458,3 @@ /** | ||
*/ | ||
head(url: Input, options?: Options): ResponsePromise; | ||
head: (url: Input, options?: Options) => ResponsePromise; | ||
@@ -429,3 +466,3 @@ /** | ||
*/ | ||
create(defaultOptions: Options): typeof ky; | ||
create: (defaultOptions: Options) => typeof ky; | ||
@@ -439,3 +476,3 @@ /** | ||
*/ | ||
extend(defaultOptions: Options): typeof ky; | ||
extend: (defaultOptions: Options) => typeof ky; | ||
@@ -472,6 +509,6 @@ /** | ||
declare namespace ky { | ||
export type TimeoutError = InstanceType<typeof ky.TimeoutError>; | ||
export type HTTPError = InstanceType<typeof ky.HTTPError>; | ||
export type TimeoutError = InstanceType<typeof TimeoutError>; | ||
export type HTTPError = InstanceType<typeof HTTPError>; | ||
} | ||
export default ky; |
24
index.js
@@ -51,4 +51,21 @@ /*! MIT License © Sindre Sorhus */ | ||
const mergeHeaders = (source1, source2) => { | ||
const result = new globals.Headers(source1); | ||
const isHeadersInstance = source2 instanceof globals.Headers; | ||
const source = new globals.Headers(source2); | ||
for (const [key, value] of source) { | ||
if ((isHeadersInstance && value === 'undefined') || value === undefined) { | ||
result.delete(key); | ||
} else { | ||
result.set(key, value); | ||
} | ||
} | ||
return result; | ||
}; | ||
const deepMerge = (...sources) => { | ||
let returnValue = {}; | ||
let headers = {}; | ||
@@ -70,3 +87,9 @@ for (const source of sources) { | ||
} | ||
if (isObject(source.headers)) { | ||
headers = mergeHeaders(headers, source.headers); | ||
} | ||
} | ||
returnValue.headers = headers; | ||
} | ||
@@ -210,2 +233,3 @@ | ||
...options, | ||
headers: mergeHeaders(this._input.headers, options.headers), | ||
hooks: deepMerge({ | ||
@@ -212,0 +236,0 @@ beforeRequest: [], |
{ | ||
"name": "ky", | ||
"version": "0.19.1", | ||
"version": "0.20.0", | ||
"description": "Tiny and elegant HTTP client based on the browser Fetch API", | ||
@@ -11,3 +11,3 @@ "license": "MIT", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
@@ -62,4 +62,4 @@ "engines": { | ||
"nyc": "^15.0.0", | ||
"puppeteer": "^2.1.0", | ||
"rollup": "^1.31.0", | ||
"puppeteer": "^3.0.4", | ||
"rollup": "^2.10.2", | ||
"tsd": "^0.11.0", | ||
@@ -66,0 +66,0 @@ "xo": "^0.25.3" |
@@ -130,3 +130,3 @@ <div align="center"> | ||
Type: `string`\ | ||
Default: `get` | ||
Default: `'get'` | ||
@@ -356,2 +356,34 @@ HTTP method used to make the request. | ||
You can pass headers as a `Headers` instance or a plain object. | ||
You can remove a header with `.extend()` by passing the header with an `undefined` value. | ||
Passing `undefined` as a string removes the header only if it comes from a `Headers` instance. | ||
```js | ||
import ky from 'ky'; | ||
const url = 'https://sindresorhus.com'; | ||
const original = ky.create({ | ||
headers: { | ||
rainbow: 'rainbow', | ||
unicorn: 'unicorn' | ||
} | ||
}); | ||
const extended = original.extend({ | ||
headers: { | ||
rainbow: undefined | ||
} | ||
}); | ||
const response = await extended(url).json(); | ||
console.log('rainbow' in response); | ||
//=> false | ||
console.log('unicorn' in response); | ||
//=> true | ||
``` | ||
### ky.create(defaultOptions) | ||
@@ -358,0 +390,0 @@ |
24
umd.js
@@ -57,4 +57,21 @@ (function (global, factory) { | ||
const mergeHeaders = (source1, source2) => { | ||
const result = new globals.Headers(source1); | ||
const isHeadersInstance = source2 instanceof globals.Headers; | ||
const source = new globals.Headers(source2); | ||
for (const [key, value] of source) { | ||
if ((isHeadersInstance && value === 'undefined') || value === undefined) { | ||
result.delete(key); | ||
} else { | ||
result.set(key, value); | ||
} | ||
} | ||
return result; | ||
}; | ||
const deepMerge = (...sources) => { | ||
let returnValue = {}; | ||
let headers = {}; | ||
@@ -76,3 +93,9 @@ for (const source of sources) { | ||
} | ||
if (isObject(source.headers)) { | ||
headers = mergeHeaders(headers, source.headers); | ||
} | ||
} | ||
returnValue.headers = headers; | ||
} | ||
@@ -216,2 +239,3 @@ | ||
...options, | ||
headers: mergeHeaders(this._input.headers, options.headers), | ||
hooks: deepMerge({ | ||
@@ -218,0 +242,0 @@ beforeRequest: [], |
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
62242
1219
590