Skip to content
Myr

Use KyRequest and KyResponse types in errors (#610)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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

authored by Mark Fulton · committed by GitHub · parent 4886b66 · Browse files · 7 files+19 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+19 added−9 removed

  • tests:+1 added−1 removed
  • other:+18 added−8 removed

7 files+19 added−9 removed

modified:source/errors/HTTPError.ts+4 added-2 removed
modified:source/errors/TimeoutError.ts+3 added-1 removed
modified:source/types/hooks.ts+5 added-5 removed
modified:source/types/request.ts+4 added
57 unmodified lines
5859type CombinedRequestInit = globalThis.RequestInit & UndiciRequestInit;6061export type RequestInitRegistry = {[K in keyof CombinedRequestInit]-?: true};6263export type KyRequest = {64 json: <T = unknown>() => Promise<T>;65} & Request;
modified:source/index.ts+1 added
46 unmodified lines
47 AfterResponseHook,48} from './types/hooks.js';4950export type {ResponsePromise} from './types/ResponsePromise.js';51export type {KyRequest} from './types/request.js';52export type {KyResponse} from './types/response.js';53export {HTTPError} from './errors/HTTPError.js';54export {TimeoutError} from './errors/TimeoutError.js';
modified:test/hooks.ts+1 added-1 removed
650 unmodified lines
651 hooks: {652 beforeError: [653 async (error: HTTPError) => {654 const {response} = error;655 const body = await response.json() as {reason: string};655 const body = await response.json<{reason: string}>();656657 if (response?.body) {658 error.name = 'GitHubError';659 error.message = `${body.reason} --- (${response.status})`.trim();
15 unmodified lines
modified:readme.md+1 added
69 unmodified lines
70- Timeout support71- URL prefix option72- Instances with custom defaults73- Hooks74- TypeScript niceties (e.g. `.json()` resolves to `unknown`, not `any`; `.json<T>()` can be used too)7576## Install7778```sh
650 unmodified lines