Comparing version 0.28.7 to 0.29.0
@@ -21,2 +21,3 @@ import { HTTPError } from '../errors/HTTPError.js'; | ||
beforeRetry: [], | ||
beforeError: [], | ||
afterResponse: [], | ||
@@ -94,3 +95,8 @@ }, options.hooks), | ||
if (!response.ok && ky._options.throwHttpErrors) { | ||
throw new HTTPError(response, ky.request, ky._options); | ||
let error = new HTTPError(response, ky.request, ky._options); | ||
for (const hook of ky._options.hooks.beforeError) { | ||
// eslint-disable-next-line no-await-in-loop | ||
error = await hook(error); | ||
} | ||
throw error; | ||
} | ||
@@ -97,0 +103,0 @@ // If `onDownloadProgress` is passed, it uses the stream API internally |
@@ -6,5 +6,5 @@ /*! MIT License © Sindre Sorhus */ | ||
export { Options, NormalizedOptions, RetryOptions, SearchParamsOption, DownloadProgress, } from './types/options.js'; | ||
export { Hooks, BeforeRequestHook, BeforeRetryHook, AfterResponseHook, } from './types/hooks.js'; | ||
export { Hooks, BeforeRequestHook, BeforeRetryHook, BeforeErrorHook, AfterResponseHook, } from './types/hooks.js'; | ||
export { ResponsePromise } from './types/response.js'; | ||
export { HTTPError } from './errors/HTTPError.js'; | ||
export { TimeoutError } from './errors/TimeoutError.js'; |
import { stop } from '../core/constants.js'; | ||
import { HTTPError } from '../index.js'; | ||
import type { NormalizedOptions } from './options.js'; | ||
@@ -12,2 +13,3 @@ export declare type BeforeRequestHook = (request: Request, options: NormalizedOptions) => Request | Response | void | Promise<Request | Response | void>; | ||
export declare type AfterResponseHook = (request: Request, options: NormalizedOptions, response: Response) => Response | void | Promise<Response | void>; | ||
export declare type BeforeErrorHook = (error: HTTPError) => HTTPError | Promise<HTTPError>; | ||
export interface Hooks { | ||
@@ -86,2 +88,29 @@ /** | ||
afterResponse?: AfterResponseHook[]; | ||
/** | ||
This hook enables you to modify the `HTTPError` right before it is thrown. The hook function receives a `HTTPError` as an argument and should return an instance of `HTTPError`. | ||
@default [] | ||
@example | ||
``` | ||
import ky from 'ky'; | ||
await ky('https://example.com', { | ||
hooks: { | ||
beforeError: [ | ||
error => { | ||
const {response} = error; | ||
if (response && response.body) { | ||
error.name = 'GitHubError'; | ||
error.message = `${response.body.message} (${response.statusCode})`; | ||
} | ||
return error; | ||
} | ||
] | ||
} | ||
}); | ||
``` | ||
*/ | ||
beforeError?: BeforeErrorHook[]; | ||
} |
@@ -105,3 +105,3 @@ import { stop } from '../core/constants.js'; | ||
// Using `.text()` or other body methods is not suppported. | ||
// Using `.text()` or other body methods is not supported. | ||
const text = await ky('https://example.com', options).text(); | ||
@@ -108,0 +108,0 @@ ``` |
@@ -127,2 +127,4 @@ import type { LiteralUnion, Required } from './common.js'; | ||
Retries are not triggered following a timeout. | ||
@example | ||
@@ -143,3 +145,3 @@ ``` | ||
/** | ||
Timeout in milliseconds for getting a response. Can not be greater than 2147483647. | ||
Timeout in milliseconds for getting a response, including any retries. Can not be greater than 2147483647. | ||
If set to `false`, there will be no timeout. | ||
@@ -146,0 +148,0 @@ |
{ | ||
"name": "ky", | ||
"version": "0.28.7", | ||
"version": "0.29.0", | ||
"description": "Tiny and elegant HTTP client based on the browser Fetch API", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -181,2 +181,4 @@ <div align="center"> | ||
Retries are not triggered following a [timeout](#timeout). | ||
```js | ||
@@ -199,3 +201,3 @@ import ky from 'ky'; | ||
Timeout in milliseconds for getting a response. Can not be greater than 2147483647. | ||
Timeout in milliseconds for getting a response, including any retries. Can not be greater than 2147483647. | ||
If set to `false`, there will be no timeout. | ||
@@ -261,2 +263,29 @@ | ||
###### hooks.beforeError | ||
Type: `Function[]`\ | ||
Default: `[]` | ||
This hook enables you to modify the `HTTPError` right before it is thrown. The hook function receives a `HTTPError` as an argument and should return an instance of `HTTPError`. | ||
```js | ||
import ky from 'ky'; | ||
await ky('https://example.com', { | ||
hooks: { | ||
beforeError: [ | ||
error => { | ||
const {response} = error; | ||
if (response && response.body) { | ||
error.name = 'GitHubError'; | ||
error.message = `${response.body.message} (${response.statusCode})`; | ||
} | ||
return error; | ||
} | ||
] | ||
} | ||
}); | ||
``` | ||
###### hooks.afterResponse | ||
@@ -460,3 +489,3 @@ | ||
// Using `.text()` or other body methods is not suppported. | ||
// Using `.text()` or other body methods is not supported. | ||
const text = await ky('https://example.com', options).text(); | ||
@@ -463,0 +492,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
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
124652
913
621