Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 15, 2019
1 parent 0bc6fb1 commit 0b93fa8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -276,7 +276,7 @@ export interface Options extends RequestInit {
/**
Normalized options passed to the `fetch` call and the `beforeRequest` hooks.
*/
interface NormalizedOptions extends RequestInit {
export interface NormalizedOptions extends RequestInit {
// Extended from `RequestInit`, but ensured to be set (not optional).
method: RequestInit['method'];
credentials: RequestInit['credentials'];
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -360,7 +360,7 @@ class Ky {
this.request,
this._options,
error,
this._retryCount,
this._retryCount
);

// If `stop` is returned from the hook, the retry process is stopped
Expand Down
9 changes: 4 additions & 5 deletions index.test-d.ts
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import ky, {HTTPError, TimeoutError, ResponsePromise, DownloadProgress, Options, Input} from '.';
import ky, {HTTPError, TimeoutError, ResponsePromise, DownloadProgress, Options, NormalizedOptions, Input} from '.';

const url = 'https://sindresorhus';

Expand All @@ -25,14 +25,13 @@ expectType<typeof ky>(ky.create({}));
expectType<typeof ky>(ky.extend({}));
expectType<HTTPError>(new HTTPError(new Response));
expectType<TimeoutError>(new TimeoutError);
expectType<symbol>(ky.stop);

ky(url, {
hooks: {
beforeRequest: [
(request, options) => {
expectType<Request>(request);
expectType<Object>(options);
expectType<NormalizedOptions>(options);
request.headers.set('foo', 'bar');
},
(_request, _options) => {
Expand All @@ -51,7 +50,7 @@ ky(url, {
beforeRetry: [
(request, options, error, retryCount) => {
expectType<Request>(request);
expectType<Object>(options);
expectType<NormalizedOptions>(options);
expectType<Error>(error);
expectType<number>(retryCount);
request.headers.set('foo', 'bar');
Expand All @@ -60,7 +59,7 @@ ky(url, {
afterResponse: [
(request, options, response) => {
expectType<Request>(request);
expectType<Object>(options);
expectType<NormalizedOptions>(options);
expectType<Response>(response);
},
(_request, _options, _response) => {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -56,9 +56,9 @@
"node-fetch": "^2.5.0",
"nyc": "^14.1.1",
"puppeteer": "^1.15.0",
"rollup": "^1.11.3",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"rollup": "^1.27.0",
"tsd": "^0.11.0",
"xo": "^0.25.3"
},
"xo": {
"envs": [
Expand Down
18 changes: 9 additions & 9 deletions readme.md
Expand Up @@ -130,7 +130,7 @@ Type: `object`

##### method

Type: `string`<br>
Type: `string`\
Default: `get`

HTTP method used to make the request.
Expand All @@ -145,7 +145,7 @@ Shortcut for sending JSON. Use this instead of the `body` option. Accepts a plai

##### searchParams

Type: `string | object<string, string | number | boolean> | Array<Array<string | number | boolean>> | URLSearchParams`<br>
Type: `string | object<string, string | number | boolean> | Array<Array<string | number | boolean>> | URLSearchParams`\
Default: `''`

Search parameters to include in the request URL. Setting this will override all existing search parameters in the input URL.
Expand Down Expand Up @@ -180,7 +180,7 @@ Notes:

##### retry

Type: `object | number`<br>
Type: `object | number`\

Default:
- `limit`: `2`
Expand Down Expand Up @@ -212,22 +212,22 @@ import ky from 'ky';

##### timeout

Type: `number | false`<br>
Type: `number | false`\
Default: `10000`

Timeout in milliseconds for getting a response. Can not be greater than 2147483647.
If set to `false`, there will be no timeout.

##### hooks

Type: `object<string, Function[]>`<br>
Type: `object<string, Function[]>`\
Default: `{beforeRequest: [], beforeRetry: [], afterResponse: []}`

Hooks allow modifications during the request lifecycle. Hook functions may be async and are run serially.

###### hooks.beforeRequest

Type: `Function[]`<br>
Type: `Function[]`\
Default: `[]`

This hook enables you to modify the request right before it is sent. Ky will make no further changes to the request after this. The hook function receives `request` and `options` as arguments. You could, for example, modify the `request.headers` here.
Expand Down Expand Up @@ -255,7 +255,7 @@ const api = ky.extend({

###### hooks.beforeRetry

Type: `Function[]`<br>
Type: `Function[]`\
Default: `[]`

This hook enables you to modify the request right before retry. Ky will make no further changes to the request after this. The hook function receives the normalized request and options, an error instance and the retry count as arguments. You could, for example, modify `request.headers` here.
Expand All @@ -279,7 +279,7 @@ import ky from 'ky';

###### hooks.afterResponse

Type: `Function[]`<br>
Type: `Function[]`\
Default: `[]`

This hook enables you to read and optionally modify the response. The hook function receives normalized request, options, and a clone of the response as arguments. The return value of the hook function will be used by Ky as the response object if it's an instance of [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response).
Expand Down Expand Up @@ -319,7 +319,7 @@ import ky from 'ky';

##### throwHttpErrors

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Throw a `HTTPError` for error responses (non-2xx status codes).
Expand Down
8 changes: 4 additions & 4 deletions test/browser.js
Expand Up @@ -57,7 +57,7 @@ test('aborting a request', withPage, async (t, page) => {
const controller = new AbortController();
const request = window.ky(`${url}/test`, {signal: controller.signal}).text();
controller.abort();
return request.catch(error => error.toString());
return request.catch(error_ => error_.toString());
}, server.url);
t.is(error, 'AbortError: The user aborted a request.');

Expand All @@ -82,7 +82,7 @@ test('throws TimeoutError even though it does not support AbortController', with
window.ky = window.ky.default;

const request = window.ky(`${url}/endless`, {timeout: 500}).text();
return request.catch(error => error.toString());
return request.catch(error_ => error_.toString());
}, server.url);
t.is(error, 'TimeoutError: Request timed out');

Expand Down Expand Up @@ -148,7 +148,7 @@ test('throws if onDownloadProgress is not a function', withPage, async (t, page)
window.ky = window.ky.default;

const request = window.ky(url, {onDownloadProgress: 1}).text();
return request.catch(error => error.toString());
return request.catch(error_ => error_.toString());
}, server.url);
t.is(error, 'TypeError: The `onDownloadProgress` option must be a function');

Expand All @@ -170,7 +170,7 @@ test('throws if does not support ReadableStream', withPage, async (t, page) => {
window.ky = window.ky.default;

const request = window.ky(url, {onDownloadProgress: () => {}}).text();
return request.catch(error => error.toString());
return request.catch(error_ => error_.toString());
}, server.url);
t.is(error, 'Error: Streams are not supported in your environment. `ReadableStream` is missing.');

Expand Down

0 comments on commit 0b93fa8

Please sign in to comment.