Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 17, 2021
1 parent a5da568 commit 4f8546b
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 38 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
9 changes: 3 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
Create an error from multiple errors.
*/
declare class AggregateError<T extends Error = Error> extends Error implements Iterable<T> {
export default class AggregateError<T extends Error = Error> extends Error implements Iterable<T> {
readonly name: 'AggregateError';

/**
Expand All @@ -10,7 +10,7 @@ declare class AggregateError<T extends Error = Error> extends Error implements I
@example
```
import AggregateError = require('aggregate-error');
import AggregateError from 'aggregate-error';
const error = new AggregateError([new Error('foo'), 'bar', {message: 'baz'}]);
Expand All @@ -34,7 +34,6 @@ declare class AggregateError<T extends Error = Error> extends Error implements I
// at run (bootstrap_node.js:394:7)
// at startup (bootstrap_node.js:149:9)
for (const individualError of error) {
console.log(individualError);
}
Expand All @@ -43,9 +42,7 @@ declare class AggregateError<T extends Error = Error> extends Error implements I
//=> [Error: baz]
```
*/
constructor(errors: ReadonlyArray<T | {[key: string]: any} | string>);
constructor(errors: ReadonlyArray<T | Record<string, any> | string>);

[Symbol.iterator](): IterableIterator<T>;
}

export = AggregateError;
10 changes: 4 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
const indentString = require('indent-string');
const cleanStack = require('clean-stack');
import indentString from 'indent-string';
import cleanStack from 'clean-stack';

const cleanInternalStack = stack => stack.replace(/\s+at .*aggregate-error\/index.js:\d+:\d+\)?/g, '');

class AggregateError extends Error {
export default class AggregateError extends Error {
constructor(errors) {
if (!Array.isArray(errors)) {
throw new TypeError(`Expected input to be an Array, got ${typeof errors}`);
Expand Down Expand Up @@ -34,6 +33,7 @@ class AggregateError extends Error {

this.name = 'AggregateError';

// TODO: Use private class field for this when ESLint support class fields.
Object.defineProperty(this, '_errors', {value: errors});
}

Expand All @@ -43,5 +43,3 @@ class AggregateError extends Error {
}
}
}

module.exports = AggregateError;
8 changes: 4 additions & 4 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {expectType} from 'tsd';
import AggregateError = require('.');
import {expectType, expectAssignable} from 'tsd';
import AggregateError from './index.js';

const aggregateError = new AggregateError([
new Error('foo'),
{foo: 'bar'},
'bar'
]);
expectType<Iterable<Error>>(aggregateError);
expectAssignable<Iterable<Error>>(aggregateError);
expectType<IterableIterator<Error>>(aggregateError[Symbol.iterator]());

for (const error of aggregateError) {
Expand All @@ -17,7 +17,7 @@ class CustomError extends Error {
public foo: string;

constructor(message: string) {
super(message)
super(message);
this.name = 'CustomError';
this.foo = 'bar';
}
Expand Down
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

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:

Expand Down
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Create an error from multiple errors",
"license": "MIT",
"repository": "sindresorhus/aggregate-error",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -30,12 +33,12 @@
"iterator"
],
"dependencies": {
"clean-stack": "^2.0.0",
"indent-string": "^4.0.0"
"clean-stack": "^4.0.0",
"indent-string": "^5.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"tsd": "^0.7.1",
"xo": "^0.25.3"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
9 changes: 3 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
*Note: With [Node.js 15](https://medium.com/@nodejs/node-js-v15-0-0-is-here-deb00750f278), there's now a built-in [`AggregateError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) type.*


## Install

```
$ npm install aggregate-error
```


## Usage

```js
const AggregateError = require('aggregate-error');
import AggregateError from 'aggregate-error';

const error = new AggregateError([new Error('foo'), 'bar', {message: 'baz'}]);

Expand Down Expand Up @@ -48,7 +46,6 @@ for (const individualError of error) {
//=> [Error: baz]
```


## API

### AggregateError(errors)
Expand All @@ -57,7 +54,7 @@ Returns an `Error` that is also an [`Iterable`](https://developer.mozilla.org/en

#### errors

Type: `Array<Error|Object|string>`
Type: `Array<Error|object|string>`

If a string, a new `Error` is created with the string as the error message.<br>
If a string, a new `Error` is created with the string as the error message.\
If a non-Error object, a new `Error` is created with all properties from the object copied over.
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import AggregateError from '.';
import AggregateError from './index.js';

test('main', t => {
const error = new AggregateError([
Expand All @@ -23,7 +23,7 @@ test('main', t => {
new Error('foo'),
new Error('bar'),
Object.assign(new Error('baz'), {code: 'EBAZ'}),
Object.assign(new Error(), {code: 'EQUX'})
Object.assign(new Error(), {code: 'EQUX'}) // eslint-disable-line unicorn/error-message
]);
});

Expand Down

0 comments on commit 4f8546b

Please sign in to comment.