[got] Add more hooks to v9

This commit is contained in:
ikokostya
2018-12-29 20:34:52 +03:00
parent efb66b22be
commit 92c07e3e6f
2 changed files with 117 additions and 7 deletions
+72
View File
@@ -286,3 +286,75 @@ got('http://todomvc.com', {
// Test got.TimeoutError.
got('http://todomvc.com', {timeout: 1}).catch((err) => err instanceof got.TimeoutError);
// Test hooks.
got('example.com', {
hooks: {
beforeRedirect: [
options => {
if (options.hostname === 'deadSite') {
options.hostname = 'fallbackSite';
}
}
]
}
});
got('example.com', {
hooks: {
beforeRetry: [
(options, error, retryCount) => {
if (error.statusCode === 413) { // Payload too large
options.body = 'test';
}
}
]
}
});
got('example.com', {
hooks: {
afterResponse: [
(response, retryWithMergedOptions) => {
if (response.statusCode === 401) { // Unauthorized
const updatedOptions = {
headers: {
token: 'new token' // Refresh the access token
}
};
// Make a new retry
return retryWithMergedOptions(updatedOptions);
}
// No changes otherwise
return response;
}
]
}
});
// Test async hooks.
{
const doSomethingAsync = () => {
throw new Error('unimplemented');
};
got('example.com', {
hooks: {
beforeRedirect: [
async () => {
await doSomethingAsync();
}
],
beforeRetry: [
async () => {
await doSomethingAsync();
}
],
afterResponse: [
async (response) => {
await doSomethingAsync();
return response;
}
]
}
});
}
+45 -7
View File
@@ -1,4 +1,4 @@
// Type definitions for got 9.2
// Type definitions for got 9.3
// Project: https://github.com/sindresorhus/got#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Linus Unnebäck <https://github.com/LinusU>
@@ -102,12 +102,50 @@ declare namespace got {
type GotUrl = string | https.RequestOptions | Url | URL;
type Hook<T> = (options: T) => any;
type Hooks<T> = Record<'beforeRequest', Array<Hook<T>>>;
/**
* Hooks allow modifications during the request lifecycle. Hook functions may be async and are
* run serially.
*
* @see https://github.com/sindresorhus/got#hooks
* @template Options Request options.
* @template Body Response body type.
*/
interface Hooks<Options, Body extends Buffer | string | object> {
beforeRequest?: Array<BeforeRequestHook<Options>>;
beforeRedirect?: Array<BeforeRedirectHook<Options>>;
beforeRetry?: Array<BeforeRetryHook<Options>>;
afterResponse?: Array<AfterResponseHook<Options, Body>>;
}
/**
* @param options Normalized request options.
*/
type BeforeRequestHook<Options> = (options: Options) => any;
/**
* @param options Normalized request options.
*/
type BeforeRedirectHook<Options> = (options: Options) => any;
/**
* @param options Normalized request options.
* @param error Error from previous attempt.
* @param retryCount Number of retry.
*/
type BeforeRetryHook<Options> = (options: Options, error: HTTPError, retryCount: number) => any;
/**
* @param options Normalized request options.
* @param retryWithMergedOptions Retries request with the updated options.
*/
type AfterResponseHook<Options, Body extends Buffer | string | object > = (
response: Response<Body>,
retryWithMergedOptions: (updateOptions: Options) => GotPromise<Body>
) => Response<Body> | Promise<Response<Body>>;
interface GotBodyOptions<E extends string | null> extends GotOptions<E> {
body?: string | Buffer | nodeStream.Readable;
hooks?: Hooks<GotBodyOptions<E>>;
hooks?: Hooks<GotBodyOptions<E>, string | Buffer | nodeStream.Readable>;
}
interface GotJSONOptions extends GotOptions<string | null> {
@@ -115,14 +153,14 @@ declare namespace got {
body?: object;
form?: boolean;
json: true;
hooks?: Hooks<GotJSONOptions>;
hooks?: Hooks<GotJSONOptions, object>;
}
interface GotFormOptions<E extends string | null> extends GotOptions<E> {
body?: {[key: string]: any};
body?: Record<string, any>;
form: true;
json?: boolean;
hooks?: Hooks<GotFormOptions<E>>;
hooks?: Hooks<GotFormOptions<E>, Record<string, any>>;
}
interface GotOptions<E extends string | null> extends InternalRequestOptions {