Make `HTTPError` and `TimeoutError` named exports
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #320
authored by Sindre Sorhus · committed by Sindre Sorhus · parent b3c9e88 · Browse files · 6 files·+21 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+21 added−27 removed
- tests:+10 added−11 removed
- other:+11 added−16 removed
Provenance
- Commit
6ec7fd7- Tree
b805099- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
b3c9e88
6 files changed+21 added−27 removed
modified:source/types/ky.ts-4 removed
123234592 unmodified lines989910010110410510210310410515 unmodified linesimport {stop} from '../core/constants.js';import type {HTTPError} from '../errors/HTTPError.js';import type {TimeoutError} from '../errors/TimeoutError.js';import type {Input, Options} from './options.js';import type {ResponsePromise} from './response.js';
export interface ky {92 unmodified lines const text = await ky('https://example.com', options).text(); ``` */ readonly stop: typeof stop; readonly TimeoutError: typeof TimeoutError; readonly HTTPError: typeof HTTPError;
/** Fetch the given `url`.
15 unmodified linesmodified:source/index.ts+3 added-4 removed
12345656786 unmodified lines151617182122192021222 unmodified lines25262728293031/*! MIT License © Sindre Sorhus */
import {Ky} from './core/Ky.js';import {requestMethods, stop} from './core/constants.js';import {HTTPError} from './errors/HTTPError.js';import {TimeoutError} from './errors/TimeoutError.js';import type {ky as KyInterface} from './types/ky.js';import type {Input, Options} from './types/options.js';import {validateAndMerge} from './utils/merge.js';
6 unmodified lines // eslint-disable-next-line @typescript-eslint/promise-function-async ky[method] = (input: Input, options: Options) => Ky.create(input, validateAndMerge(defaults, options, {method})); }
ky.HTTPError = HTTPError; ky.TimeoutError = TimeoutError; ky.create = (newDefaults?: Partial<Options>) => createInstance(validateAndMerge(newDefaults)); ky.extend = (newDefaults?: Partial<Options>) => createInstance(validateAndMerge(defaults, newDefaults)); ky.stop = stop;
2 unmodified lines
const ky = createInstance();
export default ky;
export {HTTPError} from './errors/HTTPError.js';export {TimeoutError} from './errors/TimeoutError.js';modified:test/hooks.ts+3 added-4 removed
1 import test from 'ava';2 import delay from 'delay';3-import {HTTPError} from '../source/errors/HTTPError.js';4-import ky from '../source/index.js';3+import ky, {HTTPError} from '../source/index.js';4 import {createHttpTestServer} from './helpers/create-http-test-server.js';5 6 test('hooks can be async', async t => {7 const server = await createHttpTestServer();395 unmodified lines403 await ky.get(server.url, {404 hooks: {405 beforeRetry: [406 ({error, retryCount}) => {408- t.true(error instanceof ky.HTTPError);407+ t.true(error instanceof HTTPError);408 t.true(retryCount >= 1);409 }410 ]411 }68 unmodified lines480 },481 hooks: {482 beforeRetry: [483 async ({error, retryCount}) => {485- t.true(error instanceof ky.HTTPError);484+ t.true(error instanceof HTTPError);485 t.is(error.message, 'Request failed with status code 502 Bad Gateway');486 t.true((error as HTTPError).response instanceof Response);487 t.deepEqual(await (error as HTTPError).response.json(), {awesome: true});488 t.is(retryCount, 1);116 unmodified linesmodified:test/http-error.ts+4 added-4 removed
1 import test from 'ava';2-import ky from '../source/index.js';2+import {HTTPError} from '../source/index.js';3 4 function createFakeResponse({status, statusText}: {status?: number; statusText?: string}): Response {5 // Start with a realistic fetch Response.6 const response = {...new Response()};10 unmodified lines17 18 test('HTTPError handles undefined response.statusText', t => {19 const status = 500;20 // @ts-expect-error missing Request21- const error = new ky.HTTPError(21+ const error = new HTTPError(22 // This simulates the case where a browser Response object does23 // not define statusText, such as IE, Safari, etc.24 // See https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText#Browser_compatibility25 createFakeResponse({statusText: undefined, status})3 unmodified lines29 });30 31 test('HTTPError handles undefined response.status', t => {32 // @ts-expect-error missing Request33- const error = new ky.HTTPError(33+ const error = new HTTPError(34 // This simulates a catastrophic case where some unexpected35 // response object was sent to HTTPError.36 createFakeResponse({statusText: undefined, status: undefined})37 );2 unmodified lines40 });41 42 test('HTTPError handles a response.status of 0', t => {43 // @ts-expect-error missing Request44- const error = new ky.HTTPError(44+ const error = new HTTPError(45 // Apparently, it's possible to get a response status of 0.46 createFakeResponse({statusText: undefined, status: 0})47 );48 2 unmodified linesmodified:test/main.ts+3 added-3 removed
1 import test from 'ava';2 import delay from 'delay';3-import ky from '../source/index.js';3+import ky, {TimeoutError} from '../source/index.js';4 import {createHttpTestServer} from './helpers/create-http-test-server.js';5 import {parseRawBody} from './helpers/parse-body.js';6 7 const fixture = 'fixture';268 unmodified lines276 response.end(fixture);277 });278 279 await t.throwsAsync(ky(server.url, {timeout: 1000}).text(), {280- instanceOf: ky.TimeoutError280+ instanceOf: TimeoutError281 });282 283 t.is(requestCount, 1);284 119 unmodified lines404 });405 406 await t.throwsAsync(407 ky(server.url, {throwHttpErrors: false, timeout: 500}).text(),408- {instanceOf: ky.TimeoutError}408+ {instanceOf: TimeoutError}409 );410 411 t.is(requestCount, 1);412 279 unmodified linesmodified:readme.md+8 added-8 removed
423 unmodified lines424 #### defaultOptions425 426 Type: `object`427 428-### ky.HTTPError429-430-Exposed for `instanceof` checks. The error has a `response` property with the [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response), `request` property with the [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request), and `options` property with normalized options (either passed to `ky` when creating an instance with `ky.create()` or directly when performing the request).431-432-### ky.TimeoutError433-434-The error thrown when the request times out. It has a `request` property with the [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request).435-428 ### ky.stop429 430 A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry. This will also short circuit the remaining `beforeRetry` hooks.431 23 unmodified lines455 // Using `.text()` or other body methods is not suppported.456 const text = await ky('https://example.com', options).text();457 ```458 459+### HTTPError460+461+Exposed for `instanceof` checks. The error has a `response` property with the [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response), `request` property with the [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request), and `options` property with normalized options (either passed to `ky` when creating an instance with `ky.create()` or directly when performing the request).462+463+### TimeoutError464+465+The error thrown when the request times out. It has a `request` property with the [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request).466+467 ## Tips468 469 ### Sending form data470 119 unmodified lines