From 0cf07c85887638ecca22d35209f1ba44b9593ed5 Mon Sep 17 00:00:00 2001 From: Seth Holladay Date: Sat, 16 Nov 2019 01:18:15 -0500 Subject: [PATCH 1/3] Move named exports to properties of default export --- index.js | 7 ++----- test/browser.js | 12 ------------ 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 3068db16..b0b6bebc 100644 --- a/index.js +++ b/index.js @@ -455,6 +455,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; @@ -463,8 +465,3 @@ const createInstance = defaults => { }; export default createInstance(); - -export { - HTTPError, - TimeoutError -}; diff --git a/test/browser.js b/test/browser.js index 9cabfdb2..3d26b76e 100644 --- a/test/browser.js +++ b/test/browser.js @@ -16,8 +16,6 @@ test('prefixUrl option', withPage, async (t, page) => { await t.throwsAsync(async () => { return page.evaluate(() => { - window.ky = window.ky.default; - return window.ky('/foo', {prefixUrl: '/'}); }); }, /`input` must not begin with a slash when using `prefixUrl`/); @@ -52,8 +50,6 @@ test('aborting a request', withPage, async (t, page) => { await page.addScriptTag({path: './umd.js'}); const error = await page.evaluate(url => { - window.ky = window.ky.default; - const controller = new AbortController(); const request = window.ky(`${url}/test`, {signal: controller.signal}).text(); controller.abort(); @@ -79,8 +75,6 @@ test('throws TimeoutError even though it does not support AbortController', with // TODO: make set a timeout for this evaluation so we don't have to wait 30s const error = await page.evaluate(url => { - window.ky = window.ky.default; - const request = window.ky(`${url}/endless`, {timeout: 500}).text(); return request.catch(error_ => error_.toString()); }, server.url); @@ -108,8 +102,6 @@ test('onDownloadProgress works', withPage, async (t, page) => { await page.addScriptTag({path: './umd.js'}); const result = await page.evaluate(async url => { - window.ky = window.ky.default; - // `new TextDecoder('utf-8').decode` hangs up? const decodeUTF8 = array => String.fromCharCode(...array); @@ -145,8 +137,6 @@ test('throws if onDownloadProgress is not a function', withPage, async (t, page) await page.addScriptTag({path: './umd.js'}); const error = await page.evaluate(url => { - window.ky = window.ky.default; - const request = window.ky(url, {onDownloadProgress: 1}).text(); return request.catch(error_ => error_.toString()); }, server.url); @@ -167,8 +157,6 @@ test('throws if does not support ReadableStream', withPage, async (t, page) => { await page.addScriptTag({path: './umd.js'}); const error = await page.evaluate(url => { - window.ky = window.ky.default; - const request = window.ky(url, {onDownloadProgress: () => {}}).text(); return request.catch(error_ => error_.toString()); }, server.url); From 02818df636b3d89ff665e974923fa7537d41b8ec Mon Sep 17 00:00:00 2001 From: Seth Holladay Date: Thu, 21 Nov 2019 04:33:15 -0500 Subject: [PATCH 2/3] Update more imports --- index.d.ts | 6 ++++-- index.test-d.ts | 6 +++--- readme.md | 6 ++---- test/main.js | 6 +++--- umd.d.ts | 1 - 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/index.d.ts b/index.d.ts index 92b351d0..6e708a11 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: TimeoutError; + readonly HTTPError: HTTPError; }; export default ky; diff --git a/index.test-d.ts b/index.test-d.ts index bf07f7d9..f6e2d7f5 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 481a9eea..7fe43523 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. @@ -518,9 +518,7 @@ Alternatively, you can use the [`umd.js`](umd.js) file with a traditional `