Skip to content

Commit

Permalink
Fix BeforeRetryHook and NormalizedOptions TypeScript type definit…
Browse files Browse the repository at this point in the history
…ions (#308)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
jabuj and sindresorhus committed Jan 8, 2021
1 parent 1a7ec7f commit 5da3da0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 7 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ export type BeforeRequestHook = (
options: NormalizedOptions,
) => Request | Response | void | Promise<Request | Response | void>;

export type BeforeRetryHook = (options: {
export type BeforeRetryState = {
request: Request;
response: Response;
options: NormalizedOptions;
error: Error;
retryCount: number;
}) => typeof ky.stop | void | Promise<typeof ky.stop | void>;
};
export type BeforeRetryHook = (
options: BeforeRetryState
) => typeof ky.stop | void | Promise<typeof ky.stop | void>;

export type AfterResponseHook = (
request: Request,
Expand Down Expand Up @@ -367,8 +370,8 @@ export interface NormalizedOptions extends RequestInit {
credentials: RequestInit['credentials'];

// Extended from custom `KyOptions`, but ensured to be set (not optional).
retry: Options['retry'];
prefixUrl: Options['prefixUrl'];
retry: RetryOptions;
prefixUrl: string;
onDownloadProgress: Options['onDownloadProgress'];
}

Expand Down
6 changes: 4 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import ky, {ResponsePromise, DownloadProgress, Options, NormalizedOptions, Input} from '.';
import ky, {BeforeRetryState, ResponsePromise, DownloadProgress, Options, NormalizedOptions, Input} from '.';

const url = 'https://sindresorhus';

Expand Down Expand Up @@ -48,7 +48,9 @@ ky(url, {
}
],
beforeRetry: [
({request, response, options, error, retryCount}) => {
(beforeRetryOptions) => {
const {request, response, options, error, retryCount} = beforeRetryOptions;
expectType<BeforeRetryState>(beforeRetryOptions);
expectType<Request>(request);
expectType<Response>(response);
expectType<NormalizedOptions>(options);
Expand Down

0 comments on commit 5da3da0

Please sign in to comment.