Skip to content

Commit 8e171f5

Browse files
authoredJul 19, 2024··
Use KyRequest and KyResponse types in errors (#610)
* feat: additional json convenience methods - introduces `KyRequest` which, like `KyResponse`, adds `json()` convenience method for extracting JSON with an expected TypeScript type - update API `Request`/`Response` usages to expose `KyRequest`/`KyResponse` to `ky` consumers so that they may use the `json()` convenience method in more scenarios (`HTTPError`, `TimeoutError`, and hooks) Refs: #584 * fix unnecessary assertion * docs(readme): mention `.json()` TS improvements
1 parent 4886b66 commit 8e171f5

File tree

7 files changed

+19
-9
lines changed

7 files changed

+19
-9
lines changed
 

‎readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ It's just a tiny file with no dependencies.
7171
- URL prefix option
7272
- Instances with custom defaults
7373
- Hooks
74+
- TypeScript niceties (e.g. `.json()` resolves to `unknown`, not `any`; `.json<T>()` can be used too)
7475

7576
## Install
7677

‎source/errors/HTTPError.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type {NormalizedOptions} from '../types/options.js';
2+
import type {KyRequest} from '../types/request.js';
3+
import type {KyResponse} from '../types/response.js';
24

35
// eslint-lint-disable-next-line @typescript-eslint/naming-convention
46
export class HTTPError extends Error {
5-
public response: Response;
6-
public request: Request;
7+
public response: KyResponse;
8+
public request: KyRequest;
79
public options: NormalizedOptions;
810

911
constructor(response: Response, request: Request, options: NormalizedOptions) {

‎source/errors/TimeoutError.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import type {KyRequest} from '../types/request.js';
2+
13
export class TimeoutError extends Error {
2-
public request: Request;
4+
public request: KyRequest;
35

46
constructor(request: Request) {
57
super(`Request timed out: ${request.method} ${request.url}`);

‎source/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export type {
4848
} from './types/hooks.js';
4949

5050
export type {ResponsePromise} from './types/ResponsePromise.js';
51+
export type {KyRequest} from './types/request.js';
5152
export type {KyResponse} from './types/response.js';
5253
export {HTTPError} from './errors/HTTPError.js';
5354
export {TimeoutError} from './errors/TimeoutError.js';

‎source/types/hooks.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import {type stop} from '../core/constants.js';
2-
import {type HTTPError} from '../index.js';
2+
import type {KyRequest, KyResponse, HTTPError} from '../index.js';
33
import type {NormalizedOptions} from './options.js';
44

55
export type BeforeRequestHook = (
6-
request: Request,
6+
request: KyRequest,
77
options: NormalizedOptions
88
) => Request | Response | void | Promise<Request | Response | void>;
99

1010
export type BeforeRetryState = {
11-
request: Request;
11+
request: KyRequest;
1212
options: NormalizedOptions;
1313
error: Error;
1414
retryCount: number;
1515
};
1616
export type BeforeRetryHook = (options: BeforeRetryState) => typeof stop | void | Promise<typeof stop | void>;
1717

1818
export type AfterResponseHook = (
19-
request: Request,
19+
request: KyRequest,
2020
options: NormalizedOptions,
21-
response: Response
21+
response: KyResponse
2222
) => Response | void | Promise<Response | void>;
2323

2424
export type BeforeErrorHook = (error: HTTPError) => HTTPError | Promise<HTTPError>;

‎source/types/request.ts

+4
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ type UndiciRequestInit = {
5959
type CombinedRequestInit = globalThis.RequestInit & UndiciRequestInit;
6060

6161
export type RequestInitRegistry = {[K in keyof CombinedRequestInit]-?: true};
62+
63+
export type KyRequest = {
64+
json: <T = unknown>() => Promise<T>;
65+
} & Request;

‎test/hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ test('beforeError can return promise which resolves to HTTPError', async t => {
652652
beforeError: [
653653
async (error: HTTPError) => {
654654
const {response} = error;
655-
const body = await response.json() as {reason: string};
655+
const body = await response.json<{reason: string}>();
656656

657657
if (response?.body) {
658658
error.name = 'GitHubError';

0 commit comments

Comments
 (0)
Please sign in to comment.