Skip to content

Commit 588f453

Browse files
committed
Meta tweaks
1 parent 70826b2 commit 588f453

File tree

6 files changed

+40
-38
lines changed

6 files changed

+40
-38
lines changed

Diff for: index.d.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ export type BeforeRequestHook = (
1414
) => Request | Response | void | Promise<Request | Response | void>;
1515

1616
export type BeforeRetryHook = (options: {
17-
request: Request,
18-
response: Response,
19-
options: NormalizedOptions,
20-
error: Error,
21-
retryCount: number,
17+
request: Request;
18+
response: Response;
19+
options: NormalizedOptions;
20+
error: Error;
21+
retryCount: number;
2222
}) => void | Promise<void>;
2323

2424
export type AfterResponseHook = (
@@ -329,11 +329,11 @@ export interface NormalizedOptions extends RequestInit {
329329
Returns a `Response` object with `Body` methods added for convenience. So you can, for example, call `ky.get(input).json()` directly without having to await the `Response` first. When called like that, an appropriate `Accept` header will be set depending on the body method used. Unlike the `Body` methods of `window.Fetch`; these will throw an `HTTPError` if the response status is not in the range of `200...299`. Also, `.json()` will return an empty string if the response status is `204` instead of throwing a parse error due to an empty body.
330330
*/
331331
export interface ResponsePromise extends Promise<Response> {
332-
arrayBuffer(): Promise<ArrayBuffer>;
332+
arrayBuffer: () => Promise<ArrayBuffer>;
333333

334-
blob(): Promise<Blob>;
334+
blob: () => Promise<Blob>;
335335

336-
formData(): Promise<FormData>;
336+
formData: () => Promise<FormData>;
337337

338338
// TODO: Use `json<T extends JSONValue>(): Promise<T>;` when it's fixed in TS.
339339
// See https://github.com/microsoft/TypeScript/issues/15300 and https://github.com/sindresorhus/ky/pull/80
@@ -358,9 +358,9 @@ export interface ResponsePromise extends Promise<Response> {
358358
const result = await ky(…).json<Result>();
359359
```
360360
*/
361-
json<T>(): Promise<T>;
361+
json: <T>() => Promise<T>;
362362

363-
text(): Promise<string>;
363+
text: () => Promise<string>;
364364
}
365365

366366
/**
@@ -405,54 +405,54 @@ declare const ky: {
405405
@param url - `Request` object, `URL` object, or URL string.
406406
@returns A promise with `Body` methods added.
407407
*/
408-
get(url: Input, options?: Options): ResponsePromise;
408+
get: (url: Input, options?: Options) => ResponsePromise;
409409

410410
/**
411411
Fetch the given `url` using the option `{method: 'post'}`.
412412
413413
@param url - `Request` object, `URL` object, or URL string.
414414
@returns A promise with `Body` methods added.
415415
*/
416-
post(url: Input, options?: Options): ResponsePromise;
416+
post: (url: Input, options?: Options) => ResponsePromise;
417417

418418
/**
419419
Fetch the given `url` using the option `{method: 'put'}`.
420420
421421
@param url - `Request` object, `URL` object, or URL string.
422422
@returns A promise with `Body` methods added.
423423
*/
424-
put(url: Input, options?: Options): ResponsePromise;
424+
put: (url: Input, options?: Options) => ResponsePromise;
425425

426426
/**
427427
Fetch the given `url` using the option `{method: 'delete'}`.
428428
429429
@param url - `Request` object, `URL` object, or URL string.
430430
@returns A promise with `Body` methods added.
431431
*/
432-
delete(url: Input, options?: Options): ResponsePromise;
432+
delete: (url: Input, options?: Options) => ResponsePromise;
433433

434434
/**
435435
Fetch the given `url` using the option `{method: 'patch'}`.
436436
437437
@param url - `Request` object, `URL` object, or URL string.
438438
@returns A promise with `Body` methods added.
439439
*/
440-
patch(url: Input, options?: Options): ResponsePromise;
440+
patch: (url: Input, options?: Options) => ResponsePromise;
441441

442442
/**
443443
Fetch the given `url` using the option `{method: 'head'}`.
444444
445445
@param url - `Request` object, `URL` object, or URL string.
446446
@returns A promise with `Body` methods added.
447447
*/
448-
head(url: Input, options?: Options): ResponsePromise;
448+
head: (url: Input, options?: Options) => ResponsePromise;
449449

450450
/**
451451
Create a new Ky instance with complete new defaults.
452452
453453
@returns A new Ky instance.
454454
*/
455-
create(defaultOptions: Options): typeof ky;
455+
create: (defaultOptions: Options) => typeof ky;
456456

457457
/**
458458
Create a new Ky instance with some defaults overridden with your own.
@@ -461,7 +461,7 @@ declare const ky: {
461461
462462
@returns A new Ky instance.
463463
*/
464-
extend(defaultOptions: Options): typeof ky;
464+
extend: (defaultOptions: Options) => typeof ky;
465465

466466
/**
467467
A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry.
@@ -493,8 +493,8 @@ declare const ky: {
493493
};
494494

495495
declare namespace ky {
496-
export type TimeoutError = InstanceType<typeof ky.TimeoutError>;
497-
export type HTTPError = InstanceType<typeof ky.HTTPError>;
496+
export type TimeoutError = InstanceType<typeof TimeoutError>;
497+
export type HTTPError = InstanceType<typeof HTTPError>;
498498
}
499499

500500
export default ky;

Diff for: index.test-d.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const requestMethods = [
1212
'put',
1313
'delete',
1414
'patch',
15-
'head',
15+
'head'
1616
] as const;
1717

1818
// Test Ky HTTP methods
@@ -80,20 +80,22 @@ ky(new Request(url));
8080
const input: Input = new URL('https://sindresorhus');
8181
const options: Options = {
8282
method: 'get',
83-
timeout: 5000,
84-
}
83+
timeout: 5000
84+
};
8585
ky(input, options);
8686

8787
// Extending Ky
8888
interface CustomOptions extends Options {
8989
foo?: boolean;
9090
}
9191
async function customKy(input: Input, options?: CustomOptions) {
92-
if (options && options.foo) {
92+
if (options?.foo) {
9393
options.json = {foo: options.foo};
9494
}
95+
9596
return ky(input, options);
9697
}
98+
9799
customKy(input, options);
98100

99101
// `searchParams` option
@@ -151,6 +153,6 @@ try {
151153
} else if (error instanceof ky.TimeoutError) {
152154
expectType<ky.TimeoutError>(error);
153155
} else {
154-
throw error;
156+
throw error;
155157
}
156158
}

Diff for: license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

Diff for: package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"author": {
99
"name": "Sindre Sorhus",
1010
"email": "[email protected]",
11-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1212
},
1313
"engines": {
1414
"node": ">=10"
@@ -58,8 +58,8 @@
5858
"form-data": "^3.0.0",
5959
"node-fetch": "^2.5.0",
6060
"nyc": "^15.0.0",
61-
"puppeteer": "^2.1.0",
62-
"rollup": "^1.31.0",
61+
"puppeteer": "^3.0.4",
62+
"rollup": "^2.10.2",
6363
"tsd": "^0.11.0",
6464
"xo": "^0.25.3"
6565
},

Diff for: readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Type: `object`
127127
##### method
128128

129129
Type: `string`\
130-
Default: `get`
130+
Default: `'get'`
131131

132132
HTTP method used to make the request.
133133

Diff for: test/main.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -333,19 +333,19 @@ test('searchParams option', async t => {
333333
response.end(request.url.slice(1));
334334
});
335335

336-
const arrayParams = [['cats', 'meow'], ['dogs', true], ['opossums', false]];
337-
const objectParams = {
336+
const arrayParameters = [['cats', 'meow'], ['dogs', true], ['opossums', false]];
337+
const objectParameters = {
338338
cats: 'meow',
339339
dogs: true,
340340
opossums: false
341341
};
342-
const searchParams = new URLSearchParams(arrayParams);
343-
const stringParams = '?cats=meow&dogs=true&opossums=false';
342+
const searchParameters = new URLSearchParams(arrayParameters);
343+
const stringParameters = '?cats=meow&dogs=true&opossums=false';
344344

345-
t.is(await ky(server.url, {searchParams: arrayParams}).text(), stringParams);
346-
t.is(await ky(server.url, {searchParams: objectParams}).text(), stringParams);
347-
t.is(await ky(server.url, {searchParams}).text(), stringParams);
348-
t.is(await ky(server.url, {searchParams: stringParams}).text(), stringParams);
345+
t.is(await ky(server.url, {searchParams: arrayParameters}).text(), stringParameters);
346+
t.is(await ky(server.url, {searchParams: objectParameters}).text(), stringParameters);
347+
t.is(await ky(server.url, {searchParams: searchParameters}).text(), stringParameters);
348+
t.is(await ky(server.url, {searchParams: stringParameters}).text(), stringParameters);
349349

350350
await server.close();
351351
});

0 commit comments

Comments
 (0)