Skip to content

Commit 2ff32a0

Browse files
committedJan 8, 2021
Require Node.js 14
1 parent 24822c3 commit 2ff32a0

File tree

10 files changed

+75
-54
lines changed

10 files changed

+75
-54
lines changed
 

‎.github/workflows/main.yml

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ jobs:
1111
matrix:
1212
node-version:
1313
- 14
14-
- 12
15-
- 10
1614
steps:
1715
- uses: actions/checkout@v2
1816
- uses: actions/setup-node@v1

‎browser.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import ky from 'ky';
2+
3+
export default ky;
4+
export * from 'ky';

‎browser.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export {default} from 'ky';
1+
import ky from 'ky';
2+
3+
export default ky;

‎index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import ky from 'ky';
22

3-
export = ky;
3+
export default ky;
4+
export * from 'ky';

‎index.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
'use strict';
2-
const fetch = require('node-fetch');
3-
const AbortController = require('abort-controller');
1+
import fetch, {Headers, Request, Response} from 'node-fetch';
2+
import AbortController from 'abort-controller';
3+
import ky from 'ky';
44

55
const TEN_MEGABYTES = 1000 * 1000 * 10;
66

7-
if (!global.fetch) {
8-
global.fetch = (url, options) => fetch(url, {highWaterMark: TEN_MEGABYTES, ...options});
7+
if (!globalThis.fetch) {
8+
globalThis.fetch = (url, options) => fetch(url, {highWaterMark: TEN_MEGABYTES, ...options});
99
}
1010

11-
if (!global.Headers) {
12-
global.Headers = fetch.Headers;
11+
if (!globalThis.Headers) {
12+
globalThis.Headers = Headers;
1313
}
1414

15-
if (!global.Request) {
16-
global.Request = fetch.Request;
15+
if (!globalThis.Request) {
16+
globalThis.Request = Request;
1717
}
1818

19-
if (!global.Response) {
20-
global.Response = fetch.Response;
19+
if (!globalThis.Response) {
20+
globalThis.Response = Response;
2121
}
2222

23-
if (!global.AbortController) {
24-
global.AbortController = AbortController;
23+
if (!globalThis.AbortController) {
24+
globalThis.AbortController = AbortController;
2525
}
2626

27-
if (!global.ReadableStream) {
27+
if (!globalThis.ReadableStream) {
2828
try {
29-
global.ReadableStream = require('web-streams-polyfill/ponyfill/es2018');
30-
} catch (_) {}
29+
globalThis.ReadableStream = await import('web-streams-polyfill/ponyfill/es2018');
30+
} catch {}
3131
}
3232

33-
module.exports = require('ky/umd');
33+
export default ky;

‎index.test-d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {expectType} from 'tsd';
2+
import ky, {ResponsePromise} from '.';
3+
4+
expectType<ResponsePromise>(ky('https://sindresorhus.com'));

‎license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

‎package.json

+28-8
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,24 @@
1010
"email": "sindresorhus@gmail.com",
1111
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": {
15+
"import": "./browser.js",
16+
"node": "./index.js"
17+
},
1318
"engines": {
14-
"node": ">=10.17"
19+
"node": ">=14"
1520
},
1621
"scripts": {
17-
"test": "xo && ava"
22+
"//": "XO is disabled until ESLint supports top-level await",
23+
"//test": "xo && ava && tsd",
24+
"test": "ava && tsd"
1825
},
1926
"files": [
2027
"index.js",
2128
"index.d.ts",
22-
"browser.js"
29+
"browser.js",
30+
"browser.d.ts"
2331
],
2432
"keywords": [
2533
"ky",
@@ -57,13 +65,14 @@
5765
"node-fetch": "3.0.0-beta.9"
5866
},
5967
"devDependencies": {
60-
"ava": "^2.4.0",
61-
"ky": "^0.17.0",
62-
"xo": "^0.25.3"
68+
"ava": "^3.15.0",
69+
"ky": "^0.26.0",
70+
"tsd": "^0.14.0",
71+
"xo": "^0.37.1"
6372
},
6473
"peerDependencies": {
65-
"ky": ">=0.17.0",
66-
"web-streams-polyfill": ">=2.0.0"
74+
"ky": ">=0.26.0",
75+
"web-streams-polyfill": ">=3.0.1"
6776
},
6877
"peerDependenciesMeta": {
6978
"web-streams-polyfill": {
@@ -72,8 +81,19 @@
7281
},
7382
"browser": "browser.js",
7483
"ava": {
84+
"globals": [
85+
"globalThis"
86+
],
7587
"register": [
7688
"."
7789
]
90+
},
91+
"tsd": {
92+
"compilerOptions": {
93+
"lib": [
94+
"es2019",
95+
"dom"
96+
]
97+
}
7898
}
7999
}

‎readme.md

+15-23
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ $ npm install ky ky-universal
2626
## Usage
2727

2828
```js
29-
const ky = require('ky-universal');
29+
import ky from 'ky-universal';
3030

31-
(async () => {
32-
const parsed = await ky('https://httpbin.org/json').json();
31+
const parsed = await ky('https://httpbin.org/json').json();
3332

34-
//
35-
})();
33+
//
3634
```
3735

3836
## `ReadableStream` support
@@ -46,15 +44,13 @@ $ npm install web-streams-polyfill
4644
You can then use it normally:
4745

4846
```js
49-
const ky = require('ky-universal');
47+
import ky from 'ky-universal';
5048

51-
(async () => {
52-
const {body} = await ky('https://httpbin.org/bytes/16');
53-
const {value} = await body.getReader().read();
54-
const result = new TextDecoder('utf-8').decode(value);
49+
const {body} = await ky('https://httpbin.org/bytes/16');
50+
const {value} = await body.getReader().read();
51+
const result = new TextDecoder('utf-8').decode(value);
5552

56-
//
57-
})();
53+
//
5854
```
5955

6056
## API
@@ -70,11 +66,9 @@ Use it like you would use Ky:
7066
```js
7167
import ky from 'ky-universal';
7268

73-
(async () => {
74-
const parsed = await ky('https://httpbin.org/json').json();
69+
const parsed = await ky('https://httpbin.org/json').json();
7570

76-
//
77-
})();
71+
//
7872
```
7973

8074
Webpack will ensure the polyfills are only included and used when the app is rendered on the server-side.
@@ -104,14 +98,12 @@ However, you can specify a custom `highWaterMark` if needed:
10498
```js
10599
import ky from 'ky-universal';
106100

107-
(async () => {
108-
const response = await ky('https://example.com', {
109-
// 20 MB
110-
highWaterMark: 1000 * 1000 * 20
111-
});
101+
const response = await ky('https://example.com', {
102+
// 20 MB
103+
highWaterMark: 1000 * 1000 * 20
104+
});
112105

113-
const data = await response.clone().buffer();
114-
})();
106+
const data = await response.clone().buffer();
115107
```
116108

117109
## Related

‎test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import ky from '.';
2+
import ky from './index.js';
33

44
test('main', async t => {
55
const {slideshow} = await ky('https://httpbin.org/json').json();

0 commit comments

Comments
 (0)
Please sign in to comment.