diff --git a/index.d.ts b/index.d.ts index 017f1101..55b6947d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -331,7 +331,7 @@ export interface ResponsePromise extends Promise { /** The error has a response property with the `Response` object. */ -export class HTTPError extends Error { +declare class HTTPError extends Error { constructor(response: Response); response: Response; } @@ -339,7 +339,7 @@ export class HTTPError extends Error { /** The error thrown when the request times out. */ -export class TimeoutError extends Error { +declare class TimeoutError extends Error { constructor(); } @@ -453,6 +453,8 @@ declare const ky: { ``` */ readonly stop: unique symbol; + readonly TimeoutError: typeof TimeoutError; + readonly HTTPError: typeof HTTPError; }; export default ky; diff --git a/index.js b/index.js index 2995aef7..a8b2ff0f 100644 --- a/index.js +++ b/index.js @@ -450,6 +450,8 @@ const createInstance = defaults => { ky[method] = (input, options) => new Ky(input, validateAndMerge(defaults, options, {method})); } + ky.HTTPError = HTTPError; + ky.TimeoutError = TimeoutError; ky.create = newDefaults => createInstance(validateAndMerge(newDefaults)); ky.extend = newDefaults => createInstance(validateAndMerge(defaults, newDefaults)); ky.stop = stop; @@ -458,8 +460,3 @@ const createInstance = defaults => { }; export default createInstance(); - -export { - HTTPError, - TimeoutError -}; diff --git a/index.test-d.ts b/index.test-d.ts index bf07f7d9..9568e096 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType} from 'tsd'; -import ky, {HTTPError, TimeoutError, ResponsePromise, DownloadProgress, Options, NormalizedOptions, Input} from '.'; +import ky, {ResponsePromise, DownloadProgress, Options, NormalizedOptions, Input} from '.'; const url = 'https://sindresorhus'; @@ -23,8 +23,8 @@ for (const method of requestMethods) { expectType(ky.create({})); expectType(ky.extend({})); -expectType(new HTTPError(new Response)); -expectType(new TimeoutError); +expectType>(new ky.HTTPError(new Response())); +expectType>(new ky.TimeoutError()); ky(url, { hooks: { diff --git a/readme.md b/readme.md index 7c32a291..5844d8a3 100644 --- a/readme.md +++ b/readme.md @@ -96,7 +96,7 @@ import ky from 'https://unpkg.com/ky/index.js'; In environments that do not support `import`, you can load `ky` in [UMD format](https://medium.freecodecamp.org/anatomy-of-js-module-systems-and-building-libraries-fadcd8dbd0e). For example, using `require()`: ```js -const ky = require('ky/umd').default; +const ky = require('ky/umd'); ``` With the UMD version, it's also easy to use `ky` [without a bundler](#how-do-i-use-this-without-a-bundler-like-webpack) or module system. @@ -517,9 +517,7 @@ Alternatively, you can use the [`umd.js`](umd.js) file with a traditional `