diff --git a/.github/funding.yml b/.github/funding.yml deleted file mode 100644 index 1a630e9..0000000 --- a/.github/funding.yml +++ /dev/null @@ -1,3 +0,0 @@ -github: sindresorhus -open_collective: sindresorhus -custom: https://sindresorhus.com/donate diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..d36e1a8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/index.d.ts b/index.d.ts index 502bf7a..59c7d26 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,7 +1,7 @@ /** Create an error from multiple errors. */ -declare class AggregateError extends Error implements Iterable { +export default class AggregateError extends Error implements Iterable { readonly name: 'AggregateError'; /** @@ -10,7 +10,7 @@ declare class AggregateError 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'}]); @@ -34,7 +34,6 @@ declare class AggregateError 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); } @@ -43,9 +42,7 @@ declare class AggregateError extends Error implements I //=> [Error: baz] ``` */ - constructor(errors: ReadonlyArray); + constructor(errors: ReadonlyArray | string>); [Symbol.iterator](): IterableIterator; } - -export = AggregateError; diff --git a/index.js b/index.js index f7de181..89c7713 100644 --- a/index.js +++ b/index.js @@ -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}`); @@ -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}); } @@ -43,5 +43,3 @@ class AggregateError extends Error { } } } - -module.exports = AggregateError; diff --git a/index.test-d.ts b/index.test-d.ts index 76b0f1d..155220e 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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>(aggregateError); +expectAssignable>(aggregateError); expectType>(aggregateError[Symbol.iterator]()); for (const error of aggregateError) { @@ -17,7 +17,7 @@ class CustomError extends Error { public foo: string; constructor(message: string) { - super(message) + super(message); this.name = 'CustomError'; this.foo = 'bar'; } diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (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: diff --git a/package.json b/package.json index 74fcc37..1b30229 100644 --- a/package.json +++ b/package.json @@ -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": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=8" + "node": ">=12" }, "scripts": { "test": "xo && ava && tsd" @@ -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" } } diff --git a/readme.md b/readme.md index 38be285..10e91c3 100644 --- a/readme.md +++ b/readme.md @@ -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'}]); @@ -48,7 +46,6 @@ for (const individualError of error) { //=> [Error: baz] ``` - ## API ### AggregateError(errors) @@ -57,7 +54,7 @@ Returns an `Error` that is also an [`Iterable`](https://developer.mozilla.org/en #### errors -Type: `Array` +Type: `Array` -If a string, a new `Error` is created with the string as the error message.
+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. diff --git a/test.js b/test.js index 585e75c..5ab4bae 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import AggregateError from '.'; +import AggregateError from './index.js'; test('main', t => { const error = new AggregateError([ @@ -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 ]); });