Skip to content

Commit

Permalink
Require Node.js 12 and drop UMD build (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 6, 2021
1 parent 1136e0d commit 1a7ec7f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 79 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ on:
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
runs-on: macos-latest
strategy:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
yarn.lock
.nyc_output
umd.js
coverage
30 changes: 5 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,6 @@

const globals = {};

const getGlobal = property => {
/* istanbul ignore next */
if (typeof self !== 'undefined' && self && property in self) {
return self;
}

/* istanbul ignore next */
if (typeof window !== 'undefined' && window && property in window) {
return window;
}

if (typeof global !== 'undefined' && global && property in global) {
return global;
}

/* istanbul ignore next */
if (typeof globalThis !== 'undefined' && globalThis) {
return globalThis;
}
};

const globalProperties = [
'Headers',
'Request',
Expand All @@ -36,9 +15,8 @@ const globalProperties = [
for (const property of globalProperties) {
Object.defineProperty(globals, property, {
get() {
const globalObject = getGlobal(property);
const value = globalObject && globalObject[property];
return typeof value === 'function' ? value.bind(globalObject) : value;
const value = globalThis[property];
return typeof value === 'function' ? value.bind(globalThis) : value;
}
});
}
Expand Down Expand Up @@ -525,4 +503,6 @@ const createInstance = defaults => {
return ky;
};

export default createInstance();
const ky = createInstance();

export default ky;
21 changes: 5 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,15 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"import": "./index.js",
"require": "./umd.js"
},
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"build": "rollup index.js --format=umd --name=ky --file=umd.js",
"prepare": "npm run build",
"test": "xo && npm run build && nyc ava && tsd"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts",
"umd.js",
"umd.d.ts"
"index.d.ts"
],
"keywords": [
"fetch",
Expand Down Expand Up @@ -58,12 +50,10 @@
"busboy": "^0.3.1",
"create-test-server": "2.1.1",
"delay": "^4.1.0",
"esm": "^3.2.22",
"form-data": "^3.0.0",
"node-fetch": "^2.5.0",
"nyc": "^15.0.0",
"puppeteer": "^5.1.0",
"rollup": "^2.10.2",
"puppeteer": "^5.5.0",
"tsd": "^0.13.1",
"xo": "^0.25.3"
},
Expand All @@ -83,7 +73,6 @@
},
"ava": {
"require": [
"esm",
"./test/_require"
],
"nonSemVerExperiments": {
Expand All @@ -93,7 +82,7 @@
"tsd": {
"compilerOptions": {
"lib": [
"es2018",
"es2019",
"dom"
]
}
Expand Down
22 changes: 0 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ If you are using [Deno](https://github.com/denoland/deno), import Ky from a URL.
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');
```

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.

## API

### ky(input, options?)
Expand Down Expand Up @@ -585,20 +577,6 @@ import ky from 'https://cdn.jsdelivr.net/npm/ky@latest/index.js';
</script>
```

Alternatively, you can use the [`umd.js`](umd.js) file with a traditional `<script>` tag (without `type="module"`), in which case `ky` will be a global.

```html
<script src="https://cdn.jsdelivr.net/npm/ky@latest/umd.js"></script>
<script>
(async () => {
const parsed = await ky('https://jsonplaceholder.typicode.com/todos/1').json();
console.log(parsed.title);
//=> 'delectus aut autem
})();
</script>
```

#### How is it different from [`got`](https://github.com/sindresorhus/got)

See my answer [here](https://twitter.com/sindresorhus/status/1037406558945042432). Got is maintained by the same people as Ky.
Expand Down
37 changes: 25 additions & 12 deletions test/browser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import util from 'util';
import fs from 'fs';
import body from 'body';
import ava from 'ava'; // eslint-disable-line ava/use-test
import createTestServer from 'create-test-server';
import Busboy from 'busboy';
import withPage from './helpers/with-page.js';

const test = ava.serial;
// FIXME: Skipping tests on CI as they're unreliable there for some reason.
// It's serial as Puppeteer cannot handle full concurrency.
const test = process.env.CI ? ava.skip : ava.serial;

const pBody = util.promisify(body);

const kyScript = {
type: 'module',
content: `
${fs.readFileSync(new URL('../index.js', import.meta.url), 'utf8')}
globalThis.ky = ky;
`
};

test('prefixUrl option', withPage, async (t, page) => {
const server = await createTestServer();

Expand All @@ -19,7 +31,7 @@ test('prefixUrl option', withPage, async (t, page) => {
});

await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});
await page.addScriptTag(kyScript);

await t.throwsAsync(
page.evaluate(() => {
Expand Down Expand Up @@ -56,7 +68,7 @@ test('aborting a request', withPage, async (t, page) => {
});

await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});
await page.addScriptTag(kyScript);

const error = await page.evaluate(url => {
const controller = new AbortController();
Expand Down Expand Up @@ -88,7 +100,7 @@ test('aborting a request with onDonwloadProgress', withPage, async (t, page) =>
});

await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});
await page.addScriptTag(kyScript);

const error = await page.evaluate(url => {
const controller = new AbortController();
Expand Down Expand Up @@ -117,7 +129,7 @@ test('throws TimeoutError even though it does not support AbortController', with

await page.goto(server.url);
await page.addScriptTag({path: './test/helpers/disable-abort-controller.js'});
await page.addScriptTag({path: './umd.js'});
await page.addScriptTag(kyScript);

const error = await page.evaluate(url => {
const request = window.ky(`${url}/slow`, {timeout: 500}).text();
Expand Down Expand Up @@ -149,7 +161,7 @@ test('onDownloadProgress works', withPage, async (t, page) => {
});

await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});
await page.addScriptTag(kyScript);

const result = await page.evaluate(async url => {
// `new TextDecoder('utf-8').decode` hangs up?
Expand Down Expand Up @@ -184,7 +196,7 @@ test('throws if onDownloadProgress is not a function', withPage, async (t, page)
});

await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});
await page.addScriptTag(kyScript);

const error = await page.evaluate(url => {
const request = window.ky(url, {onDownloadProgress: 1}).text();
Expand All @@ -204,7 +216,7 @@ test('throws if does not support ReadableStream', withPage, async (t, page) => {

await page.goto(server.url);
await page.addScriptTag({path: './test/helpers/disable-stream-support.js'});
await page.addScriptTag({path: './umd.js'});
await page.addScriptTag(kyScript);

const error = await page.evaluate(url => {
const request = window.ky(url, {onDownloadProgress: () => {}}).text();
Expand Down Expand Up @@ -234,7 +246,7 @@ test('FormData with searchParams', withPage, async (t, page) => {
});

await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});
await page.addScriptTag(kyScript);
await page.evaluate(url => {
const formData = new window.FormData();
formData.append('file', new window.File(['bubblegum pie'], 'my-file'));
Expand Down Expand Up @@ -290,7 +302,7 @@ test('FormData with searchParams ("multipart/form-data" parser)', withPage, asyn
});

await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});
await page.addScriptTag(kyScript);
await page.evaluate(url => {
const formData = new window.FormData();
formData.append('file', new window.File(['bubblegum pie'], 'my-file', {type: 'text/plain'}));
Expand All @@ -317,12 +329,13 @@ test('headers are preserved when input is a Request and there are searchParams i
});

await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});
await page.addScriptTag(kyScript);

await page.evaluate(url => {
const request = new window.Request(url + '/test', {
headers: {'content-type': 'text/css'}
});

return window.ky(request, {
searchParams: 'foo=1'
}).text();
Expand All @@ -347,7 +360,7 @@ test('retry with body', withPage, async (t, page) => {
});

await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});
await page.addScriptTag(kyScript);

await t.throwsAsync(
page.evaluate(async url => {
Expand Down
1 change: 0 additions & 1 deletion umd.d.ts

This file was deleted.

0 comments on commit 1a7ec7f

Please sign in to comment.