Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move named exports to properties of default export (#206)
  • Loading branch information
sholladay committed Feb 2, 2020
1 parent 55b2535 commit cf6ca95
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 33 deletions.
6 changes: 4 additions & 2 deletions index.d.ts
Expand Up @@ -331,15 +331,15 @@ export interface ResponsePromise extends Promise<Response> {
/**
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;
}

/**
The error thrown when the request times out.
*/
export class TimeoutError extends Error {
declare class TimeoutError extends Error {
constructor();
}

Expand Down Expand Up @@ -453,6 +453,8 @@ declare const ky: {
```
*/
readonly stop: unique symbol;
readonly TimeoutError: typeof TimeoutError;
readonly HTTPError: typeof HTTPError;
};

export default ky;
7 changes: 2 additions & 5 deletions index.js
Expand Up @@ -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;
Expand All @@ -458,8 +460,3 @@ const createInstance = defaults => {
};

export default createInstance();

export {
HTTPError,
TimeoutError
};
6 changes: 3 additions & 3 deletions 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';

Expand All @@ -23,8 +23,8 @@ for (const method of requestMethods) {

expectType<typeof ky>(ky.create({}));
expectType<typeof ky>(ky.extend({}));
expectType<HTTPError>(new HTTPError(new Response));
expectType<TimeoutError>(new TimeoutError);
expectType<InstanceType<typeof ky.HTTPError>>(new ky.HTTPError(new Response()));
expectType<InstanceType<typeof ky.TimeoutError>>(new ky.TimeoutError());

ky(url, {
hooks: {
Expand Down
6 changes: 2 additions & 4 deletions readme.md
Expand Up @@ -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.
Expand Down Expand Up @@ -517,9 +517,7 @@ Alternatively, you can use the [`umd.js`](umd.js) file with a traditional `<scri
<script src="https://cdn.jsdelivr.net/npm/ky@latest/umd.js"></script>
<script>
(async () => {
const client = ky.default;
const parsed = await client('https://jsonplaceholder.typicode.com/todos/1').json();
const parsed = await ky('https://jsonplaceholder.typicode.com/todos/1').json();
console.log(parsed.title);
//=> 'delectus aut autem
Expand Down
15 changes: 0 additions & 15 deletions test/browser.js
Expand Up @@ -20,8 +20,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`/);
Expand Down Expand Up @@ -56,8 +54,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();
Expand Down Expand Up @@ -86,8 +82,6 @@ test('throws TimeoutError even though it does not support AbortController', with
await page.addScriptTag({path: './umd.js'});

const error = await page.evaluate(url => {
window.ky = window.ky.default;

const request = window.ky(`${url}/slow`, {timeout: 500}).text();
return request.catch(error_ => error_.toString());
}, server.url);
Expand All @@ -114,8 +108,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);

Expand Down Expand Up @@ -151,8 +143,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);
Expand All @@ -173,8 +163,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);
Expand Down Expand Up @@ -202,7 +190,6 @@ test('FormData with searchParams', withPage, async (t, page) => {
await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});
await page.evaluate(url => {
window.ky = window.ky.default;
const formData = new window.FormData();
formData.append('file', new window.File(['bubblegum pie'], 'my-file'));
return window.ky(url, {
Expand Down Expand Up @@ -232,7 +219,6 @@ test('headers are preserved when input is a Request and there are searchParams i
await page.addScriptTag({path: './umd.js'});

await page.evaluate(url => {
window.ky = window.ky.default;
const request = new window.Request(url + '/test', {
headers: {'content-type': 'text/css'}
});
Expand Down Expand Up @@ -261,7 +247,6 @@ test('retry with body', 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 + '/test', {
body: 'foo',
method: 'PUT',
Expand Down
6 changes: 3 additions & 3 deletions test/main.js
Expand Up @@ -3,7 +3,7 @@ import test from 'ava';
import createTestServer from 'create-test-server';
import body from 'body';
import delay from 'delay';
import ky, {TimeoutError} from '..';
import ky from '..';

const pBody = util.promisify(body);
const fixture = 'fixture';
Expand Down Expand Up @@ -246,7 +246,7 @@ test('timeout option', async t => {
response.end(fixture);
});

await t.throwsAsync(ky(server.url, {timeout: 500}).text(), TimeoutError);
await t.throwsAsync(ky(server.url, {timeout: 500}).text(), ky.TimeoutError);
t.is(requestCount, 1);

await server.close();
Expand All @@ -262,7 +262,7 @@ test('timeout:false option', async t => {
response.end(fixture);
});

await t.notThrowsAsync(ky(server.url, {timeout: false}).text(), TimeoutError);
await t.notThrowsAsync(ky(server.url, {timeout: false}).text(), ky.TimeoutError);
t.is(requestCount, 1);

await server.close();
Expand Down
1 change: 0 additions & 1 deletion umd.d.ts
@@ -1,2 +1 @@
export {default} from 'ky';
export * from 'ky';

0 comments on commit cf6ca95

Please sign in to comment.