(got) fix: add init hook (#36145)

* fix: add init hook

* test: add test

* test: separate test case

* fix: set return value to void

* fix: update version
This commit is contained in:
Julian Hundeloh
2019-06-17 09:01:33 +02:00
committed by Daniel Rosenwasser
parent 1611b14d95
commit 5d1bd2231e
2 changed files with 16 additions and 1 deletions

View File

@@ -341,6 +341,15 @@ got('http://todomvc.com', {
got('http://todomvc.com', { timeout: 1 }).catch((err) => err instanceof got.TimeoutError);
// Test hooks.
got('example.com', {
hooks: {
init: [
options => {
options.baseUrl = 'https://google.com';
}
]
}
});
got('example.com', {
hooks: {
beforeRequest: [

View File

@@ -1,4 +1,4 @@
// Type definitions for got 9.4
// Type definitions for got 9.6
// Project: https://github.com/sindresorhus/got#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Linus Unnebäck <https://github.com/LinusU>
@@ -140,12 +140,18 @@ declare namespace got {
* @template Body Response body type.
*/
interface Hooks<Options, Body extends Buffer | string | object> {
init?: Array<InitHook<Options>>;
beforeRequest?: Array<BeforeRequestHook<Options>>;
beforeRedirect?: Array<BeforeRedirectHook<Options>>;
beforeRetry?: Array<BeforeRetryHook<Options>>;
afterResponse?: Array<AfterResponseHook<Options, Body>>;
}
/**
* @param options Unnormalized request options.
*/
type InitHook<Options> = (options: Options) => void;
/**
* @param options Normalized request options.
*/